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

C# 如何相对于相机方向进行此移动,c#,unity3d,camera,vectormath,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"; pub

我需要在世界空间中相对于主摄影机面对的方向移动目标对象,但仅在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;
    }
}
非常感谢您的指导

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;
    }
}
公共类测试:单一行为{
公共字符串raise=“raise”;
公共字符串lower=“lower”;
公共字符串left=“left”;
公共字符串right=“right”;
公共字符串closer=“closer”;
公共字符串further=“further”;
公共游戏对象目标;
私人浮动XPO;
私人浮动YPO;
私人浮动zPos;
//用于初始化
无效开始(){
xPos=target.transform.position.x;
yPos=target.transform.position.y;
zPos=目标.transform.position.z;
}
无效固定更新(){
向量3 currPos=target.transform.position;
Vector3 nextPos=新Vector3(XPO、YPO、zPos);
target.GetComponent().velocity=(nextPos-currPos)*10;
if(Input.GETBUTON(升起)){
打印(“上移”);
yPos=yPos+0.05f;
}
if(Input.GETBUTON(下)){
打印(“向下移动”);
yPos=yPos-0.05f;
}
if(Input.GetButton(左)){
打印(“左移”);
xPos=xPos-0.05f;
}
if(Input.GetButton(右)){
打印(“向右移动”);
xPos=xPos+0.05f;
}
if(Input.GetButton(closer)){
打印(“靠近”);
zPos=zPos-0.05f;
}
if(Input.GetButton(进一步)){
打印(“进一步移动”);
zPos=zPos+0.05f;
}
}

您可以像这样获得相机的方向:

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;
    }
}
var camDir = Camera.main.transform.forward;
您只需要x/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;
    }
}
camDir.y = 0;
camDir.Normalized();
var playerUp = Vector3.up;
这是正向矢量。因为它现在实际上是一个二维矢量,我们可以很容易地得到凸轮的右侧矢量:

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;
    }
}
var camRight = new Vector3(camDir.z, 0f, -camDir.x);
我假设你的玩家的上方向是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;
    }
}
camDir.y = 0;
camDir.Normalized();
var playerUp = Vector3.up;
现在,在您的示例中,您正在进行手动积分,然后将其传递给刚体系统以再次进行积分。让我们直接计算出我们自己的速度:

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;
    }
}
var newVel = Vector3.zero;
if (/*left*/) newVel -= camRight * 0.05;
if (/*right*/) newVel += camRight * 0.05;
if (/*closer*/) newVel -= camDir * 0.05;
if (/*farter*/) newVel += camDir * 0.05;
if (/*raise*/) newVel += playerUp * 0.05;
if (/*lower*/) newVel -= playerUp * 0.05;
如果你想移动得更快或更慢,可以更改为0.05。你可以在这里做很多事情来让控件感觉非常好,比如有一个小死区,或者直接输入模拟输入而不是按钮

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;
    }
}
然后最后将其提交到刚体中:

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;
    }
}
target.GetComponent<Rigidbody>().velocity = newVel;
target.GetComponent().velocity=newVel;

谢谢你的精彩回答,tenpn,这确实帮了我的忙。但现在,当我松开上升按钮时,高度会重置。那么,我要去currentPos-newVel吗?我需要更多详细信息。你从地板开始,点击上升,然后发生什么?你想发生什么?好的,我明白了,这是一个遥控系统,所以当我按下上升对象按钮时,它应该d设置物体离地面的高度,当我松开按钮时,保持在该位置。然后刚体是错误的答案。执行类似操作,但将
.velocity=call
替换为
target.transform.position+=newVel*Time.deltaTime
。如果刚体不需要移动,则将其从物体上移除我现在很困惑,为什么我不能使用刚体?有没有办法得到我现有的相对于相机的代码?当我像我需要的那样松开按钮时,它就会悬浮起来。