Search Unity

Character Controller Move Backwards

Discussion in 'Animation' started by WheresMommy, Mar 25, 2015.

  1. WheresMommy

    WheresMommy

    Joined:
    Oct 4, 2012
    Posts:
    890
    Hey there everyone,

    I am just working on a game which has kind of dark souls controls. Currently everything is working fine, you can run around, rotate the camera independently and you can use the shoulder button on the controller to like focus in a direction. In this state, the player is able to run forward, backward and sideways. And here is the problem. In the code below, you will see, that I am moving the character forward with the speed float. But as it can never be negative in my code, my character does not want to go backwards. If I make it negative, it will fly up because of negative y floats on the moveDirection variable.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class AnimatorScript : MonoBehaviour {
    5.  
    6.     private CharacterController controller;
    7.     Animator anim;
    8.     public float h ;
    9.     public float v ;
    10.     public float speed ;
    11.     public float runSpeed ;
    12.     public float walkSpeed ;
    13.     public float jumpSpeed ;
    14.     public float gravity ;
    15.     public float minAngle = 1; // min angle to detect rotation since last frame
    16.     private Vector3 oldFwd ;
    17.     private GameObject mainCamera ;
    18.     private Vector3 moveDirection = Vector3.zero;
    19.     public float maxSpeed ;
    20.     private float direction ;
    21.     private float strafing ;
    22.     private float lockRotation = 0.0f;
    23.     float horizontal;
    24.     float vertical;
    25.     private bool jump;
    26.    
    27.     // Use this for initialization
    28.     void Start () {
    29.         anim = GetComponent<Animator>();
    30.         controller = GetComponent<CharacterController> ();
    31.         mainCamera = Camera.main.gameObject;
    32.  
    33.         mainCamera.GetComponent<SmoothFollow>().enabled = false ;
    34.     }
    35.    
    36.     // Update is called once per frame
    37.     void Update () {
    38.                 // Is the controller on the ground?
    39.         if (controller.isGrounded)
    40.         {
    41.             jump = false;
    42.             // Assigning Translation Controls
    43.             moveDirection = new Vector3(Input.GetAxis("HorizontalMove") , 0, Input.GetAxis("VerticalMove"));
    44.  
    45.             // Align forward vector of movement and camera. Turn it off when a camera isn't assigned.
    46.             moveDirection = Camera.main.transform.rotation * moveDirection;
    47.  
    48.             // Rotation parameters based on stick direction
    49.             if(moveDirection.sqrMagnitude > 0.01f) {
    50.                 transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.LookRotation(moveDirection), Time.deltaTime * 5);
    51.             }
    52.  
    53.             // Calculate controller x and y = no slowing down while running curves
    54.             if(Mathf.Abs(Input.GetAxis("HorizontalMove")) > 0.01f || Mathf.Abs(Input.GetAxis("VerticalMove")) > 0.01f){
    55.                 if(speed < maxSpeed ){
    56.                     speed = (Mathf.Abs(Input.GetAxis("HorizontalMove")) + Mathf.Abs(Input.GetAxis("VerticalMove"))) ;
    57.                 } else {
    58.                     speed = maxSpeed ;
    59.                 }
    60.             }else{
    61.                 if(speed > 0.1f)speed -= 2f * Time.deltaTime;
    62.             }
    63.  
    64.             if(anim.GetBool ("Weapon") == true){
    65.                 transform.rotation = mainCamera.transform.rotation ;
    66.             }
    67.  
    68.             // Check rotation
    69.             Vector3 curForw = transform.forward ;
    70.             Vector3 movForw = moveDirection ;
    71.             Vector3 directionVect = Vector3.Cross(curForw, movForw);
    72.             direction = directionVect.y ;
    73.  
    74.             CheckAnimation ();
    75.         }
    76.         // Applying gravity variable to the character controller when jumping
    77.         moveDirection.y -= gravity * Time.deltaTime;
    78.  
    79.         // Applying move direction to the character controller
    80.         controller.Move (moveDirection * speed * Time.deltaTime);
    81.     }
    82.  
    83.     void LateUpdate(){
    84.         // Lock rotations x and z to stop tilting.
    85.         controller.transform.rotation = Quaternion.Euler(lockRotation, transform.rotation.eulerAngles.y, lockRotation);
    86.     }
    87.     void CheckAnimation(){
    88.         if (Input.GetAxis ("HorizontalMove") > 0.01f || Input.GetAxis ("VerticalMove") > 0.01f
    89.             || Input.GetAxis ("HorizontalMove") < -0.01f || Input.GetAxis ("VerticalMove") < -0.01f) {
    90.             maxSpeed = runSpeed;
    91.             anim.SetBool("RunCheck", true);
    92.         } else {
    93.             maxSpeed = 0 ;
    94.             anim.SetBool("RunCheck", false);
    95.         }
    96.  
    97.         //Roll
    98.         if (Input.GetButtonUp("Jump"))
    99.         {
    100.             //moveDirection.y = jumpSpeed;
    101.             moveDirection.z = jumpSpeed ;
    102.             anim.SetBool ("jumpHash", true);
    103.             jump = true;
    104.         } else {
    105.             jump = false ;
    106.             anim.SetBool ("jumpHash", false);
    107.         }
    108.                
    109.         if (Input.GetKeyUp (KeyCode.E)) {
    110.             anim.SetTrigger ("pUpLowHash");
    111.         }
    112.         if (Input.GetKeyUp (KeyCode.R)) {
    113.             anim.SetTrigger ("pUpHighHash");
    114.         }
    115.  
    116.         if (Input.GetAxis ("Weapon") < 0) {
    117.             anim.SetBool ("Weapon", true);
    118.             SetAnimation(speed, 0, direction);
    119.         }
    120.         if (Input.GetAxis ("Weapon") > 0) {
    121.             anim.SetBool ("Weapon", false);
    122.             SetAnimation(speed, direction, 0);
    123.         }
    124.     }
    125.  
    126.     void SetAnimation(float spd, float drctin, float strf){
    127.         anim.SetFloat ("Speed", spd);
    128.         anim.SetFloat ("Direction", drctin);
    129.         anim.SetFloat ("Strafe", strf);
    130.     }
    131. }
    132.  
    I hope you guys can help me somehow :) Thanks in advance!
     
  2. WheresMommy

    WheresMommy

    Joined:
    Oct 4, 2012
    Posts:
    890
    Nothing over here? Damn, did not think, this is such a big issue :D
     
  3. WheresMommy

    WheresMommy

    Joined:
    Oct 4, 2012
    Posts:
    890
    Is there anything, you need, to answer this question? Did I miss something or is it just so easy? :D