Search Unity

First Person Weapons

Discussion in 'Scripting' started by deram_scholzara, Aug 27, 2005.

  1. deram_scholzara

    deram_scholzara

    Joined:
    Aug 26, 2005
    Posts:
    1,026
    I want to have an in-hand weapon object linked to the player's camera in such a way that it will rotate with the camera. I built a basic FPS player as is detailed in the mouselook script, positioned the weapon, and put the weapon into the camera object in the hierarchy, but there's a problem...the weapon not only rotates, but moves as well. When you look up, the weapon moves and rotates up and the same for down...the weapon should only rotate with the camera, but that is not the result that I'm getting.

    Can anybody help explain this and how I can fix it?
    Thanks.
     
  2. Richard_B

    Richard_B

    Joined:
    Jul 22, 2005
    Posts:
    436
    It would seem that what you really want is not to parent the weapon to the camera, but rather to have its rotation dynamically track that of the camera. If that is the case, then parent the weapon to the same object the camera is parented to, make sure the pivot point of the weapon is positioned at the point about which you want the weapon to rotate. Then, attach something like the following script to the weapon:

    Code (csharp):
    1.  
    2. var cam
    3.  
    4. function Update(){
    5. transform.rotation = cam.transform.rotation
    6. }
    7.  
    then drag the camera on to the cam var in the above script.

    Richard.
     
unityunity