Search Unity

ETeeskiTutorials FPS1.8 Basic Gun With Weight - Help needed

Discussion in 'Scripting' started by venhip, Jul 20, 2012.

  1. venhip

    venhip

    Joined:
    Jul 7, 2012
    Posts:
    6
    Hey, I've been following ETeeski's tutorials but trying to adapt them to my own game as and when I need them. At the moment I'm trying to add a torch to my game (my first item), trying to give it some weight and make it follow the camera, but although it rotates with the camera it doesn't move with it, though I suspect this is because I don't have the same MouseLookScript. This is the code I'm using:

    Code (csharp):
    1. var cameraObject : GameObject;
    2.  
    3. @HideInInspector
    4. var targetXRotation : float;
    5. @HideInInspector
    6. var targetYRotation : float;
    7. @HideInInspector
    8. var targetXRotationV : float;
    9. @HideInInspector
    10. var targetYRotationV : float;
    11.  
    12. @HideInInspector
    13. var yRotation : float; //I put this in here because its usually in the MouseLookScript, which i dont have
    14. @HideInInspector
    15. var xRotation : float; //I put this in here because its usually in the MouseLookScript, which i dont have
    16.  
    17.  
    18.  
    19. var rotationSpeed :float;
    20.  
    21. var holdHeight : float;
    22.  
    23. var holdSide : float;
    24.  
    25. function Update(){
    26.  
    27. yRotation += Input.GetAxis("Mouse X"); //Here is what is in the MouseLookScript to change the Y rotation, slightly edited
    28.  
    29. xRotation -= Input.GetAxis("Mouse Y"); //Here is what is in the MouseLookScript to change the X rotation, slightly edited
    30.  
    31.  
    32. transform.position = cameraObject.transform.position + (Quaternion.Euler(0,targetYRotation,0) * Vector3(holdSide,holdHeight,0));
    33.  
    34. targetXRotation = Mathf.SmoothDamp(targetXRotation,xRotation,targetXRotationV, rotationSpeed);
    35. targetXRotation = Mathf.SmoothDamp(targetYRotation,yRotation,targetYRotationV, rotationSpeed);
    36.  
    37. transform.rotation = Quaternion.Euler(targetXRotation,targetYRotation,0);
    38. }

    Thanks.
     
    Last edited: Jul 20, 2012