因此,为了统一,我一直在努力让这个第三人称控制器脚本工作,到目前为止,它是按照我希望的方式工作的,只是现在我被困在如何让他同时向前和向左移动(或其他方向)的问题上,因为现在他开始崩溃了。
移动代码:
using UnityEngine;
using System.Collections;
using UnityStandardAssets.CrossPlatformInput;
public class Character_Controler : MonoBehaviour
{
[SerializeField]
public float speed = 1f;
// the world-relative desired move direction, calculated from the camForward and user input.
private bool canJump = false;
Vector3 movement;
Rigidbody playerRbody;
void Awake()
{
playerRbody = GetComponent<Rigidbody>();
}
private void FixedUpdate()
{
playerRbody.AddForce(Physics.gravity * playerRbody.mass);
// float h = Input.GetAxisRaw("Horizontal");
// float v = Input.GetAxisRaw("Vertical");
var jump = Input.GetKeyDown(KeyCode.Space);
if (!canJump && playerRbody.velocity.y == 0)
{
canJump = true;
}
if (jump && canJump == true)
{
playerRbody.AddForce(new Vector3(0, 6f, 0), ForceMode.Impulse);
canJump = false;
}
if (Input.GetKey(KeyCode.W))
{
moveForward(speed);
}
else if (Input.GetKey(KeyCode.S))
{
moveBack(speed);
}
if (Input.GetKey(KeyCode.A))
{
moveLeft(speed);
}
else if (Input.GetKey(KeyCode.D))
{
moveRight(speed);
}
transform.localEulerAngles = new Vector3(0, transform.localEulerAngles.y, transform.localEulerAngles.z);
//transform.forward = Camera.main.transform.forward;
}
}
private void moveForward(float speed)
{
transform.forward = Camera.main.transform.forward;
// Quaternion newRotation = Quaternion.LookRotation(transf);
// playerRbody.MoveRotation(newRotation);
transform.localPosition += transform.forward * speed * Time.deltaTime;
}
private void moveBack(float speed)
{
transform.forward = -Camera.main.transform.forward;
transform.localPosition += transform.forward * speed * Time.deltaTime;
}
private void moveRight(float speed)
{
transform.forward = Camera.main.transform.right;
transform.localPosition += transform.forward * speed * Time.deltaTime;
}
private void moveLeft(float speed)
{
transform.forward = -Camera.main.transform.right;
transform.localPosition += transform.forward * speed * Time.deltaTime;
}
}发布于 2016-08-30 00:55:12
不要那样处理变换位置。
https://docs.unity3d.com/ScriptReference/Vector3.MoveTowards.html
https://docs.unity3d.com/ScriptReference/Rigidbody.html
和Vector3.Up * 6f与新的Vector3(0,6,0)相同;如果你想让你的球员向上移动,但他的旋转方式不同,他不会使用新的Vector3()相对于他的旋转向上移动,使用它的变换来找出up指向的地方。
我也建议不要把播放器从相机上移到右/上/左/等等。
https://stackoverflow.com/questions/39211087
复制相似问题