Search Unity

Player moves forward when switching camera view?

Discussion in 'Scripting' started by StephCat, Jan 27, 2020.

  1. StephCat

    StephCat

    Joined:
    Nov 17, 2016
    Posts:
    20
    I'm relatively new to coding and unity, so apologies if this is a newbie question...
    I'm trying to make a character controller that can switch between first and third person view. The controls for each are slightly different: First person always faces the camera direction and strafes left/right and walks backwards with wasd, but in third person the character can move in directions independently from camera direction. The issue is that when a camera switch is triggered the player object moves forward by a fraction of a unit. If I don't press any directional keys but just trigger the camera switch I can move them across the map, hilariously. I don't know what's causing this, and have included my cam switch and my character controller below.

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class CamSwitch : MonoBehaviour
    4. {
    5.     public Camera FPCam;
    6.     public Camera TPCam;
    7.  
    8.     public bool fpCam = true;
    9.  
    10.     // Start is called before the first frame update
    11.     void Start()
    12.     {
    13.         FPCam.enabled = fpCam;
    14.         TPCam.enabled = !fpCam;
    15.     }
    16.  
    17.     // Update is called once per frame
    18.     void Update()
    19.     {
    20.         if(Input.GetKeyDown(KeyCode.C))
    21.         {
    22.             fpCam = !fpCam;
    23.             FPCam.enabled = fpCam;
    24.             TPCam.enabled = !fpCam;
    25.         }
    26.     }
    27. }
    28.  
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class PlayerMovement : MonoBehaviour
    4. {
    5.     public CharacterController controller;
    6.     public Camera FPcam, TPcam;
    7.     Transform camT;
    8.  
    9.     public float walkSpeed = 4f;
    10.     public float runSpeed = 8f;
    11.     public float gravity = -20f;
    12.     public float jumpHeight = 2f;
    13.  
    14.     public Transform groundCheck;
    15.     public float groundDistance = 0.4f;
    16.     public LayerMask groundMask;
    17.  
    18.     Vector3 velocity;
    19.     bool isGrounded;
    20.    
    21.     //useful for animation transitions
    22.     public float speedSmoothTime = 0.1f;
    23.     float speedSmoothVelocity;
    24.     float currentSpeed;
    25.  
    26.     //smooths character turning when using camera or wasd
    27.     public float turnSmoothTime = 0.2f;
    28.     float turnSmoothVelocity;
    29.  
    30.     void Start()
    31.     {
    32.         camT = TPcam.transform;
    33.     }
    34.     // Update is called once per frame
    35.     void Update()
    36.     {
    37.         GroundCheck();
    38.  
    39.         //looks for wasd key input to move
    40.         float x = Input.GetAxis("Horizontal");
    41.         float z = Input.GetAxis("Vertical");
    42.  
    43.         if (FPcam.isActiveAndEnabled) //if in first person view, use these controls
    44.         {
    45.             FirstPersonControls(x, z);              
    46.         }
    47.         else //if (TPcam.isActiveAndEnabled)
    48.         {
    49.             ThirdPersonControls(x, z);
    50.         }
    51.  
    52.         if (Input.GetButtonDown("Jump"))
    53.         {
    54.             Jump();
    55.         }
    56.  
    57.         CalcGravity();
    58.     }
    59.  
    60.     void FirstPersonControls(float x, float z)
    61.     {
    62.         //if the character is running, change speed and smooth movement
    63.         bool running = Input.GetKey(KeyCode.LeftShift);
    64.         float targetSpeed = ((running) ? runSpeed : walkSpeed);
    65.         currentSpeed = Mathf.SmoothDamp(currentSpeed, targetSpeed, ref speedSmoothVelocity, speedSmoothTime);
    66.  
    67.         //moves player controller based on above inputs
    68.         Vector3 move = transform.right * x + transform.forward * z;
    69.         controller.Move(move * currentSpeed * Time.deltaTime);
    70.     }
    71.  
    72.     void ThirdPersonControls(float x, float z)
    73.     {
    74.         //prevents camera direction from being reset every time rotation stops
    75.         Vector2 input = new Vector2(x, z);
    76.         Vector2 inputDir = input.normalized;
    77.  
    78.         if (inputDir != Vector2.zero)
    79.         {
    80.             //will move player in the direction that the camera is facing
    81.             float targetRotation = Mathf.Atan2(x, z) * Mathf.Rad2Deg + camT.eulerAngles.y;
    82.             transform.eulerAngles = Vector3.up * Mathf.SmoothDampAngle(transform.eulerAngles.y, targetRotation, ref turnSmoothVelocity, turnSmoothTime);
    83.         }
    84.  
    85.         //if the character is running, change speed and smooth movement
    86.         bool running = Input.GetKey(KeyCode.LeftShift);
    87.         //multiplying by inputDir.magnitude prevents movement when not actively using wasd
    88.         float targetSpeed = ((running) ? runSpeed : walkSpeed) * inputDir.magnitude;
    89.         currentSpeed = Mathf.SmoothDamp(currentSpeed, targetSpeed, ref speedSmoothVelocity, speedSmoothTime);
    90.  
    91.         controller.Move(transform.forward * currentSpeed * Time.deltaTime);
    92.     }
    93.  
    94.     void GroundCheck()
    95.     {
    96.         //checks if the groundcheck object at the players feet is touching an object with the layer "ground", will set bool accordingly
    97.         isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
    98.         if (isGrounded && velocity.y < 0) //makes sure player sticks to the ground
    99.         {
    100.             velocity.y = -2f;
    101.         }
    102.     }
    103.  
    104.     void Jump()
    105.     {
    106.         if (isGrounded)
    107.         {
    108.             Debug.Log("Jumping");
    109.             velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
    110.         }
    111.  
    112.     }
    113.  
    114.     void CalcGravity()
    115.     {
    116.         //gravity calculations
    117.         velocity.y += gravity * Time.deltaTime;
    118.         controller.Move(velocity * Time.deltaTime);
    119.     }
    120. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,752
    It might be floating point error, perhaps caused by setting the .eulerAngles every frame (line 82), then the new position/rotation offset triggers your physics and you creep a bit?
     
    StephCat likes this.