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
0
Question by superventure · May 29, 2011 at 06:29 AM · rotationjavascriptlerptranslation

Smooth and restrict rotations?

I'm Stuck!! How do I correctly lock the z axis so it cannot rotate, and how can I get the rotations to be smoother (less sensitive)? Thanks a bunch for any help!

 var howMuch = .1;
 var speed = 8;
 private var yRotationOffset : float = 0;
 
 function Update () {
     var controller : CharacterController = GetComponent(CharacterController);
     var hit : RaycastHit;
     var castPos = Vector3(transform.position.x,transform.position.y- howMuch,transform.position.z);
     if (controller.isGrounded)
     {
         if (Physics.Raycast (castPos, -transform.up, hit)) {
             transform.up = hit.normal; //direction
             transform.Rotate (0, yRotationOffset, 0); //Apply y-rotation offset.
         }else{
             // If we're not aligning the player, we can apply the rotation directly
             transform.Rotate (0, Input.GetAxis ("Horizontal"), 0); 
         }
         yRotationOffset += Input.GetAxis ("Horizontal") * speed;
         if (!controller.isGrounded){
             transform.rotation.z = 0;
             transform.rotation.x =0;
         }
     }//if (controller.isGrounded)
 }//update()
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

Answer by SirGive · May 29, 2011 at 06:34 AM

You can try using the "lerp" function to get smoother rotations, and setting rotation.x to zero fixes the rotation problem right? Check here for a reference on all kinds of "lerp" functions.

Here is how you would "lerp" angles:

 var minAngle = 0.0;
 var maxAngle = 90.0;
 
 function Update () {
     //Time.time is one second. Change that to any time you want
     var angle : float = Mathf.LerpAngle(minAngle, maxAngle, Time.time);
     transform.eulerAngles = Vector3(0, angle, 0);
 }

And here is how you would "lerp" a vector3:

 var objPos : transform.position;
 var endPos : Vector3(0,0,0);
 
 function Update () {
     //Time.time is one second. Change that to any time you want
     transform.position = Vector3.Lerp(objPos, endPos, Time.time);
 }


Comment
Add comment · Show 7 · 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 29, 2011 at 06:38 AM 0
Share

no, I set the x to zero to just when the character controller is not grounded (for when they jump or swim). I am ai$$anonymous$$g to get the player to rotate to the slope beneath it when grounded. How would I plug in a lerp function?

avatar image SirGive · May 29, 2011 at 06:43 AM 0
Share

lerp functions basically work like this: the move from one position to the next and how fast. It all depends on what you want to lerp, though. For instance, Vector3.Lerp(fromPositionOne, toPositionTwo, timeToLerpInSeconds) is how you would lerp a vector. Check the link for a bunch of different types of lerps. Also, if you want there to be no rotation then set the rotation dimension to be zero.

avatar image superventure · May 29, 2011 at 07:10 AM 0
Share

I tried plugging transform.rotation.z =0; into the script when it is grounded, but it made things worse by upsetting all the rotations, so that is why I need help there. Is your example for locking with a new variable in C#? I only know java script. Also, I meant how I would alter transform.Rotate to lerp ins$$anonymous$$d.

avatar image SirGive · May 29, 2011 at 07:13 AM 0
Share

yeah. for JS just don't use "new"
And for lerping, just use mathf.Rotate(firstAngle, secondAngle, speed) for the angle you want to rotate.

avatar image superventure · May 29, 2011 at 07:35 AM 0
Share

Okay, I'm sorry but I keep getting lost- I don't know how to set up either Vector3(var.x, var.y, 0) or Quaternion(var.x, 0, 0, var2) correctly. I keep getting error messages. var.x? I've never seen a var set like that. Also, how would I define either the first angle or the next angle I need to lerp to?

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

2 People are following this question.

avatar image avatar image

Related Questions

Smoothly Rotate 2D sprite 90 degrees on click 2 Answers

Character Rotation - Script not functioning correctly 1 Answer

Rotate character to the moving direction problems? 2 Answers

Use Lerp to rotate object 2 Answers

Aligning a transform according to two different positions and directions 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