Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by TheCount · Dec 27, 2013 at 09:45 PM · rotationjavascripttransformpathfindingaxis

Need to change direction of transform?

I'm really confused and maybe in over my head on this AI stuff.

Here's the skinny: I have an object that needs to know if it should turn left or right.

I have:

 function GetSteer() {
 var relativePoint = transform.InverseTransformPoint(path[currentPathObj].position);
 newSteer = relativePoint.z;
 if (newSteer > 0){
 newSteer = 1;
 }
 else if (newSteer < 0){
 newSteer = -1;
 }
 
 else if (newSteer == 0){
 
 }
 }

If newSteer = 1, the object will turn right, if it's negative the object will turn left.

Here's the problem:

My object's forward isn't the z-axis.

Here is a picture:

alt text

(It's an aerial view thus I left out the Y axis)

I want the object to turn towards the gray x and go towards the gray x.

The drawn axes are what my axes look like they are oriented to in unity.

explanation.png (5.0 kB)
Comment
Add comment · Show 2
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 Vonni · Dec 27, 2013 at 10:55 PM 0
Share

What is your objects forward then? Just use that ins$$anonymous$$d of .z, but this seems like an odd way to do what I think you want to do

avatar image TheCount · Dec 27, 2013 at 10:57 PM 0
Share

$$anonymous$$y object orients itself such that it's right side faces the currentPathObj. If that helps

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by aldonaletto · Dec 27, 2013 at 11:46 PM

Do you want to know whether the target is at left or right? If Y is your vertical direction, check the polarity of Y of the cross product of current forward and the target direction:

 // get the target direction:
 var targetDir: Vector3 = path[currentPathObj].position - transform.position;
 targetDir.y = 0; // keep only the horizontal direction
 // get 1 or -1 depending on the relative direction:
 newSteer = Mathf.Sign(Vector3.Cross(targetDir, transform.forward));

If newSteer gets values opposed to what you want, just swap the arguments of Vector3.Cross.

If you want a value proportional to how much the vehicle must steer, normalize targetDir and use the cross product directly:

 newSteer = Vector3.Cross(targetDir.normalized, transform.forward);

The cross product of two normalized vectors (transform.forward is normalized) is numerically equal to the sine of the angle between them.

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 TheCount · Dec 27, 2013 at 11:56 PM

I got it. If i set it to the negative y axis it works.

I'm not sure why but it does what I want it to. XD

Thanks!

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

20 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 avatar image avatar image avatar image

Related Questions

Move an object toward an angle in 2d space 0 Answers

Problem changing rotation of 3rd Person Controller 1 Answer

Follow x rotation of a target object. 1 Answer

[JS] transform.rotation not working 1 Answer

How to make the player turn left or right only in desired places o edges and not everywhere ???? 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