• 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 Mungyun · Jan 13, 2013 at 04:44 PM · transform.forward

Keep forward directon in line with path

I have a somewhat side scrolling game I am working on and I am having a difficult time figuring out how to keep the players forward direction in line with the terrain path as it is not always going the same direction.

Below, if you look at the second image you will see that their forward isnt following the path which should point to those platforms, but it points off to the side.

How can i go about accomplishing this? The current way I am trying is to use a spline and keep the forward direction of the player the same as the spline but it doesn't seem to work as good as i was hoping. Possibly i am just doing it wrong.

alt text

alt text

2013-01-13 10_31_14-unity - terraintest.unity - midaerav1-3 - web player_.png (258.0 kB)
2013-01-13 10_31_46-unity - terraintest.unity - midaerav1-3 - web player_.png (219.7 kB)
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by jogo13 · Jan 13, 2013 at 08:38 PM

An easy way to align along a path is to LookAt a point slighty ahead on the path:

 Vector3 lookat_target = // you just need a point slightly ahead of current position
                         // (however your personal spline/path implementation calculates it)   
 
 transform.LookAt(lookat_target);

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 Mungyun · Jan 18, 2013 at 04:04 PM 0
Share

Yep thats pretty much what i am doing with my spline point being ahead of my player. See my comment to Loiuzein =-)

avatar image
0

Answer by Loius · Jan 13, 2013 at 08:48 PM

I use a spline for movement+facing as well; what's your code look like? I use 4-point bezier curves, but it's the same idea - use the difference in curve(@X+1) - curve(@X) to determine the direction at time X.

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 Mungyun · Jan 18, 2013 at 04:03 PM 0
Share

Sorry this may sound confusing as I cant really describe this all that well. But, using SuperSplines, I kinda got it working by having the current point in the playerLookSpline slightly ahead of the player and when the player moves along +z or -z I set the speed of the SplineAnimator to a positive or negative value respectively.

$$anonymous$$y main problem with this method is that I have to keep guessing at the speed the 'saPlayerLookDirection' should be, as the splines speed vs my players speed are not the same. So currently all i am doing is

saPlayerLookDirection.speed = -moveDir.z Time.deltaTime playerPathSpeed;

Where moveDir is to deter$$anonymous$$e if the player is moving forward or backward and playerPathSpeed is the variable i use to slow down or speed up the lookAt point on the spline.

I think i got this working relatively well but does this seem like a decent way to go about it? Never really tried such a thing so Im not sure if there is a better way to handle this

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

10 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

Related Questions

Bayonet Action Transform 0 Answers

how to get the GameObject[] that my player touching 3 Answers

Object positioned at wrong location 1 Answer

"Transform.side" version of Transform.forward? 1 Answer

transform.forward breaks when changing scenes 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