xxxxxxxxxx
using UnityEngine;
public class VectorExample : MonoBehaviour
{
void Start()
{
// Creating a 3D vector
Vector3 myVector = new Vector3(1.0f, 2.0f, 3.0f);
// Accessing individual components of the vector
float xComponent = myVector.x;
float yComponent = myVector.y;
float zComponent = myVector.z;
// Modifying vector components
myVector.x = 0.5f;
myVector.y = 1.5f;
// Performing vector operations
Vector3 anotherVector = new Vector3(2.0f, 4.0f, 6.0f);
Vector3 sum = myVector + anotherVector;
Vector3 difference = myVector - anotherVector;
float dotProduct = Vector3.Dot(myVector, anotherVector);
Vector3 crossProduct = Vector3.Cross(myVector, anotherVector);
// Outputting vector information
Debug.Log("Vector: " + myVector);
Debug.Log("Sum: " + sum);
Debug.Log("Difference: " + difference);
Debug.Log("Dot Product: " + dotProduct);
Debug.Log("Cross Product: " + crossProduct);
}
}
xxxxxxxxxx
- Vector is a mathematical concept that holds both magnitude and direction.
Vector3.right /* (1, 0, 0) */
Vector2.right /* (1, 0) */
Vector3.left /* (-1, 0, 0) */
Vector2.left /* (-1, 0) */
Vector3.up /* (0, 1, 0) */
Vector2.up /* (0, 1) */
Vector3.down /* (0, -1, 0) */
Vector2.down /* (0, -1) */
Vector3.forward /* (0, 0, 1) */
Vector3.back /* (0, 0, -1) */
Vector3.zero /* (0, 0, 0) */
Vector2.zero /* (0, 0) */
Vector3.one /* (1, 1, 1) */
Vector2.one /* (1, 1) */
float length = myVector.magnitude /* Length of this Vector */
myVector.normalized /* Keeps direction, but reduces length to 1 */
xxxxxxxxxx
Vector3 testvector=new Vector3(0,1,1);
- Vector is a mathematical concept that holds both magnitude and direction.
Vector3.right /* (1, 0, 0) */
Vector2.right /* (1, 0) */
Vector3.left /* (-1, 0, 0) */
Vector2.left /* (-1, 0) */
Vector3.up /* (0, 1, 0) */
Vector2.up /* (0, 1) */
Vector3.down /* (0, -1, 0) */
Vector2.down /* (0, -1) */
Vector3.forward /* (0, 0, 1) */
Vector3.back /* (0, 0, -1) */
Vector3.zero /* (0, 0, 0) */
Vector2.zero /* (0, 0) */
Vector3.one /* (1, 1, 1) */
Vector2.one /* (1, 1) */
float length = myVector.magnitude /* Length of this Vector */
myVector.normalized /* Keeps direction, but reduces length to 1 */
xxxxxxxxxx
public Vector3 scaleName; // Scale property in the X, Y, and Z dimensions