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 Mrcl1450 · Oct 26, 2014 at 06:02 PM · rotationraycastlookatsplinespherecast

SphereCast help

My problem is this. My player can run around a tube with no problem but when the tube makes a curve she's always looking forward like this: alt text

The first arrow is where I want her to be looking, at the second is what she is doing because there is no rotation.

The problem starts here:

alt text

I want her to turn on the curve here looking at the tube, not always forward.

Where is my code

         Ray ray;
         RaycastHit hit;
 
         // movement code
         
         Vector3 dir = Vector3.zero;
         dir.x = Input.GetAxis("Horizontal");
         if (dir.sqrMagnitude > 1)
             dir.Normalize();
         
         dir *= Time.deltaTime;
         transform.Translate(dir * Aspeed);
         //myTransform.Rotate(0, Input.acceleration.x * turnSpeed * Time.deltaTime, 0);
         //myTransform.Rotate(0, Input.GetAxis("Horizontal") * turnSpeed * Time.deltaTime, 0);
 
         // update surface normal and isGrounded:
         ray = new Ray(myTransform.position, -myNormal); // cast ray downwards
         if (Physics.Raycast(ray, out hit))
         { // use it to update myNormal and isGrounded
             isGrounded = hit.distance <= distGround + deltaGround;
             surfaceNormal = hit.normal;
             Debug.DrawLine(transform.position, hit.point, Color.blue);
         }
         else
         {
             isGrounded = false;
             surfaceNormal = Vector3.up; // assume usual ground normal to avoid "falling forever"
         }
         myNormal = Vector3.Lerp(myNormal, surfaceNormal, lerpSpeed * Time.deltaTime);
         Vector3 myForward = Vector3.Cross(myTransform.right, myNormal); // find forward direction with new myNormal
         Quaternion targetRot = Quaternion.LookRotation(myForward, myNormal); // align character to the new myNormal while keeping the forward direction
         myTransform.rotation = Quaternion.Lerp(myTransform.rotation, targetRot, lerpSpeed * Time.deltaTime);
         anim.SetFloat("Speed", moveSpeed);
         myTransform.Translate(0, 0, 1f * moveSpeed * Time.deltaTime); // auto move the character

If I use

 myTransform.Rotate(0, Input.GetAxis("Horizontal") * turnSpeed * Time.deltaTime, 0);

she can rotate but I just want to control the player rotation around the tube, not the curve rotation.

I appreciate if someone can help me.

EDIT: If I add to my code

 if (Physics.SphereCast(transform.position, 0.5f, -transform.up, out hit, 5))
         {
             transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.LookRotation(Vector3.Cross(transform.right, hit.normal), hit.normal), Time.deltaTime * 5.0f);
         }

it does exactly what I want and align the player with the tube, but it makes the player do "small jumps" as if she were walking over spheres. alt text

Can anyone help me avoid that?

2.png (287.2 kB)
1.png (78.1 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 robertbu · Oct 26, 2014 at 09:10 PM 0
Share

I'm not sure I completely understand your problem, but it appears you have too much freedom to detect the turn. I think you are going to have to define a spline down the axis of your tube in order to detect turns, or at least do a whole lot more raycasting.

avatar image Mrcl1450 · Oct 26, 2014 at 09:38 PM 0
Share

robertbu Thank you for your comment, I've thought about doing a raycast line of 45 degrees to make it turn if not detect the tube ahead, but with no success because she runs all over the tube so it is always detected.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by yanuaris · Mar 25, 2016 at 11:26 AM

Use Transform.transformdirection with the character's transform as the root and the camera's.

It'll then take into account the camera direction vector or whereever it is pointing to and turn it to the character's direction. It's not as precise yet, but it's close enough

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Top down look rotation snaps when looking left, right, and back 1 Answer

[HELP]Object Look At Raycast position 2 Answers

Rotate object to lookAt point and be perpendicular to the ground 1 Answer

Have An Object Rotate Around Transform A So That Transform B is Looking at Transform C (C#) 0 Answers

LookAt To Only Rotate on Y Axis 2 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