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;
///
/// Utilities to convert Joystiq input to worldspace ( based in main camera)
/// and to convert worldspace to Speed and Direction
///
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;
}
}
}
↧