Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 superventure · May 20, 2011 at 08:06 PM · raycastaxisnormalslope

Allow y rotation

Hello. I am using this code to orient the player to the slope of whatever is below it:

 function Update () {
     var hit : RaycastHit;
     var castPos = Vector3(transform.position.x,transform.position.y-.25,transform.position.z);

     if (Physics.Raycast (castPos, -transform.up, hit)) {
         transform.rotation = Quaternion.FromToRotation (Vector3.up, hit.normal);
         transform.rotation.z = 0;
     }
 }

This makes the player rotate to the ground. The thing is, it completely restricts their y rotation and you can only move forward/backwards. How do I release the y axis? I only really need the x to rotate up or down, with the z frozen.

The player is controlled with the following in a different script:

 var controller : CharacterController = GetComponent(CharacterController);
 
     var forward = transform.TransformDirection(Vector3.forward);
 
     var curSpeed = speed * Input.GetAxis ("Vertical");
 
     transform.Rotate(0, Input.GetAxis ("Horizontal") * rotateSpeed, 0);
 
 }if (!swimming && controller.isGrounded){
             controller.SimpleMove(forward * curSpeed);

Thank you

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
2
Best Answer

Answer by burnumd · May 20, 2011 at 08:24 PM

In the code you posted, you're manually setting the Transform rotation every frame, which may override any Input you're getting.

So one solution is to cache the y-offset of how much your player should be rotated and, whenever you're aligning the player, apply that offset imediately.

 private var yRotationOffset : float = 0;

 function Update () {
     var hit : RaycastHit;
     var castPos = Vector3(transform.position.x,transform.position.y-.25,transform.position.z);
 
     if (Physics.Raycast (castPos, -transform.up, hit)) {
         transform.up = hit.normal; //This is a shorter way of aligning the player to the surface's normal.
         transform.Rotate (0, yRotationOffset, 0); //Apply our cached y-rotation offset.
     }
     else
     {
         transform.Rotate (0, Input.GetAxis ("Horizontal"), 0); // If we're not aligning the player, we can apply the rotation directly
     }

     yRotationOffset += Input.GetAxis ("Horizontal");
 }

If you're modifying the player's rotation in some other script's update, there's no way to determine which will happen first. What's likely happening is that you're taking the player input and doing that rotation and then this script comes along and overwrites it. The simplest solution is to move your raycast code into the same script, above where you take input and do rotation based on that. Other options you might try include notifying your input script when the player's orientation has been determined and only then taking input that frame.

Comment
Add comment · Show 10 · 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 superventure · May 20, 2011 at 08:32 PM 0
Share

Thanks for replying! It still restricts y rotation though. It's not for mouselook, just for a character.

avatar image burnumd · May 20, 2011 at 08:38 PM 0
Share

Without knowing what you're doing to rotate the character, it's hard to say what's preventing it. I should have been clearer that setting transform.up is still potentially going to override input, depending on how it's being handled.

avatar image burnumd · May 20, 2011 at 08:49 PM 0
Share

Right, but it's relevant what's happening in that other script. We know that in this script, the transform is being rotated every frame to match the slope of whatever's below it. How does that other script rotate the player? Is it done in an absolute fasion (as with the included $$anonymous$$ouseLook script), or a relative fashion? If it's absolute, you're going to get a conflict with what you're trying to do here.

avatar image superventure · May 20, 2011 at 08:54 PM 0
Share

methinks that it's absolute, but I need them to work together. I included the script below--

avatar image burnumd · May 20, 2011 at 08:57 PM 0
Share

See updated answer.

Show more comments

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Set Quaternion to another Quaternion, but oriented along 1 Answer

Angle between Ray and Normal 1 Answer

Automatically rotating objects to fit on to others 0 Answers

How do I obtain the surface normal for a point on a collider (can't use RaycastHit.normal)? 3 Answers

How can i get point reletive to hit.point 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