如何相对于相机方向进行此移动

时间:2015-04-29 15:50:51

标签: c# unity3d camera vectormath

我需要相对于主摄像机所朝向的方向在世界空间中移动我的目标对象,但仅在x& z轴上,并且相对于y轴上的我的播放器。

非常感谢任何指导。

public class test : MonoBehaviour {

public string raise = "Raise";
public string lower = "Lower";
public string left = "Left";
public string right = "Right";
public string closer = "Closer";
public string further = "Further";

public GameObject target;


private float xPos;
private float yPos;
private float zPos;


// Use this for initialization
void Start () {


    xPos = target.transform.position.x;
    yPos = target.transform.position.y;
    zPos = target.transform.position.z;

}

void FixedUpdate () {

    Vector3 currPos = target.transform.position;
    Vector3 nextPos = new Vector3 (xPos, yPos, zPos);

    target.GetComponent < Rigidbody > ().velocity = (nextPos - currPos) * 10;

    if (Input.GetButton (raise)) {
        print ("Moving Up");
        yPos = yPos + 0.05f;
    }
    if (Input.GetButton (lower)) {
        print ("Moving Down");
        yPos = yPos - 0.05f;
    }
    if (Input.GetButton (left)) {
        print ("Moving Left");
        xPos = xPos - 0.05f;
    }
    if (Input.GetButton (right)) {
        print ("Moving Right");
        xPos = xPos + 0.05f;
    }
    if (Input.GetButton (closer)) {
        print ("Moving Closer");
        zPos = zPos - 0.05f;
    }
    if (Input.GetButton (further)) {
        print ("Moving Further");
        zPos = zPos + 0.05f;
    }
}

0 个答案:

没有答案