如何将对象旋转到鼠标在Unity中的位置?

时间:2018-01-19 13:17:26

标签: c# unity3d rotation mouseevent

我对播放器有一点问题。

我已经实现了一个代码,使对象慢慢朝着鼠标的位置移动。

问题:对象不旋转,我希望它旋转,指向鼠标。

private Vector3 _target;
public Camera Camera;
public bool FollowMouse;
public bool ShipAccelerates;
public float ShipSpeed = 2.0f;

public void OnEnable()
{
    if (Camera == null)
    {
        throw new InvalidOperationException("Camera not set");
    }
}

public void Update()
{
    if (FollowMouse || Input.GetMouseButton(0))
    {
        _target = Camera.ScreenToWorldPoint(Input.mousePosition);
        _target.z = 0;
    }

    var delta = ShipSpeed * Time.deltaTime;
    if (ShipAccelerates)
    {
        delta *= Vector3.Distance(transform.position, _target);
    }

    transform.position = Vector3.MoveTowards(transform.position, _target, delta);
}

如果有人帮助我,我将不胜感激 谢谢!

0 个答案:

没有答案