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 Carnivorous · Oct 01, 2014 at 09:10 PM · raycasting

Using a raycast's direction in parent's Y-rotation

Hi! I have a controllable character in 3d and I'm casting a raycast from it's center and for the direction of the raycast I'm using rigidbody.velocity.normalized. Now, with debug.drawRay I'm seeing that the raycast works properly, but I can't figure out how to get the rotation of the ray to use for the player's Y-rotation. The goal would be to detect the direction of the ray so that my character would have a target Y-rotation or something like that. Hope this question is simple enough. I didn't want to resort to UnityAnswers but here I am anyway.

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 robertbu · Oct 01, 2014 at 10:54 PM

The answer will depend on some context you did not provide:

  • Is the character always aligned with Vector3.up?

  • Is the velocity always parallel to the XY plane (i.e. no 'y' component)?

  • Did you construct your object so that the front is facing positive 'z' when the rotation is (0,0,0).

  • Do you need immediate rotation, or a smoothed rotation.

  • If you need a smoothed rotation, do you want that smoothing eased or at a constant speed?

If your answers are yes, yes, yes, no, no, then you can do this:

  var v = rigidbody.velocity;
  if (v != Vector3.zero) {
      transform.rotation = Quaternion.LookRotation(v);
  }

Other answers to the five questions will result in different code.

Comment
Add comment · Show 2 · 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 Carnivorous · Oct 05, 2014 at 11:35 AM 0
Share

Hi, and thanks for the reply. I think that the character is always aligned with Vector3.up The second one is a no, the y-velocity comes from jumping & falling. Yes, it's facing positive z. And I was going for a smoothed rotation. I'm not too terribly much familiar with rotations, so could you enlighten me about "smoothing eased". To me it sound better than constant speed though. Cheers.

avatar image robertbu · Oct 05, 2014 at 01:49 PM 0
Share
 #pragma strict
 
 var speed = 5.0;
 
 function Update() {
      var v = rigidbody.velocity;
      v.y = 0;
      if (v != Vector3.zero) {
           var q = Quaternion.LookRotation(v);
           transform.rotation = Quaternion.Slerp(transform.rotation, q, Time.deltaTime * speed);
      }
 }

You may also like a constant speed rotation. Replace 'Slerp' with 'RotateTowards' and greatly increase the value of speed (in the Inspector).

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

2 People are following this question.

avatar image avatar image

Related Questions

how can i instantiate a GameObject in mouse position and in a special distance from the camera? 2 Answers

Help with Raycast C# 0 Answers

Obstacle avoidance 1 Answer

C# RayCast Fire Cooldown DeltaTime Help 1 Answer

Ultimate FPS Camera, Raycasting Troubles :( 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