在Unity 5中向后调试角色移动

时间:2016-05-24 17:44:37

标签: c# debugging unity3d game-physics unity5

我有一个角色(机器人凯尔),我可以将角色从一侧移动到另一侧,然后向前移动。当角色开始向后移动时,它会无缘无故地消失。

另外,如何将角色朝鼠标方向移动?我有它所以它面向鼠标的方向,但相机角度扭曲了一点点。

using UnityEngine;
using System.Collections;

public class PlayerScript : MonoBehaviour {

    public GameControlScript control;

    float strafeSpeed = 2;
    Animator anim;

    bool jumping = false;

    void Start () {
        anim = GetComponent<Animator>();
    }

    void Update () {
        if (Input.GetKey(KeyCode.RightArrow)){

            Vector3 newPosition = this.transform.position;
            newPosition.x++;
            this.transform.position = newPosition;

        }
        if (Input.GetKey(KeyCode.LeftArrow)){

            Vector3 newPosition = this.transform.position;
            newPosition.x--;
            this.transform.position = newPosition;

        }
        if (Input.GetKey(KeyCode.UpArrow)){

            Vector3 newPosition = this.transform.position;
            newPosition.z++;
            this.transform.position = newPosition;


        }
        if (Input.GetKey(KeyCode.DownArrow)){

            Vector3 newPosition = this.transform.position;
            newPosition.z--;
            this.transform.position -= newPosition;

        }
        Vector3 mousePos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 1000);
        Vector3 lookPos = Camera.main.ScreenToWorldPoint(mousePos);
        lookPos = lookPos - this.transform.position;
        float angle = Mathf.Atan2(lookPos.z, lookPos.x) * Mathf.Rad2Deg;
        this.transform.rotation = Quaternion.AngleAxis(angle, Vector3.down);

    }

    void OnTriggerEnter(Collider other)
    {
        if(other.gameObject.name == "Powerup(Clone)")
        {
            control.PowerupCollected();
        }
        else if(other.gameObject.name == "Obstacle(Clone)" &&
            jumping == false)
        {
            control.SlowWorldDown();
        }

        Destroy(other.gameObject);
    }
}

1 个答案:

答案 0 :(得分:0)

ScreenToWorldPoint可能是您的问题,因为您将1000固定为相机与物体之间的距离。

结帐link并尝试记录您的lookPos变量。