Unity:使用InputManager(侧卷轴)时更改方向字符面

时间:2014-02-09 00:32:02

标签: animation unity3d unityscript side-scroller

我正在创建一个简单的侧卷轴,有几个人使用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();     
}

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

1 个答案:

答案 0 :(得分: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);           
}