让Unity3d中的玩家基于相机移动?

时间:2016-08-25 11:45:28

标签: javascript c# unity3d unityscript unity5

所以我正在尝试制作一个第三人称玩家,现在我将所有东西都设置为一个问题,玩家在移动时总是移动到相同的方向,无论相机在哪里,这使得运动非常困难。

我的播放器控制器:

public class Character_Controler : MonoBehaviour
{
public float speed = 6f;
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 (h != 0 || v != 0 && !jump) Move(h,v);
}

        void Move (float h, float v)

    {
    movement.Set(h, 0f, v);
    movement = movement.normalized * speed * Time.deltaTime;
    playerRbody.MovePosition(transform.position + movement);
    Quaternion newRotation = Quaternion.LookRotation(movement);
    playerRbody.MoveRotation(newRotation);
    }



}

相机的控制器(我没有这样做):

      var target : Transform;
var distance = 10.0;
var xSpeed = 250.0;
var ySpeed = 120.0;

var yMinLimit = -20;
var yMaxLimit = 80;

private var x = 0.0;
private var y = 0.0;

@script AddComponentMenu("Camera-Control/Mouse Orbit")

function Start () {
var angles = transform.eulerAngles;
x = angles.y;
y = angles.x;

// Make the rigid body not change rotation
if (GetComponent.<Rigidbody>())
    GetComponent.<Rigidbody>().freezeRotation = true;
}

function LateUpdate () {
if (target) {
    x += Input.GetAxis("Mouse X") * xSpeed * 0.02;
    y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;

    y = ClampAngle(y, yMinLimit, yMaxLimit);

    var rotation = Quaternion.Euler(y, x, 0);
    var position = rotation * Vector3(0.0, 2.0, -distance) + target.position;

    transform.rotation = rotation;
    transform.position = position;
}
}

static function ClampAngle (angle : float, min : float, max : float) {
if (angle < -360)
    angle += 360;
if (angle > 360)
    angle -= 360;
return Mathf.Clamp (angle, min, max);
}

0 个答案:

没有答案