Search Unity

Question Weapon Sway with FPS arms

Discussion in 'Scripting' started by Mr_Packer12, Apr 21, 2023.

  1. Mr_Packer12

    Mr_Packer12

    Joined:
    Aug 11, 2022
    Posts:
    30
    I am looking to add weapon sway to my Set up. I watched a couple of tutorials and found a simple one that works. Code below:

    Code (CSharp):
    1. {
    2.     public float smooth;
    3.     public float swayAmount;
    4.  
    5.     // Update is called once per frame
    6.     void Update()
    7.     {
    8.        float mouseX = -Input.GetAxisRaw("Mouse X") * swayAmount;
    9.        float mouseY = Input.GetAxisRaw("Mouse Y") * swayAmount;
    10.  
    11.        Quaternion rotationX = Quaternion.AngleAxis(mouseY, Vector3.right);
    12.        Quaternion rotationY = Quaternion.AngleAxis(-mouseX, Vector3.up);
    13.  
    14.        Quaternion targetRotation = rotationX * rotationY;
    15.  
    16.        transform.localRotation = Quaternion.Slerp(transform.localRotation, targetRotation, smooth * Time.deltaTime);
    17.     }
    The thing is, whenever attached to anything except the gun, the object gets turned around. I currently have it attached to just the guns, and it looks good. However my arms do not move with the sway. I tried adding it to the arms, and it forces the arms to do a 180 (facing behind me). I attached a screen shot of the hierarchy that shows the order of where everything is stored. Untitled design (62).jpg
    Right now I am just testing this out on the AKM. When picking up the weapon, it becomes a child object of the AKMHoldLocation. I think that it has something to do with the fact that I cannot get the arms to face the same direction as the camera unless I rotate it 180 Degrees. so it starts with that rotation on it. Would love feedback on A.) if there is a better way to set the hierarchy up, and B.) what is causing the arms to turn 180 when the script is activated(turns on when weapon is picked up). I am still shaky with camera rotation and things related, so anything helps. Thank you. Mouse Look Script Below:

    Code (CSharp):
    1. {
    2.     public float mouseSensitivity = 100f;
    3.  
    4.     public Transform playerBody;
    5.  
    6.     float xRotation = 0f;
    7.  
    8.     // Start is called before the first frame update
    9.     void Start()
    10.     {
    11.         Cursor.lockState = CursorLockMode.Locked;
    12.    
    13.     }
    14.  
    15.     // Update is called once per frame
    16.     void Update()
    17.     {
    18.        
    19.        
    20.             float mouseX = Input.GetAxis ("Mouse X") * mouseSensitivity * Time.deltaTime;
    21.         float mouseY = Input.GetAxis ("Mouse Y") * mouseSensitivity * Time.deltaTime;
    22.  
    23.         xRotation -= mouseY;
    24.         xRotation = Mathf.Clamp(xRotation, -90f, 90f);
    25.  
    26.         transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
    27.  
    28.  
    29.         playerBody.Rotate(Vector3.up * mouseX);
    30.  
    31.     }
     
  2. AshwinTheGammer

    AshwinTheGammer

    Joined:
    Sep 23, 2016
    Posts:
    69
    Try attaching it to the parent of the gun