如何使游戏对象朝其所面对的方向“向前”移动?

时间:2018-07-01 23:58:09

标签: c# unity3d

所以我有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;    
}

我正在使用移动设备,如果格式有些问题,请您谅解。有人知道如何解决这个问题吗?

1 个答案:

答案 0 :(得分:2)

无需通过transform.rotation旋转Vector3.up / Vector3.forward,transform类提供了为您计算正确矢量的吸气剂

Vector3 forward=transform.forward;

将此对象转发给您,请使用transform.parent.forward获取父标题

相关问题