Search Unity

My first person camera, goes all over the place after enabling weapon collider

Discussion in 'Scripting' started by Sinci1, Jul 26, 2017.

  1. Sinci1

    Sinci1

    Joined:
    Feb 18, 2016
    Posts:
    53
    Hello, I have a first person script, and it work good, but however if i enable another collider, then it acts all crazy? also i have tried disabling the script of the first person camera, and then the crazy movement stops, but when i enable it again it goes again. Meaning that it not a collider issue. Here is a GIF of the problem And here is the script of the first person camera :
    Code (CSharp):
    1.    using System.Collections;
    2.      using System.Collections.Generic;
    3.      using UnityEngine;
    4.  
    5.      public class ScanningRobotTestCamera : MonoBehaviour {
    6.          //ThirdPersonCamStuff VVVVVVVVVVVVVVVVVVVVVVVVVVVV
    7.          public const float Y_AXIS_MIN = 0.00f;
    8.          public const float Y_AXIS_MAX = 50.00f;
    9.          [Space]
    10.          [Header("ThirdPlayerCamStuff")]
    11.      
    12.          public Transform LookAtTarget;
    13.          public Transform CameraTransform;
    14.      
    15.          private Camera cam;
    16.      
    17.          public float Speed;
    18.          public float distance;
    19.          public float currentX;
    20.          public float currentY;
    21.          public float sensetivityX;
    22.          public float sensetivityY;
    23.          public bool Once;
    24.          public bool SecondOnce;
    25.          //ThirdPersonCamStuff ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    26.          //FirstPersonCamStuff VVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
    27.          [Space]
    28.          [Header("FirstPlayerCamStuff")]
    29.          public float ClampFirstPersonMin;
    30.          public float ClampFirstPersonMax;
    31.          public GameObject Arms;
    32.          public GameObject RobotModel;
    33.          public GameObject PlayerMdel;
    34.      
    35.          public Vector2 MouseLook;
    36.          public Vector2 SmoothV;
    37.          public float sensitivity;
    38.          public float smoothing;
    39.          public Vector2 md;
    40.          public bool Escape;
    41.          public GameObject Character;
    42.          public GameObject NormalParent;
    43.          //FirstPersonCamStuff^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    44.      
    45.          [Space]
    46.          [Header("Other")]
    47.          public string ChangeCamsKey;
    48.          public int DiffrentCamsNumber;
    49.          public int NumberOfDiffrentCams;
    50.          public enum DiffrentCams{FirstPerson,ThirdPerson};
    51.          public DiffrentCams DiffCams;
    52.      
    53.          // Use this for initialization
    54.          void Start () {
    55.          
    56.              CameraTransform = transform;
    57.          }
    58.      
    59.      
    60.      
    61.      
    62.      
    63.      
    64.      
    65.      
    66.          void Update(){
    67.              if (Input.GetKeyDown(ChangeCamsKey)){
    68.                  if (DiffrentCamsNumber == NumberOfDiffrentCams){
    69.                      DiffrentCamsNumber = 0;
    70.                  }
    71.                  DiffrentCamsNumber += 1;
    72.                  if (DiffrentCamsNumber == 1){
    73.                      DiffCams = DiffrentCams.FirstPerson;
    74.                  }
    75.                  if (DiffrentCamsNumber == 2){
    76.                      DiffCams = DiffrentCams.ThirdPerson;
    77.                  }
    78.              }
    79.          
    80.          
    81.          
    82.          
    83.          
    84.              //FirstPersonCamStuff VVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
    85.              if(DiffCams == DiffrentCams.FirstPerson){
    86.                  Arms.transform.parent = transform;
    87.                  RobotModel.GetComponent<MeshRenderer>().shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.ShadowsOnly;
    88.                  if(!SecondOnce){
    89.                      //                transform.localEulerAngles = Vector3.zero;
    90.                      Arms.transform.localEulerAngles = new Vector3(0,0,Arms.transform.localEulerAngles.z);
    91.                      SecondOnce=true;
    92.                  }
    93.                  Once=false;
    94.                  gameObject.transform.parent = Character.transform;
    95.                  gameObject.transform.localPosition = new Vector3(0,2.6399991f,0);
    96.                  md = new Vector2(Input.GetAxisRaw("Mouse X"),Input.GetAxisRaw("Mouse Y"));
    97.                  print (md);
    98.                  md = Vector2.Scale(md,new Vector2(sensitivity * smoothing, sensitivity * smoothing));
    99.                  SmoothV.x = Mathf.Lerp (SmoothV.x, md.x, 1f / smoothing);
    100.                  SmoothV.y = Mathf.Lerp (SmoothV.y, md.y, 1f / smoothing);
    101.                  MouseLook += SmoothV;
    102.                  MouseLook.y = Mathf.Clamp (MouseLook.y, ClampFirstPersonMin,ClampFirstPersonMax);
    103.                  Arms.transform.localPosition = new Vector3(-1.85f,-2,1.31f);
    104.                  transform.localRotation = Quaternion.AngleAxis (-MouseLook.y, Vector3.right);
    105.                  Character.transform.localRotation = Quaternion.AngleAxis (MouseLook.x,Character.transform.up);
    106.              }//FirstPersonCamStuff^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    107.              //ThirdPersonCamStuff VVVVVVVVVVVVVVVVVVVVVVVVVVVV
    108.              if(DiffCams == DiffrentCams.ThirdPerson){
    109.                  Arms.transform.parent = PlayerMdel.transform;
    110.                  RobotModel.GetComponent<MeshRenderer>().shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.On;
    111.                  Arms.transform.localPosition = Vector3.zero;
    112.                  if(!Once){
    113.                      MouseLook.y = 0;
    114.                      Arms.transform.localEulerAngles = Vector3.zero;
    115.                      Once=true;
    116.                  }
    117.                  SecondOnce=false;
    118.                  gameObject.transform.parent = NormalParent.transform;
    119.              
    120.                  transform.localRotation = Quaternion.AngleAxis (-MouseLook.y, Vector3.right);
    121.                  //        Character.transform.localRotation = Quaternion.AngleAxis (MouseLook.x,Character.transform.up);
    122.                  if(Input.GetKey(KeyCode.Mouse1)){
    123.                      print(Vector3.back);
    124.                      currentY +=    Input.GetAxis("Mouse Y") * Speed;
    125.                      currentX +=    Input.GetAxis("Mouse X") * Speed;
    126.                      currentY = Mathf.Clamp (currentY, Y_AXIS_MIN, Y_AXIS_MAX);
    127.                  }}//ThirdPersonCamStuff ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    128.          }
    129.      
    130.          // Update is called once per frame
    131.          void LateUpdate () {
    132.              //ThirdPersonCamStuff VVVVVVVVVVVVVVVVVVVVVVVVVVVV
    133.              if (DiffCams == DiffrentCams.ThirdPerson) {
    134.                  Vector3 dir = new Vector3 (0, 0, -distance);
    135.                  Quaternion rotation = Quaternion.Euler (currentY, currentX, 0);
    136.                  CameraTransform.position = LookAtTarget.position + rotation * dir;
    137.                  CameraTransform.LookAt (LookAtTarget);
    138.              }//ThirdPersonCamStuff ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    139.          }
    140.      }
     
  2. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    You're not enabling/disabling the script in that GIF. You're toggling the Collider on the arms.

    Since you're controlling the arms' movement with the script, those colliders will push around any other rigidbodies they come into contact with, including that of the player controller itself.

    It is a collider issue.
     
  3. Sinci1

    Sinci1

    Joined:
    Feb 18, 2016
    Posts:
    53
    But i have different layers, also here is a GIF where i disable the script, and you will see that it stops spinning. Also when i disable the collider in the other GIF, it still spins.
     
  4. Sinci1

    Sinci1

    Joined:
    Feb 18, 2016
    Posts:
    53
    will anybody reply, im sorry but this is a really bad problem because i can't continue on my project...
     
  5. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    I agree with Schneider21. Also in your 2nd GIF, I don't see where the camera is spinning then stops when you disable the script as you claim. All I see is you enabling a collider. It is also so low res it is hard to see clearly what is happening at all.
     
  6. Sinci1

    Sinci1

    Joined:
    Feb 18, 2016
    Posts:
    53
    Oops wrong gif sorry, also sorry for bad quality but hopefully you will be able to see this GIF.
    Also if you notice in the first gif, the camera is moving not the arm, and the camera has no collider
     
  7. Habitablaba

    Habitablaba

    Joined:
    Aug 5, 2013
    Posts:
    136
    Your update is suspicious to me. It looks like you are doing a bunch of set up stuff, plus some lerping, and some position setting? I think you need to take a critical look at what you've got going on there.

    As a nit pick, you shouldn't set the transform parent the way you are doing it, you should use SetParent() instead.
     
  8. Sinci1

    Sinci1

    Joined:
    Feb 18, 2016
    Posts:
    53
    Habitblaba, okay i will refactor my code in the update section, and then i will check if it works.
     
  9. Habitablaba

    Habitablaba

    Joined:
    Aug 5, 2013
    Posts:
    136
    I'm also interested to know what happens if you comment out the mouse look code.
    My theory is that you're getting the mouse location while the mouse is off screen (clicking the check box), then constantly trying to look to the right, because that's where the mouse is.
    In order to do mouse look, you're going to want to center the mouse. Then, in update, check how far away from center it moved (and in which direction), then move the camera by an appropriate amount. Finally, re-center the mouse.
     
  10. Sinci1

    Sinci1

    Joined:
    Feb 18, 2016
    Posts:
    53
    Yes but the weird thing is after enabling collider it starts spinning and won't stop until i restart game or i disable the script(but it starts again if i enable it again)

    P.S: I havent refactored the code yet
     
  11. Habitablaba

    Habitablaba

    Joined:
    Aug 5, 2013
    Posts:
    136
    I think what we're going to find is that there are multiple things wrong here, so we're going to have to try a whole bunch of stuff.

    Edit:
    Try something for me. Do everything you do in the first gif you posted, except don't actually toggle the box collider. So start the scene, look around a bit, pause and switch to scene view.... then unpause and switch back to game view.
    What happens?
     
  12. Sinci1

    Sinci1

    Joined:
    Feb 18, 2016
    Posts:
    53
    Im so happy, i solved it, ok i will try to explain what is.
    So after doing a little bit of debugging i figured out the "Character.transform.localRotation = Quaternion.AngleAxis (MouseLook.x,Character.transform.up);" was where the problem was causing, so after printing ever variable, i figured out that after enabling the collider, the "Character.transform.up" was going from (0.0,1.0,0.0) to (*insert random number -1 to 1 here*,*insert random number -1 to 1 here*,*insert random number -1 to 1 here*), so i just replaced "Character.transform.up", to "Vector3.up", and now it works :D. Thank you Habitblaba, Joe-Censorned , and Schneider21.