Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

gun does not follow the camera properrly

Discussion in 'Scripting' started by abdelrahmanyoussry509, Apr 19, 2020.

  1. abdelrahmanyoussry509

    abdelrahmanyoussry509

    Joined:
    Jan 26, 2020
    Posts:
    14
    my gun does not follow gun properly it rotates like if there is an arc there around it (sorry i can not explain it very well i am new) here is code for the looking around
    public float MouseSenstivity;
    public Transform PlayerBody;
    private float rotationY;
    public Transform weapon;
    void Start()
    {
    Cursor.lockState = CursorLockMode.Locked;
    }
    // Update is called once per frame
    void Update()
    {
    float MouseX = Input.GetAxis("Mouse X") * MouseSenstivity * Time.deltaTime;
    float MouseY = Input.GetAxis("Mouse Y") * MouseSenstivity * Time.deltaTime;
    PlayerBody.Rotate(Vector3.up * MouseX);

    rotationY = rotationY - MouseY;
    rotationY = Mathf.Clamp(rotationY, -90f, 90f);

    transform.localRotation = Quaternion.Euler(rotationY, 0f, 0f);
    weapon.localRotation = Quaternion.Euler(rotationY, 0f, 0f);

    }
    }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    Please check the first post in the forum for how to format your code.

    That said, the last two lines of code are suspicious in that you're setting two separate objects to the exactly same .localRotation. Not only that but you are supplying the variable rotationY into the x input of the Euler() helper. And it also appears you are not doing anything with the X values, which would make it a rather limited weapon.
     
    abdelrahmanyoussry509 likes this.
  3. Leonetienne500

    Leonetienne500

    Joined:
    Dec 5, 2016
    Posts:
    130
    1) Please format your code!
    Code (CSharp):
    1. void DoShit()
    2. {
    3.   S***();
    4. }
    2) What the hell are you trying to achieve with this code?:D
    Just parent the gun to your camera, or better yet, use this kind of hirachy:

    Player (holds the script)
    MainCamera
    Gun
     
  4. abdelrahmanyoussry509

    abdelrahmanyoussry509

    Joined:
    Jan 26, 2020
    Posts:
    14
    I am new so I apologize for the mess i will formate any new questions i have

    Thank you i figured it out !