【问题标题】:How can I make a gameobject move 'forward' in the direction it's facing?如何让游戏对象朝着它所面对的方向“向前”移动?
【发布时间】:2018-07-02 20:02:54
【问题描述】:

所以我有these 两个圈子。白色的应该在它转向的方向上从有色的那个跳开。它有时会起作用,但有时会以错误的角度跳跃或几乎不移动。这是我的代码:

void Update () {       
    if (Input.GetKeyDown(KeyCode.Space)) {
        jumpDirection = transform.parent.rotation * Vector3.up;
        transform.parent = null;    
        go1.AddComponent();        
        go2.AddComponent();     
    }
    if(transform.parent == null) {    
        Vector3 rrotation = jumpDirection;      
        transform.Translate(rrotation * Time.deltaTime, Space.World);    
    }    
}

void OnCollisionEnter2D(Collision2D col) {       
    if (col.collider.tag == "sun") {    
        Destroy(gameObject);    
    }    
    if(col.collider.tag == "border") {    
        Destroy(gameObject);    
    }    
    transform.SetParent(col.gameObject.transform);    
    transform.rotation = transform.parent.rotation;    
}

我在移动设备上,如果格式有点不对,请见谅。有谁知道如何解决这个问题?

【问题讨论】:

  • 对你的游戏对象使用 transform.forward。

标签: c# unity3d


【解决方案1】:

不需要通过transform.rotation来旋转Vector3.up / Vector3.forward,transform类提供了getter来为你计算正确的向量

Vector3 forward=transform.forward;

给你这个对象转发,使用 transform.parent.forward 获取父标题

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-21
    • 1970-01-01
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    相关资源
    最近更新 更多