Public Group

# Having trouble with quaternions in Unity

This topic is 2569 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Edit: NEVERMIND! Buddy helped me figure it out. Solution came from the fact that Unity defaults transforms to quaternions. I was thinking of things as euler rotations since that's what we think of as default in average everyday life. Whew, there's just so much random crap to learn and ways to view online documentation to hunt down the small little issues. Heads still spinning.

Thanks in advance for any help!

This is my first go at a project and I used a tutorial to get mouselook to work on a cannon. I wanted to add nuances to the movement by changing how rotation works. Right now I want to know how much the cannon wants to rotate, then rotate it that much on the 2d plane, a vector3.forward however somewhere along the line it just doesn't rotate how it should, been staring at my first few lines of self made code for hours now reading over documentation trying to figure out why this isn't working.

Here's the unworking code:
====================================
 using UnityEngine; using System.Collections; public class WeaponFollow : MonoBehaviour { private float rotationModifier; private float mouseDif; private float hitdist; private Ray ray; private Plane playerPlane; private Vector3 targetPoint; private Quaternion targetRotation; public GUIText text; // Use this for initialization void Start () { } // Update is called once per frame void Update () { playerPlane = new Plane(Vector3.forward, transform.position); ray = Camera.main.ScreenPointToRay (Input.mousePosition); hitdist = 0.0f; if (playerPlane.Raycast (ray, out hitdist)) { targetPoint = ray.GetPoint(hitdist); targetRotation = Quaternion.LookRotation(targetPoint - transform.position); mouseDif = Quaternion.Angle(transform.rotation, targetRotation); transform.localRotation = Quaternion.AngleAxis(mouseDif,Vector3.forward); //transform.rotation = targetRotation; Debug.Log (mouseDif); } } } 

Edit: Did some tests and it looks like Quaternion.AngleAxis doesn't incriment on a local axis and simply sets the current rotation which could explain things.

1. 1
2. 2
3. 3
4. 4
Rutin
13
5. 5

• 15
• 10
• 9
• 9
• 11
• ### Forum Statistics

• Total Topics
633693
• Total Posts
3013358
×