【发布时间】:2018-12-27 17:08:58
【问题描述】:
我有这个仍在开发中的统一游戏。到目前为止,我的移动代码可以用箭头键移动玩家。我还有一个用鼠标移动播放器的功能。它让玩家知道它是如何面对的。假设玩家面向左侧。如果我单击向上箭头键,它仍会向前移动。我想将玩家移动到它所面对的方向。
代码:
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour {
public float distance = 5f;
public Transform playerCam;
void FixedUpdate () {
transform.localEulerAngles = new Vector3(transform.localEulerAngles.x,
Camera.main.transform.localEulerAngles.y, transform.localEulerAngles.z);
if (Input.GetKey(KeyCode.LeftArrow))
{
Vector3 position = this.transform.position;
position.x--;
this.transform.position = position;
}
if (Input.GetKey(KeyCode.RightArrow))
{
Vector3 position = this.transform.position;
position.x++;
this.transform.position = position;
}
if (Input.GetKey(KeyCode.UpArrow))
{
Vector3 position = this.transform.position;
position.z++;
this.transform.position = position;
}
if (Input.GetKey(KeyCode.DownArrow))
{
Vector3 position = this.transform.position;
position.z--;
this.transform.position = position;
}
if (Input.GetKey("W"))
{
Vector3 position = this.transform.position;
position.z++;
this.transform.position = position;
}
if (Input.GetKey("S"))
{
Vector3 position = this.transform.position;
position.z--;
this.transform.position = position;
}
if (Input.GetKey("A"))
{
Vector3 position = this.transform.position;
position.x--;
this.transform.position = position;
}
if (Input.GetKey("D"))
{
Vector3 position = this.transform.position;
position.x++;
this.transform.position = position;
}
}
}
【问题讨论】:
-
谢谢,但我需要知道如何移动播放器,而不是看它。