Search Unity

What is causing my player gameobject to be shooting straight up into the sky? bugged move script.

Discussion in 'Scripting' started by unity_DWogdn9M-ulk_Q, Jan 18, 2022.

  1. unity_DWogdn9M-ulk_Q

    unity_DWogdn9M-ulk_Q

    Joined:
    May 29, 2021
    Posts:
    1
    This is the code to my movement script attached to my gameobject. I don't know what is causing my gameobject player to be shooting straight up into the sky. This is borrowed code modified, and I'm not sure what I did wrong here. Any suggestions?
    Code (CSharp):
    1.  
    2.     void Update()
    3.     {
    4.         yaw = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * mouseSensitivity;
    5.         pitch -= mouseSensitivity * Input.GetAxis("Mouse Y");
    6.        
    7.         pitch = Mathf.Clamp(pitch, -maxLookAngle, maxLookAngle);
    8.  
    9.         transform.localEulerAngles = new Vector3(0, yaw, 0);
    10.         playerCamera.transform.localEulerAngles = new Vector3(pitch, 0, 0);
    11.     }
    12.  
    13.     private void FixedUpdate()
    14.     {
    15.         Vector3 targetVelocity = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
    16.         targetVelocity = transform.TransformDirection(targetVelocity) * walkSpeed;
    17.  
    18.         // Apply a force that attempts to reach our target velocity
    19.         Vector3 velocity = rb.velocity;
    20.         Vector3 velocityChange = (targetVelocity - velocity);
    21.         velocityChange.x = Mathf.Clamp(velocityChange.x, -maxVelocityChange, maxVelocityChange);
    22.         velocityChange.z = Mathf.Clamp(velocityChange.z, -maxVelocityChange, maxVelocityChange);
    23.         velocityChange.y = 0;
    24.  
    25.         rb.AddForce(velocityChange, ForceMode.VelocityChange);
    26.         Debug.Log(velocityChange);
    27.     }
    28.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    Did you rig him funny like with the camera pointed straight up? Most of what I see above seems reasonable.

    One thought is if you are rotating a collider when you drive the localRotations directly... that could glitch the controller. For the root object you might want to instead call rigidbody.MoveRotation() (the one that only goes around the yaw).

    Alternately, take the addforce out and verify if it isn't that.

    You must find a way to get the information you need in order to reason about what the problem is.

    What is often happening in these cases is one of the following:

    - the code you think is executing is not actually executing at all
    - the code is executing far EARLIER or LATER than you think
    - the code is executing far LESS OFTEN than you think
    - the code is executing far MORE OFTEN than you think
    - the code is executing on another GameObject than you think it is
    - you're getting an error or warning and you haven't noticed it in the console window

    To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run? what order does it run in?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?
    - are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

    Knowing this information will help you reason about the behavior you are seeing.

    You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.

    Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494