我怎样才能左右移动圆柱体?

时间:2016-11-02 14:28:35

标签: c# unity3d

在两点之间或仅在左侧不间断或仅在右侧不间断。

在这段代码中我旋转了圆柱体,但是我无法将它移到两侧:

using UnityEngine;
using System.Collections;

public class MakeTwoPoints3D : MonoBehaviour
{
    public float speed = 10f;

    public float delta = 15.5f;  // Amount to move left and right from the start point
    public float moveSpeed = 5.0f;
    private Vector3 startPos;

    void Start()
    {
        startPos = transform.position;
    }

    void Update()
    {
        transform.Rotate(Vector3.up, speed * Time.deltaTime);
        transform.position += transform.right * Time.deltaTime * moveSpeed;
    }
}

如果我进行transform.right,它会将圆柱体在圆圈中上下移动。如果我进行transform.up它将它移动给我,我的意思是像前进但相机,但至少它会移动它。如果我进行转换。再次转发它会产生圆圈并将圆圈向上移除。

我无法弄清楚如何将它移到两侧。

1 个答案:

答案 0 :(得分:2)

您需要使用Vector3.right代替transform.right

void Update()
{
    transform.Rotate(Vector3.up, speed * Time.deltaTime);
    transform.position += Vector3.right * Time.deltaTime * moveSpeed;
}

使用transform.right时,Vector3将采用该对象变换的局部旋转。意思是,如果对象绕Y轴旋转45度,则transform.right向量将处于一个角度。如果您在旋转对象时沿着它的本地轴进行平移,它将会以圆形运行。

另一方面,Vector3.right总是在世界空间中,所以它总是面对" true"右。