Search Unity

  1. Unity 2019.1 is now released.
    Dismiss Notice

making the player always face the camera direction while strafing/moving back

Discussion in 'Scripting' started by twn9009, May 23, 2019.

  1. twn9009

    twn9009

    Joined:
    May 2, 2019
    Posts:
    28
    i have a script that i've taken from a tutorial and modified it for my purposes, the character always looks at the camera when hes walking forwards however when you try to move backwards or sideways he faces that direction, how can i make it so he strafes and walks backwards while facing the camera direction as when i am trying to attack and walk back/sideways he is facing away from the target.
    Code (CSharp):
    1.  void Update()
    2.     {
    3.  
    4.  
    5.         //movement jumping and turning
    6.         Vector2 input = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
    7.         Vector2 inputDir = input.normalized;
    8.  
    9.         if (Input.GetKeyDown(KeyCode.Space) | Input.GetAxisRaw("Jump") > 0)
    10.         {
    11.             Jump();
    12.         }
    13.         if (inputDir != Vector2.zero)
    14.         {
    15.             float targetRotation = Mathf.Atan2(inputDir.x, inputDir.y) * Mathf.Rad2Deg + cameraT.eulerAngles.y;
    16.             transform.eulerAngles = Vector3.up * Mathf.SmoothDampAngle(transform.eulerAngles.y, targetRotation, ref turnSmoothVelocity, turnSmoothTime);
    17.         }
    18.         if (SBar.CurrentStamValue > 1)
    19.         {
    20.             bool running = Input.GetKey(KeyCode.LeftShift);
    21.             float speed = ((running) ? runSpeed : walkSpeed) * inputDir.magnitude;
    22.             Vector3 velocity = transform.forward * speed + Vector3.up * velocityY;
    23.  
    24.             velocityY += Time.deltaTime * gravity;
    25.  
    26.             controller.Move(velocity * Time.deltaTime);
    27.             speed = new Vector2(controller.velocity.x, controller.velocity.z).magnitude;
    28.  
    29.  
    30.  
    31.  
    32.             if (controller.isGrounded)
    33.             {
    34.                 velocityY = 0;
    35.             }
    36.  
    37.             float animationSpeedPercent = ((running) ? speed / runSpeed : speed / walkSpeed * .5f);
    38.             anim.SetFloat("SpeedPercent", animationSpeedPercent);
    39.         }
    40.         else if(SBar.CurrentStamValue <= 1 & inputDir != Vector2.zero)
    41.         {
    42.             bool walking = !Input.GetKey(KeyCode.LeftShift) & inputDir != Vector2.zero;
    43.             float targetRotation = Mathf.Atan2(inputDir.x, inputDir.y) * Mathf.Rad2Deg + cameraT.eulerAngles.y;
    44.             transform.eulerAngles = Vector3.up * Mathf.SmoothDampAngle(transform.eulerAngles.y, targetRotation, ref turnSmoothVelocity, turnSmoothTime);
    45.             anim.SetFloat("SpeedPercent", 0.5f);
    46.             float speed = ((walking) ? walkSpeed : walkSpeed) * inputDir.magnitude;
    47.             Vector3 velocity = transform.forward * speed + Vector3.up * velocityY;
    48.        
    49.             controller.Move(velocity * Time.deltaTime);
    50.             if(inputDir.x == 0 & inputDir.y == 0)
    51.             {
    52.                 anim.SetFloat("SpeedPercent", 0);
    53.  
    54.             }
    55.         }
    56.          }
    if you need any more info please ask and thanks for taking the time to read this
     
    Last edited: May 23, 2019