1

我正在与几个使用 Unity 的人一起创建一个简单的横向滚动条,我绝对是个初学者。正在使用的角色是 3D 并且可以很好地向前跑,但是当向后跑时,他仍然面向前方。我在 InputManager 中设置了控件,因此按 A 向后移动,D 向前移动,但我不确定该怎么做,所以他面对各自的动作。

任何帮助将不胜感激,如果您需要除了以下代码之外的更多信息,请告诉我,它基于我发现的另一篇文章。

var speed : float = 6.0;
var jumpSpeed : float = 6.0;
var gravity : float = 12.0;

//This variable is now inheriting the Vector3 info (X,Y,Z) and setting them to 0.
private var moveDirection : Vector3 = Vector3.zero;

function MoveAndJump() {

    var controller : CharacterController = GetComponent(CharacterController);

    if(controller.isGrounded) {           
        //moveDirection is inheriting Vector3 info.  This now sets the X and Z coords to receive the input set to "Horizontal" and "Vertical"
        moveDirection = Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")); //Allows player Input
        moveDirection = transform.TransformDirection(moveDirection);    //How to move
        moveDirection *= speed;     //How fast to move

        if(Input.GetButtonDown("Jump")) {
            animation.Play("Jump");
            moveDirection.y = jumpSpeed;
        }
    }

    //Apply gravity
    moveDirection.y -= gravity * Time.deltaTime;

    //This moves the controller
    controller.Move(moveDirection * Time.deltaTime);

    if(Input.GetButton("Fire1")){
        animation.Play("Attack");
    } 

    if(Input.GetButton("Vertical")){
        animation.Play("Run"); 
    }     
}

function Update() {     
    MoveAndJump();     
}

我遇到的另一个问题是让两个不同的动画能够同时工作,比如跑步和攻击。我想我应该在我在这里的时候提一下,如果有人知道该怎么做的话。再次感谢您的宝贵时间!

4

1 回答 1

1

我最终根据我偶然发现的不同代码解决了这个问题,然后在 Update() 中调用了该函数:

var speed : float;        
var jumpSpeed : float;        
var gravity : float;

private var moveDirection : Vector3 = Vector3.zero;

function MoveJumpAttack() {
    var controller : CharacterController = GetComponent(CharacterController);

    if (controller.isGrounded) { 
        moveDirection = Vector3(Input.GetAxis("Horizontal"), 0, 0);
        moveDirection *= speed;

        if (moveDirection.sqrMagnitude > 0.01)
            transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation (moveDirection), 90);
    }

    moveDirection.y -= gravity * Time.deltaTime;
    controller.Move(moveDirection * Time.deltaTime);           
}
于 2014-02-10T05:05:14.500 回答