Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to modify a Quaternion

Discussion in 'Scripting' started by RichardsonKevin, Apr 17, 2014.

  1. RichardsonKevin

    RichardsonKevin

    Joined:
    Feb 3, 2013
    Posts:
    43
    Hello everyone! I am having trouble making my soldier point his weapon directly at the enemy. I have attached a script to the spine joint of my soldier because that is naturally where he will be rotating from. The problem that I have is that when he is in the aiming pose without any rotations added, he is standing slightly sideways, with the spine joint not rotated directly forward, so when I apply this script it points the affected spine joint directly at the target, causing the rest up his upper body to skew up and to the left. Here is the script I am placing on his spine joint.

    using UnityEngine;
    using System.Collections;

    public class TorsoLookAt : MonoBehaviour
    {
    public GameObject target;

    void LateUpdate()
    {

    if(target != null)
    {
    Vector3 dir = target.transform.position - transform.position;
    Quaternion rotation = Quaternion.LookRotation(dir);
    transform.rotation = rotation;
    }
    }

    }

    Is there a way I can modify the angles of the quaternion to offset the difference in my characters stance so his gun points strait at the target instead of the spine joint itself?
     
  2. RichardsonKevin

    RichardsonKevin

    Joined:
    Feb 3, 2013
    Posts:
    43
    Ahh Euler angles... nevermind
     
  3. RichardsonKevin

    RichardsonKevin

    Joined:
    Feb 3, 2013
    Posts:
    43
    Well now I have to change the euler angle offset floats in my script for almost every different animation state... is there an easier way to handle this?