clone.velocity is referencing the rigidbody on clone and setting the velocity (how fast the object is moving), which is the object you instantiated. Rigidbody is the physics component. Transform is the component that handles the location, rotation, and scale.
Here’s the parameters for Object.Instantiate:
Instantiate(GameObject prefab, Vector3 position, Quaternion rotation)
The prefab and position parameters are self-explanatory, so I won’t go into those. Rotation is the rotation of the object, rotated along the x-axis, then y, then z. Quaternions use 4 dimensions, however, x, y, z, and w, which you shouldn’t change on their own according to the script reference. Supposedly they’re less memory-intensive than Euler angles (x,y,z), but I don’t understand or hope to understand why. The identity rotation (0, 0, 0) has the forward (+Z, Blue) axis of the object along the +Z axis of the world. If you’re using the rotation of the spawner object as the base rotation, then +Z of the Instantiated object will match the +Z of the spawner. If you’re moving the object along the +X (Right, Red) axis of the spawner and you want the Instantiated object’s forward vector to match the direction of it’s velocity, you could use [clone.forward = clone.velocity;], minus brackets, which will match the Instantiated object’s forward vector to the velocity vector.
What you’re doing in a step-by-step format (Code in Green):
- Code instantiates (Instantiate() projectile (projectile) at the current position (transform.position) and rotation (transform.rotation))
- Code sets the new object’s velocity (clone.velocity) to value V along the x-axis of the current object (= V*transform.right)Note that the x-axis of clone is the same as the x-axis of the object with the script on it! You will thus be moving the projectile right relative to the projectile’s forward axis.
What you want to be doing:
- Code instantiates projectile… blah blah blah
- Code sets the instantiated projectile’s forward vector (clone.forward) to be equal to the right vector of the object with the script (= transform.right)
- Code sets the projectile’s velocity (clone.velocity) to value V along the x-axis of this object (= V * transform.right)Note that this does not move the projectile right, as you might think, but it moves the object right relative to this object, which, due to step 2, is forward relative to the projectile.
You could also do this when instantiating the projectile: clone = Instantiate(projectile, transform.position, Quaternion.Euler(transform.right))Why do this? This does steps one and two in “What you want to be doing” in a single step: it creates the new projectile and correctly orients it so that it will appear to be moving forward.
And actually, var clone: Rigidbody; is not a variable within a variable, it is identifying the type of the variable “clone.” If you didn’t do this, “clone” could be a Transform, a Collider, or anything. By stating clone: Rigidbody; you are making sure that “clone” will be a rigidbody, requiring you to reference other objects as rigidbodies before you can assign them to “clone.”