- Home /
Local vs. Global movement
I am instantiating a projectile from an object and giving it a forward global translation, but I would like to have it go in the forward direction locally. How would I accomplish this? And thank you.
Answer by Jesse Anders · May 29, 2011 at 05:00 AM
You can use Transform.Translate() if you're moving the object by directly manipulating its transform. If the object has a rigid body component attached, you can use AddRelativeForce(). Also, Transform.forward will give you the local forward direction vector for the object, which you can use to move the object directly (e.g. by modifying the 'position' property), or to create e.g. a force vector for use with Rigidbody.AddForce().
Answer by jacob johnston · May 30, 2011 at 03:29 AM
Thank you. do you think you could give me an example script though. when i tried to write my own it came up with errors. So far i have : // Instantiate a rigidbody then set the velocity
var projectile : Rigidbody;
function Update () { // left mouse was pressed, launch a projectile if (Input.GetButtonDown("Fire1")) { // Instantiate the projectile at the position and rotation of this transform
var clone = Instantiate(projectile, transform.position, transform.rotation);
clone.AddComponent("Rigidbody");
// Give the cloned object an initial velocity along the current
// object's Z axis
clone.velocity = transform.TransformDirection (Vector3.forward);
}
} this sends the projectile in the forward direction Globally. if you could help make it local. also the console gives me this error when i press the mouse button to fire : MissingFieldException: Field 'UnityEngine.GameObject.velocity' not found. thank you for everything
These forums can be a little confusing; you're actually only supposed to post 'answers' as answers, while follow-up questions should be posted as comments or as edits to your original post. (Don't worry - you're not the first person to make this mistake.) As for your code, are you using UnityScript? I'm not entirely sure what all is going on in your code, but you're trying to access the field 'velocity' of the GameObject class, and there is no such field. What you want to do is set the velocity field of the rigid body component; to achieve that, try 'clone.rigidbody.velocity =...'.
Answer by Wolfram · May 30, 2011 at 03:33 AM
transform.forward is the local "forward" direction (=direction of the +Z axis) of a transform. If that is not what you are looking for, please explain "go in the forward direction locally".
Answer by jacob johnston · May 30, 2011 at 03:45 AM
i guess i am doing something wrong. it doesn't matter what rotation the object that instantiates the projectile is at, the projectile still goes in the same direction, which is down in the top down style of my game. is that local? i am not too new to unity or scripting but i am no expert. thank you for your help
Please do not post comments/questions as answers - this is not a forum.
What do you mean by "top down style"? At the time where your object is instantiating a projectile, where is its blue axis pointing? (press pause, select your object, make sure the "Local/Global" toggle in your editor is set to "Local", and observe where the blue axis is pointing.
i don't think my statement of top down style was that relevant or important i just could not think of another way to describe it. when i pause the game the blue arrow is pointing the same way the bottom of my spaceship is facing. it runs perpendicular to the ship. i hope this makes sense.
Unity generally has the convention that the blue axis (=Z) points "forward". If that is not the case with your spaceship, either modify it in the modelling program, add a transform above it which performs a rotation making "Z" the forward direction, or use a different axis in your scripts ins$$anonymous$$d of transform.forward (e.g., transform.right for the red axis, transform.up for the green axis).
Answer by digitalConundrum · May 30, 2011 at 05:22 AM
As long as you instantiate it in the direction you want it to travel, you could use something like:
transform.position += transform.forward * speed * Time.deltaTime;
to move it forward.
this code is in c#, but its an example of a projectile script I'm currently using in one of my projects. (I handle collision in the actor's scripts and the layer is passed to the projectile based on the actor that fired it...
e.g. the player fires the bullet, the player script passes its layer to the targetLayer variable (a bit of a misnomer, but it woks), the target's trigger then checks to see if the collision is in the enemy's layer and that it is a bullet; I use the name bullet, but you could also give it the tag bullet)
public class BulletTarget : MonoBehaviour {
public Vector3 source;
public float range = 10.0F;
public float bulletSpeed = 30.0F;
public int targetLayer;
void Start ()
{
gameObject.layer = targetLayer;
}
void FixedUpdate () {
if (Vector3.Distance(source, transform.position) > range)
{
Destroy(gameObject);
}
transform.position += transform.forward * bulletSpeed * Time.deltaTime;
}
public float GetDamage()
{
return (Random.Range(10, 30));
}
}
Your answer
Follow this Question
Related Questions
Instantiate and move a single object on click 0 Answers
Spawn along local axis. 3 Answers
How do you combine local and planar translation? 1 Answer
Accessing local system ( File Browser ) 2 Answers