在 Unity 5 中调试角色向后移动
Debug character movement backwards in Unity 5
我有一个角色(机器人 Kyle),我可以将角色从一侧移动到另一侧,然后向前移动。当角色开始向后移动时,它会无缘无故地消失。
另外,我如何将角色移动到鼠标的方向?我有它所以它朝向鼠标的方向,但相机角度有点扭曲。
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);
}
}
ScreenToWorldPoint
可能是您的问题,因为您将 1000 固定为相机和物体之间的距离。
查看此 link 并尝试记录您的 lookPos
变量。
我有一个角色(机器人 Kyle),我可以将角色从一侧移动到另一侧,然后向前移动。当角色开始向后移动时,它会无缘无故地消失。
另外,我如何将角色移动到鼠标的方向?我有它所以它朝向鼠标的方向,但相机角度有点扭曲。
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);
}
}
ScreenToWorldPoint
可能是您的问题,因为您将 1000 固定为相机和物体之间的距离。
查看此 link 并尝试记录您的 lookPos
变量。