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
0
Question by Wuzseen · Jun 30, 2013 at 05:42 AM · rotationrigidbodyquaternion

Point Object Towards X/Z Position

Here's an image that kind of shows what I'm trying to do:

http://i.imgur.com/36VZzkf.png

I have a cube riding along a track and I'm trying to make it use physics to do so. My cube slides along just fine so long as the track is straight, I've got the physics working basically just right.

What I need to do is point the cube properly so that it doesn't collide with the edges of the track in a destructive manner. Currently I've constrained X/Y/Z rotation on my rigidbody.

         Vector3 ntp = curveHandler.nextTrackPoint(transform.position); // Next track point...
         Vector3 dir = (ntp - transform.forward).normalized;
         if(rigidbody.velocity.magnitude < 3f)
             rigidbody.AddForce(dir * rotationSpeed);
         Quaternion lookRotation = Quaternion.LookRotation(dir);
         transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, Time.deltaTime * rotationSpeed);

This code is what I'm working with.

nextTrackPoint is a method that finds the points the black lines are coming out of in the image. Those points technically lie inside the track however. This method is working just as I intend it to, I can modify that if need be though.

Essentially I want my cube to look at the point, but above the track. That is, I only want it to rotate around its Y axis so that it points at what amounts to just the X/Z coordinate of the black line in the image.

My code isn't really doing the trick, any advice or direction would be great. This seems like a straightforward fix but I just can't nail it.

Comment
Add comment · Show 4
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 robertbu · Jun 30, 2013 at 07:27 AM 1
Share

If I understand correctly, all you need to so is zero out the 'y' value of 'dir' before you do the LookRotation(). Insert this between lines 4 and 5:

 dir.y = 0.0f;
avatar image Wuzseen · Jun 30, 2013 at 05:37 PM 0
Share

I think you understand what I'm trying to do--I get what you're suggesting as well.

Now I seem unable to rotate the object whatsoever. Restricting the rigidbody rotation should still allow you to rotate the transform in the code, no?

avatar image robertbu · Jun 30, 2013 at 05:44 PM 1
Share

Yes, you should still be able to rotate the object directly.

avatar image Wuzseen · Jun 30, 2013 at 05:46 PM 0
Share

I've been checking values, the "lookRotation" in my code always equates to (.0,.7,.0,.7). $$anonymous$$aybe the rotation is too small?

1 Reply

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

Answer by aldonaletto · Jun 30, 2013 at 07:09 PM

The direction the rigidbody is pointing doesn't affect (or is affected by) the direction it moves - you must rotate the cube to the movement direction, as you're currently doing. From your code, I suppose that rotationSpeed is too small: the object tries to follow the track direction, but too slowly to produce the desired effect - values in the range 5 to 20 should work fine.

Besides this, you probably will have problems trying to move the rigidbody along the track with physics: the force adds velocity in the desired direction, but the momentum makes the movement go in an intermediate direction, producing an overshoot. You can reduce the overshoot by using a high drag value: this reduces the momentum and makes the force effect more dominant in the final result.

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 Wuzseen · Jun 30, 2013 at 08:05 PM 0
Share

I think I'll opt for a more "puck" like train so that it rolls through the track a bit more naturally and physics doesn't make it hate the corners. I just want to change the direction so that, should we make a real model for the train, it's pointed forward properly.

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

16 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

Related Questions

Finding A Rigidbody's Rotation Speed And Direction 2 Answers

Player character X rotation going haywire on game start 1 Answer

Rotating Rigidbodies 2 Answers

Alternative to Quaternion.LookRotation() ? 2 Answers

Keeping transform.forward facing forwards 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