简体   繁体   English

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

[英]How can I make a gameobject move 'forward' in the direction it's facing?

So I have these two circles. 所以我有两个圈子。 The white one is supposed to jump away from the coloured one in the direction that it's turned. 白色的应该在旋转方向上从彩色的跳跃。 It works sometimes, but sometimes it jumps at the wrong angle or barely moves at all. 它有时可以工作,但有时会跳错角度或几乎不移动。 This is the code I have: 这是我的代码:

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;    
}

I'm on mobile so sorry if the formatting is a little off. 我正在移动设备上,如果格式有些问题,请您谅解。 Does anyone know how to fix this? 有谁知道如何解决这一问题?

There is no need to rotate Vector3.up / Vector3.forward by transform.rotation, transform class offers getters that calculate the correct vector for you 无需通过transform.rotation旋转Vector3.up / Vector3.forward,transform类提供了为您计算正确矢量的吸气剂

Vector3 forward=transform.forward;

Gives you this objects forward, use transform.parent.forward to get parent heading 向您提供此对象,使用transform.parent.forward获取父标题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM