• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Fox-Handler · Sep 06, 2014 at 01:56 AM · quaterniondirection

Quaternion multiplication

I was studying this script from an example project and after comparing it to another tutorial, did some research on Quaternion multiplication. Now I am by no means a math whiz (only learning what I need to when necessary), But what I found interesting was this line right here

 direction.Normalize ();

I was reading this post http://answers.unity3d.com/questions/372371/multiply-quaternion-by-vector3-how-is-done.html and from what I could gather, Multiplying a quaternion by a vector3 or (Vector2) gives the same rotation. Because I thought the Horizontal and Vertical Input both have a magnitude of 1 anyways is using Vector3.normalize necessary?

 using UnityEngine;
 using System.Collections;
 
 /// <summary>
 /// Utilities to convert Joystiq input to worldspace ( based in main camera) 
 /// and to convert worldspace to Speed and Direction
 /// </summary>
 public class JoystickToWorld
 {    
     public static Vector3 ConvertJoystickToWorldSpace ()
     {        
             Vector3 direction;            
             float horizontal = Input.GetAxis ("Horizontal");
             float vertical = Input.GetAxis ("Vertical");        
             Vector3 stickDirection = new Vector3 (horizontal, 0, vertical);                                        
             direction = Camera.main.transform.rotation * stickDirection; // Converts joystick input in Worldspace coordinates //Quaterntion multiplication
             direction.y = 0; // Kill Z
             //direction.Normalize ();    <-------------
 
             return direction;
     }
 
     public static void ComputeSpeedDirection (Transform root, ref float speed, ref float direction)
     {
             Vector3 worldDirection = ConvertJoystickToWorldSpace ();
 
             speed = Mathf.Clamp (worldDirection.magnitude, 0, 1);
             if (speed > 0.01f) { // dead zone
                     Vector3 axis = Vector3.Cross (root.forward, worldDirection);
                     direction = Vector3.Angle (root.forward, worldDirection) / 180.0f * (axis.y < 0 ? -1 : 1);
             } else {
                     direction = 0.0f; 
             }
     }    
 } 

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by robertbu · Sep 06, 2014 at 02:18 AM

is using Vector3.normalize necessary?

Yes. Take a look at these three possible vectors from your code above: (1,0,0), (0,0,1), and (1,0,1). The magnitude of the first two is 1.0, but the magnitude of the last is sqrt(2). Without the Normalize(), your character would move faster when going in a diagonal direction. But you are right in thinking that the Quaternon multiplication does not change the magnitude of the vector.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Fox-Handler · Sep 06, 2014 at 02:50 AM 0
Share

right that makes sense. I got a little confused because in the other script the author doesn't normalize the direction Vector. He pretty much does the same thing except that. He does use vector3.sqrmagnitude for the speed though.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Rotation direction in coroutine 2 Answers

3D nested Turret Prefab Rotation 1 Answer

How to calculate order of directions relative to world forward around point? 1 Answer

Rotate vector3 relative to another vector3 1 Answer

Relative rotation offset? 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges