• 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 biggestgeek · Sep 04, 2013 at 06:05 AM · transform.forward

Transform.Forward dosen't work consistently

     public void generateTrack()
         {
             print ("generate Track Called");
             
             //Setting the location on next track in line
             nextTrackLocation = currentTrackLocation.forward * 10;
             print ("nextTrackLocation before inst" + nextTrackLocation);
             print ("currentTrackLocation before inst" + currentTrackLocation.position);
             /*
             nextTrackLocation.x = this.gameObject.transform.position.x;
             nextTrackLocation.y = this.gameObject.transform.position.y - 1f;
             nextTrackLocation.z = this.gameObject.transform.position.z + 5f;
             */
             
             
             //print (nextTrackLocation);
             
             //Instancing the next track in line
             GameObject trackInstance = Instantiate(planeStraightPreFab, nextTrackLocation, Quaternion.identity) as GameObject;        
             currentTrackLocation = trackInstance.transform;
             print ("nextTrackLocation after inst " + nextTrackLocation);
             print ("currentTrackLocation after inst" + currentTrackLocation.position);
         }

I'm currently attempting to generate a track 10 units infront of the current track. The first track is predefined and works well but as soon as this function is called again the next track in line will spawn at the previous track's position. The nextTrackLocation = currentTrackLocation.forward * 10; line does not seem to update the location.

Any help would be great. Thanks!

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Hoeloe · Sep 04, 2013 at 08:25 AM

You're misunderstanding what Transform.forward is. It gives you a direction relative to the transform object. So let's say you have a transform at coordinates (10,10,10), facing along the z-axis. Transform.forward would give you the vector: (0,0,1), which is the direction the object is facing, relative to its position. The problem you are experiencing is that (I assume) all the objects are facing in the same direction, so all your calls to currentTrackLocation.forward give the same vector, which you are then multiplying by 10 and using as the position. Hopefully you can see why they are all being instantiated in the same place. There is a solution, however, and a simple one: I mentioned that the forward vector was relative to the position, so the fix is simple: add the position back on! If you change your code to set the location to this:

  nextTrackLocation = currentTrackLocation.position + currentTrackLocation.forward * 10;

You should get exactly what you need, and hopefully you can see why.

Comment
Add comment · Show 4 · 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 biggestgeek · Sep 04, 2013 at 08:33 AM 0
Share

Thanks @Hoeloe! That worked! I've actually solved it by changeing the currentTrackLocation.forward to currentTrackLocation.transform.TransformPoint(Vector3.forward * 10);

Guess that works the same too.

avatar image Hoeloe · Sep 04, 2013 at 08:42 AM 0
Share

That works in pretty much exactly the same way, except that will also take into account the scale of the transform,so if you double the length, the new position will change accordingly - watch out for that.

avatar image biggestgeek · Sep 04, 2013 at 09:12 AM 0
Share

@Hoeloe I have another problem in that I have tracks which curve left and right and right now I have trouble getting them to align properly. Currently they are all just spawning in a straight line even if the track goes left or right.

Any idea how am I to go about doing that?

avatar image Hoeloe · Sep 04, 2013 at 09:18 AM 0
Share

That's because of the forward direction of the object. You'll want to make sure the forward vector (the blue arrow in the editor) is pointing in the direction you want the track to go - that should solve the issue.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Can you change an object's forward without rotating it? 2 Answers

Is there any way to translate normalized vectors into angles? 1 Answer

Rotate object around transform.forward 1 Answer

setting Transform.forward results in strange artifact 2 Answers

Trouble making laserbeam with LineRenderer 0 Answers


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