Jump to content
  • Advertisement
Sign in to follow this  

Having trouble with quaternions in Unity

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

If you intended to correct an error in the post then please contact us.

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.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!