1

I'm creating a simple side scroller with a couple people using Unity and am definitely a beginner. The character being used is 3D and runs forward well, but when running backwards he still faces forward. I have the controls setup in the InputManager so pressing A moves backwards and D moves forward but I'm not sure what to do so he faces his respective movement.

Any help would be greatly appreciated and if you need more information besides the following code I have for the movement let me know, it's based off another post I found.

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();     
}

For what it's worth another problem I'm having involves having two different animations being able to work at the same time, like run and attack. I figured I should mention while I'm here if anybody knew how to go about that. Thank you again for your time!

CodeSmile
  • 64,284
  • 20
  • 132
  • 217
eg3
  • 21
  • 4
  • Have you tried to modify transform.localRotation = new Vector(0, 180,0); ? – David Feb 09 '14 at 01:57
  • Hm I get back: "Cannot convert 'UnityEngine.Vector3' to 'UnityEngine.Quaternion'", (I'm assuming you meant Vector3). I'm thinking I might just use GetKey instead of the InputManager – eg3 Feb 09 '14 at 02:17
  • Aha, it is my mistake. I have corrected it as shown in answer below. – David Feb 09 '14 at 02:21

1 Answers1

1

I ended up solving this based off a different code I stumbled upon, then called the function inside of 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);           
}
eg3
  • 21
  • 4