Unity upcast object to Vector3 code example
Example 1: unity instantiate vector3
using UnityEngine;// Instantiate a rigidbody then set the velocitypublic class Example : MonoBehaviour
{
// Assign a Rigidbody component in the inspector to instantiate public Rigidbody projectile; void Update()
{
// Ctrl was pressed, launch a projectile
if (Input.GetButtonDown("Fire1"))
{
// Instantiate the projectile at the position and rotation of this transform
Rigidbody clone;
clone = Instantiate(projectile, transform.position, transform.rotation); // Give the cloned object an initial velocity along the current
// object's Z axis
clone.velocity = transform.TransformDirection(Vector3.forward * 10);
}
}
}
Example 2: unity vector3 to array
Vector3 vector = new Vector3(0,1,0);
// Convert to float array
floats[] toFloat = new float[]{vector.x,vector.y,vector.z};
// Back to vector
vector = new Vector3 (toFloat[1], toFloat[2], toFloat[3]);