• 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 nw15347 · Jun 17, 2019 at 02:33 PM · rotationraycastquaternionface

How to match rotation of Object to a face on another Object

Hello, I'm having some issues working out how to solve this problem:

alt text

My game is relatively simple at the moment, a cube travels along a track. However this track is slightly bent (which is necessary). It is cylindrical, so the player is able to move sideways around the track, with local gravity.

As the player moves forwards, they follow the black line, this is due to the slight bend. However what I'd like instead is for the player to generally follow the direction of the track as they move forward (i.e. the white line instead). I've realised that one solution to this would be simply if the player rotated on it's local Y axis to face the same direction (+some offset rotation) as the underlying face that it is in contact with, which as I understand could use a raycast.

I've not had any luck getting this to work using something like the following:

    if (Physics.Raycast(ray, out hit))
     { 
             transform.rotation = Quaternion.LookRotation(new Vector3(hit.point.x, transform.position.y, hit.point.z));
     
     }

If anyone has any ideas or solutions (I'm new to Unity), please let me know, Thank you.

facefollow.png (14.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

1 Reply

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

Answer by camdenlink7 · Jun 17, 2019 at 02:54 PM

Have you tried using a Rigidbody? If you have not, what you can do is attach a Rigidbody component to the cube, make it Kinematic, and then adding a force that is in the direct you want.

 void Update()
 {
      Rigidbody rb;
      rb = theCube.GetComponent<Rigidbody>();
      
      rb.AddForce(float xForce, float yForce, float zForce * Time.deltaTime);
 
 }


I hope this helps in some way, I am sorry if I didn't understand your question properly.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

179 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 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 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 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 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 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 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 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 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

Child transforms screwed up by parent's rotations 1 Answer

Quarternion.LookRotation is only rotating my game object on one axis 1 Answer

Why does the raycast hit only objects/faces of a certain rotation? 1 Answer

Setting rotation of instantiated object to that of a raycast? 1 Answer

How can I smooth the rotation using quaternion and raycast? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges