【问题标题】:Moving gameobject in a smooth circle around the player object围绕玩家对象以平滑的圆圈移动游戏对象
【发布时间】:2015-08-31 15:00:02
【问题描述】:

如果有区别,我正在使用 unity 5.1.2。

我有一个游戏对象盾牌,我想绕着玩家转一圈。我让它在一定程度上工作,盾牌对输入的反应很好,但没有动画,因为它只是传送到新的位置,而不是在一个圆中平滑旋转。游戏是 2D 自上而下的,因此只能在 x/y 平面上工作。尝试使用 lerp 和 slerp 但没有得到任何乐趣

非常感谢您帮助解决这个问题!

这是我目前所拥有的:

public class ShieldMovement : MonoBehaviour {

    public Transform target; //player shield is attaced to

    float distance = 0.8f; // distance from player so it doesn't clip
    Vector3 direction = Vector3.up;


    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {

        float angle = Mathf.Atan2 (Input.GetAxisRaw("rightH"), Input.GetAxisRaw("rightV"))* Mathf.Rad2Deg;

        if(Input.GetAxis("rightH") != 0f || Input.GetAxis("rightV") != 0f)
        {
            direction =  new Vector3(Input.GetAxis("rightH"),Input.GetAxis("rightV"), 0.0f ) ;
        }

        Ray ray = new Ray(target.position, direction);
        transform.position = ray.GetPoint(distance);

        if(Input.GetAxis("rightH") != 0f || Input.GetAxis("rightV") != 0f)
        {
            transform.rotation = Quaternion.AngleAxis(angle,Vector3.forward*-1);
        }
    }
}

【问题讨论】:

  • 你是想让transform.position顺利移动还是transform.rotation
  • 可以为您节省一些代码的方法是将盾牌放在空变换下,然后将空变换置于播放器下的中心和父级。这样,您不需要弄清楚盾牌的旋转/位置,只需弄清楚其父级的旋转即可。 (只是提供您当前方法的替代方案。)
  • @StevenMills transform.position。本质上,如果盾牌位于右下角,并且我将摇杆直接移到左上角,则盾牌会从左下角消失并立即出现在右上角,而不是平稳地绕圈移动直到到达新点。跨度>

标签: c# unity3d rotation jquery-animate


【解决方案1】:

试试这个。有这么多对Input.GetAxis("...") 的方法调用,您真的只想在开始时调用它一次并将其缓存到一个变量中以加快速度。此外,您实际上并不需要检查两次if(Input.GetAxis("rightH") != 0f || Input.GetAxis("rightV") != 0f),所以我将所有内容都放入其中一项检查中。我添加了一个乘以 Time.smoothDeltaTime 以希望为您解决问题

float h = Input.GetAxis("rightH");
float v = Input.GetAxis("rightV");

float angle = Mathf.Atan2(h, v) * Mathf.Rad2Deg;

if(h != 0f || v != 0f)
{
    float step = Time.smoothDeltatime * speed;
    direction = Vector3.RotateTowards(transform.forward, new Vector2(h, v).normalized, step, 0.0F);
    Ray2D ray = new Ray2D(target.position, direction);
    transform.position = ray.GetPoint(distance);
    transform.rotation = Quaternion.AngleAxis(angle, -Vector3.forward);
}

【讨论】:

  • 感谢关于方法调用的提示,我的代码现在看起来好多了!但是添加 Time.smoothDeltaTime 对改变角度没有任何作用,并且破坏了我的旋转 xD
  • 代码有效,但只是和以前一样,没有围绕圆的曲线平滑移动
  • 现在可以试试吗?我注意到方向没有标准化,Quaternion 现在乘以旋转
  • 盾牌仍然会传送到新的位置,但现在只要我握住那个方向的棍子,盾牌就会原地旋转。我的代码中的旋转很好,因为它使盾牌以任何给定的角度面向正确的方向,但是问题在于围绕玩家的移动。
  • 我想如果我能找到 Lerp 方向向量的方法,它会间接地通过 Ray 对 transform.position 进行 Lerp 吗?虽然我似乎无法让它工作
【解决方案2】:

经过大量搜索和一些帮助,我终于解决了问题,谢谢大家 :) 这是我想出的解决方案

public class ShieldMovement : MonoBehaviour {

public Transform target; //player shield is attaced to

float distance = 0.8f; // distance from player so it doesn't clip
Vector3 direction = Vector3.up;
public float circSpeed = 0.1f; // Speed Shield moves around ship


// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {

    float rh = Input.GetAxisRaw("rightH");
    float rv = Input.GetAxisRaw("rightV");

    if(Mathf.Abs(rh) > 0.15f || Mathf.Abs(rv) > 0.15f)
    {
        Vector3 targetDir = new Vector3(rh, rv, 0.0f);
        direction  = Vector3.Slerp(transform.position-target.position, targetDir, circSpeed);

    }

    Ray ray = new Ray(target.position, direction); // cast ray in direction of point on circle shield is to go

    transform.position = ray.GetPoint(distance); //move shield to the ray as far as the radius

    float angleY = transform.position.y - target.position.y;
    float angleX = -(transform.position.x - target.position.x);

    float angle = Mathf.Atan2 (angleY, angleX) * Mathf.Rad2Deg-90; //Get angle

    if(Mathf.Abs(rh) > 0.15f || Mathf.Abs(rv) > 0.15f)
    {
        transform.rotation = Quaternion.AngleAxis(angle,Vector3.forward*-1); // keep shield facing outwards in respect to player, will need revisiting on animating shield properly
    }


}

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多