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.

Resolved Weapon Sway Script Keeps Making Gun Face Backwards

Discussion in 'Scripting' started by Slyrae, May 9, 2022.

  1. Slyrae

    Slyrae

    Joined:
    Aug 28, 2017
    Posts:
    4
    Resolved: Weapon model was made with -Z forward, where the rotation I was trying to apply assumed +Z forward.

    Original Post:
    Hello there,

    For the life of me, I can't seem to figure out why exactly my weapon sway script works exactly as I want it to with one issue; the weapon faces the backward. I have tinkered around with the script quite a bit to try to force it to face forward but it just insists on resisting. Quaternions are still something I'm in a learning phase with. Could somebody please help me out?

    First, here's a couple images of the problem.
    upload_2022-5-8_20-47-51.png

    upload_2022-5-8_20-49-26.png

    WeaponSwayHandler.cs
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using CMF;
    5.  
    6. public class WeaponSwayHandler : MonoBehaviour
    7. {
    8.     [SerializeField] AdvancedWalkerController controller;
    9.     [SerializeField] private GameObject weapon;
    10.     [SerializeField] private float smooth = 8f;
    11.     [SerializeField] private Vector2 swayMultiplier = new Vector2(4f,4f);
    12.     [SerializeField] private bool invertSway = true;
    13.  
    14.     public bool enableSway = true;
    15.  
    16.     // Update is called once per frame
    17.     void Update()
    18.     {
    19.         if (!enableSway) return;
    20.         Rigidbody rb = controller.gameObject.GetComponent<Rigidbody>();
    21.  
    22.         // Get Mouse Input.
    23.         float mouseX = Input.GetAxisRaw("Mouse X") * swayMultiplier.x;
    24.         float mouseY = Input.GetAxisRaw("Mouse Y") * swayMultiplier.y;
    25.  
    26.         // Calculate Target Rotation.
    27.         Quaternion rotationX = Quaternion.AngleAxis(-mouseY - rb.velocity.y, Vector3.right);
    28.         Quaternion rotationY = Quaternion.AngleAxis(mouseX, Vector3.up);
    29.  
    30.         Quaternion targetRotation = rotationX * rotationY;
    31.  
    32.         // Rotate.
    33.         if (invertSway) targetRotation = Quaternion.Inverse(targetRotation);;
    34.  
    35.         weapon.transform.localRotation = Quaternion.Slerp(weapon.transform.localRotation, targetRotation, smooth * Time.deltaTime);        //weapon.transform.
    36.     }
    37. }
    38.  

    What I've tried so far is to invert the z and y rotations (in different attempts to understand the problem), as well as inverting the targetRotation quaternion altogether at the end of the script. I understand these are probably not the right ways to approach the problem, and I apologize as this is still a fairly new area I'm exploring.

    I just want to understand what's going on here, and get it fixed. Or reworked to be better at it's job. Thanks in advance.

    Project is using Universal Render Pipeline and Unity 2021.2.7f1.
    If you have any suggestions on how I can improve my posts, please let me know!
     

    Attached Files:

    Last edited: May 10, 2022
  2. Peeling

    Peeling

    Joined:
    Nov 10, 2013
    Posts:
    319
    If you set weapon.transform.localRotation = Quaternion.identity, which way does the gun face?
     
    Kurt-Dekker likes this.
  3. Slyrae

    Slyrae

    Joined:
    Aug 28, 2017
    Posts:
    4
    It still faces backwards. Would that suggest that the model itself is oriented incorrectly?

    Edit: This actually seems to be the case. I rotated the weapon 180 degrees, set the scale of Z to -1, and it does face the correct way now. This isn't a fix but I think it highlights the issue. Sometimes the simplest things manage to elude me. Thanks!
     
    Last edited: May 10, 2022
unityunity