Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by jacob johnston · May 29, 2011 at 04:40 AM · instantiatetranslatelocal

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

5 Replies

· Add your reply
  • Sort: 
avatar image
0

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().

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jesse Anders · May 30, 2011 at 03:45 AM 1
Share

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 =...'.

avatar image
0

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".

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Wolfram · May 30, 2011 at 03:54 AM 0
Share

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.

avatar image jacob johnston · May 30, 2011 at 04:11 AM 0
Share

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.

avatar image Wolfram · May 30, 2011 at 01:01 PM 0
Share

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).

avatar image
0

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));
     }
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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

How can I instantiate and move objects smoothly? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges