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 stuthemoo · Sep 13, 2011 at 07:04 AM · clamplimitrestrict

Limit rotation?

Hi, I'm using this code to move by object side to side and spin it at the same time:

 var speed : float = 10.0;
 var rotationSpeed : float = 100.0;
 
 function Update () {
     transform.Translate(Vector3.right * speed * Input.GetAxis ("Horizontal") * Time.deltaTime, Space.World);
     transform.Rotate(-Vector3.forward * rotationSpeed * Input.GetAxis ("Horizontal") * Time.deltaTime);
 }

But I want to limit the rotation so that it only spins 30 degrees and then continues to translate. I also want to limit the translation so that it stops after 50 units. I've looked at Mathf.Clamp but can't figure out how to implement it. How can I do this? I promise you this will help me learn :)

Comment
Add comment · Show 5
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 stuthemoo · Sep 14, 2011 at 07:12 AM 0
Share

Thanks guys, what if I want to add some damping to the rotation is that possible? eg it slows down as its about to reach its limit.

avatar image syclamoth · Sep 14, 2011 at 07:24 AM 0
Share

The problem with that is that it implies that the current rotation knows how fast it is going, as well as how close it is to being clamped- you might want to write your own damping algorithm, or maybe make the rotation non-linear. The Vector3 library includes a SmoothDamp function, maybe this is what you want? You would clamp the target value using the algorithms below, and then interpolate the actual value using some smoothing function.

avatar image stuthemoo · Sep 16, 2011 at 05:28 AM 0
Share

Ok great. Just one more thing, how can I make my object rotate back to 0 degrees when the Horizontal axis is released? So the object only rotates while the left/right keys are held and then it rotates back to 0 when these keys are let go. Cheers.

avatar image jahroy · Sep 16, 2011 at 06:59 AM 0
Share
 function Update ()
 {
 
     if ( Input.GetAxis("Horizontal") ) {
         
         /* place code to do stuff when the keys are held down here */
 
     }
 
 
     else {
 
         /* no horizontal input - place code to reset rotation to zero here */

     }
 }
avatar image mcarriere · Sep 16, 2011 at 01:59 PM 0
Share

I've updated my code to include returning back to 0 degrees. If you do it only when the horizontal axis is 0, you end up getting some relatively jerky return rotation, whereas if you have your object constantly trying to rotate back towards 0, the motion will be quite smooth.

You should mark one of these answers as the correct answer, and continue on with your project. Don't use the site to write your game for you. :) (Remember, you said you wanted to learn!)

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by mcarriere · Sep 13, 2011 at 08:10 PM

This works, but I'll admit, it's pretty hacky:

 var speed : float = 10.0;
 var rotationSpeed : float = 100.0;
 var rotationReturnSpeed : float = 5.0;
 var initialPosition : Vector3;
 var currentAngle : float;
 var currentDistance : float;
 
 function Start(){
     initialPosition = transform.position;
     currentDistance = 0;
     currentAngle = transform.eulerAngles.z;
 }
 
 function Update () {
     currentDistance += speed * Input.GetAxis ("Horizontal") * Time.deltaTime;
     currentDistance = Mathf.Clamp(currentDistance, -50, 50);
     var newPosition : Vector3 = initialPosition;
     newPosition.x += currentDistance;
     
     transform.Translate(newPosition - transform.position, Space.World);
             
     currentAngle += (-1 * rotationSpeed * Input.GetAxis ("Horizontal") * Time.deltaTime);
     currentAngle = Mathf.Clamp(currentAngle, -30, 30);
     
     // this will cause the currentAngle to tend to 0.
     currentAngle -= rotationReturnSpeed * Time.deltaTime * currentAngle;

     transform.eulerAngles = new Vector3(0,0, currentAngle);
 }
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
avatar image
0

Answer by jahroy · Sep 13, 2011 at 08:37 PM

You give the clamp function three values:

The initial value you want to clamp, the min allowed value, and the max allowed value.

If the initial value is less than the min, clamp will return the min.

If the initial value is more than the max, clamp will return the max.

The initial value will be returned otherwise.

Here's one way to limit translation using the Mathf.Clamp:

var speed : float = 10.0; var rotationSpeed : float = 100.0;

var minX = -10; var minY = -10; var minZ = -10;

var maxX = 10; var maxY = 10; var maxZ = 10;

function Update () {

 /* determine the translation vector using some nifty code */

 var panVector = calculateTranslationVector();

 /* add the translation to the current position */

 var beforeClamp = transform.position + panVector;


 /* clamp all three coordinates so the oject can't move too far */

 var afterClamp : Vector3;

 afterClamp.x = Mathf.Clamp(beforeClamp.x, minX, maxX);
 afterClamp.y = Mathf.Clamp(beforeClamp.y, minY, maxY);
 afterClamp.z = Mathf.Clamp(beforeClamp.z, minZ, maxZ);

 /* set the transforms position to the clamped vector */

 transform.position = afterClamp;

}

There's probably a function that clamps a vector, but you get the idea.

You would use the same logic to clamp a rotation.

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Two way joint? 1 Answer

How to restrict distance from object 3 Answers

limit accelerometer controlled rotation 1 Answer

Clamp rotation of a GameObject 1 Answer

How do I limit the vertical rotation of a camera? 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