Unity - 主题可以移动WASD,但是WASD不会转向相机指向的位置

时间:2014-06-13 19:43:41

标签: unity3d unityscript

我正在遵循一些关于角色控制的指南,并复制了以下脚本:

#pragma strict

    var walkAcceleration : float = 5;
    var walkDeacceleration : float =5;
    @HideInInspector var walkDeaccelerationVolx : float;
    @HideInInspector var walkDeaccelerationVolz : float;

    var cameraObject : GameObject;
    var maxWalkSpeed : float = 10;
    @HideInInspector var horizontalMovement : Vector2;

    //Jumping Variables
    var jumpVelocity : float = 275;
    var grounded : boolean = false;
    var maxSlope : float = 60;

function Update () {

    horizontalMovement = Vector2(rigidbody.velocity.x, rigidbody.velocity.z);
    if(horizontalMovement.magnitude > maxWalkSpeed){

        horizontalMovement = horizontalMovement.normalized;
        horizontalMovement *= maxWalkSpeed;

    }


    rigidbody.velocity.x = horizontalMovement.x;
    rigidbody.velocity.z = horizontalMovement.y;

    if(Input.GetAxis("Horizontal") == 0 && Input.GetAxis("Vertical") ==0){
        rigidbody.velocity.x = Mathf.SmoothDamp(rigidbody.velocity.x, 0, walkDeaccelerationVolx, walkDeacceleration);
        rigidbody.velocity.z = Mathf.SmoothDamp(rigidbody.velocity.z, 0, walkDeaccelerationVolz, walkDeacceleration);
    }

    transform.rotation = Quaternion.Euler(0, cameraObject.GetComponent(MouseLook).currentYRotation, 0);
    //New movement to remove latency in movement.
    transform.position = Vector3(transform.position.x + Input.GetAxis("Horizontal") * walkAcceleration * Time.deltaTime, transform.position.y ,
                                 transform.position.z + Input.GetAxis("Vertical") * walkAcceleration * Time.deltaTime);


    //Old movement
    //rigidbody.AddRelativeForce(Input.GetAxis("Horizontal") * walkAcceleration, 0, Input.GetAxis("Vertical") * walkAcceleration);

    if (Input.GetButtonDown("Jump") && grounded)
        rigidbody.AddForce(0, jumpVelocity, 0);

}

function OnCollisionStay (collision : Collision){

    for(var contact : ContactPoint in collision.contacts) {

        if(Vector3.Angle(contact.normal, Vector3.up) < maxSlope)
        grounded = true;

    }
}

function OnCollisionExit(){
    grounded = false;
    }

问题与(我确定)有关:

  

transform.rotation = Quaternion.Euler(0, cameraObject.GetComponent(MouseLook).currentYRotation, 0);

但是,我并不完全确定如何修改此行以使其适用于我的修改。

1 个答案:

答案 0 :(得分:0)

你想旋转相机,但我没有看到在任何地方设置旋转。你只需要从玩家的旋转中取Y轴(我相信它是Y)并将其设置为Camera,即。像这样的东西(idk UnityScript的语法,所以这里是C#):

Vector3 newRotation = cameraObject.transform.rotation;
newRotation.y = transform.rotation.y;
cameraObject.transform.rotation = newRotation;