Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

FPS Character Spinning out of control when I add a long capsule for a spear

Discussion in 'Editor & General Support' started by g-hoot, Jul 4, 2016.

  1. g-hoot

    g-hoot

    Joined:
    Apr 18, 2015
    Posts:
    69
    I followed a simple tutorial to make an FPS character. The character is just a capsule and it all works fine. Then I added another capsule and made it very long and skinny, for a spear for my character. Now, I can look around a little, then it goes crazy. The rigidbody on my character is set to only rotate in the Y axis, but that doesn't stop it. Here's a video of the behavior:


    If I comment the very last line of code out, then the character doesn't do that...of course he doesn't rotate to the sides like he should either. This is the code for the mouse look.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class camMouseLook : MonoBehaviour
    5. {
    6.  
    7.     Vector2 mouseLook;
    8.     Vector2 smoothV;
    9.     public float sensitivity = 5.0f;
    10.     public float smoothing = 2.0f;
    11.     GameObject charachter;
    12.  
    13.     // Use this for initialization
    14.     void Start ()
    15.     {
    16.         charachter = this.transform.parent.gameObject;
    17.     }
    18.  
    19.     // Update is called once per frame
    20.     void Update ()
    21.     {
    22.         var md = new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y"));
    23.  
    24.         md = Vector2.Scale(md, new Vector2(sensitivity * smoothing, sensitivity * smoothing));
    25.         smoothV.x = Mathf.Lerp(smoothV.x, md.x, 1f / smoothing);
    26.         smoothV.y = Mathf.Lerp(smoothV.y, md.y, 1f / smoothing);
    27.         mouseLook += smoothV;
    28.  
    29.         transform.localRotation = Quaternion.AngleAxis(-mouseLook.y, Vector3.right);
    30.         charachter.transform.localRotation = Quaternion.AngleAxis(mouseLook.x, charachter.transform.up);
    31.  
    32.     }
    33. }
    Any ideas? THANKS!
     
  2. Phixle

    Phixle

    Joined:
    Dec 16, 2015
    Posts:
    1
    Hello, I actually follow the same exact tutorial by the look of the code and also got the same type of spinning.
    On the last line in AngleAxis you are using (charachter.transform.up). Its using the local transform.up so if you have locked all rotation except Y on the rigidbody component and the character somehow leans it will mess up everything. If you use (Vector3.up) instead it will work just fine.
     
  3. g-hoot

    g-hoot

    Joined:
    Apr 18, 2015
    Posts:
    69
    Thank you! I will check it out.