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 gathos · Oct 01, 2013 at 10:57 PM · rotationrigidbodymathf.clamp

limiting rotation of a rigidbody in C#

so... I have been fighting with this for a while. I am new to unity so please excuess my complete ignorance.

this is a "space" flying game. simple, no gravity. The controls work the way I want them to because I have a script that I found and suited to my needs, works great except I want to limit the "roll" of my object say between 45 degrees and -45 degrees.

I thought the line "Mathf.Clamp(AddRot.z,-45,45);" would solve it but my limited understanding is hindering me.

I think it is because the rotation works between 0 and 360, but I am not sure how to tell my object that it can only rotate between 45 and 315.

Heck I am probably completely off my rocker and using it incorrectly and in the wrong place.

please help. this is what my flyscript.cs looks like.

 using UnityEngine;
 using System.Collections;
 
 public class flyscript : MonoBehaviour {
     // Use this for initialization
     void Start () {
 
     }
     
     // Update is called once per frame
     public float AmbientSpeed = 100.0f;
 
     public float RotationSpeed = 200.0f;
     
 
     void Update()
     {        
         Quaternion AddRot = Quaternion.identity;
         float roll = 0;
         float pitch = 0;
         float yaw = 0;
 
         roll = Input.GetAxis("roll") * (Time.deltaTime * RotationSpeed);
         pitch = Input.GetAxis("pitch") * (Time.deltaTime * RotationSpeed);
         yaw = Input.GetAxis("yaw") * (Time.deltaTime * RotationSpeed);
         AddRot.eulerAngles = new Vector3(-pitch, yaw, -roll);
         Mathf.Clamp(AddRot.z,-45,45);
         rigidbody.rotation *= AddRot;
         Vector3 AddPos = Vector3.forward;
         AddPos = rigidbody.rotation * AddPos;
         rigidbody.velocity = AddPos * (Time.deltaTime * AmbientSpeed);
     }
 }
Comment
Add comment · Show 6
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 theUndeadEmo · Oct 01, 2013 at 11:08 PM 0
Share
  AddRot.eulerAngles = new Vector3(-pitch, yaw, -roll);
  $$anonymous$$athf.Clamp(AddRot.z,-45,45);


change the above to the code below, not sure if this is what you wanted. i am assu$$anonymous$$g you want to stop the role going above 45 or less than -45. its a simple solution

 if (roll>45)
 {
    roll = 45;
 }
 
 else if (roll<-45)
 {
    roll = -45;
 }
 AddRot.eulerAngles = new Vector3(-pitch, yaw, -roll);
  
avatar image gathos · Oct 01, 2013 at 11:14 PM 0
Share

yeah I tried something similar. And I just tried your code, but I get no error messages, but my object still does not stop rotating or "rolling" past a 45 degree angle.

Thank you very much for trying to help me.

avatar image gathos · Oct 01, 2013 at 11:19 PM 0
Share

I just did a print of my objects rigidbody.rotation.z it never goes above 1 or below -1 ... i guess it is because it never reaches a 45 degree rotation within the update() function. it just keeps rotating a small amount every frame.

i think im confused :(

avatar image theUndeadEmo · Oct 01, 2013 at 11:31 PM 0
Share

does you game work with keyboard controls only?

if that's the case. this might not be the optimum solution but will get your game working

 // what ever control you want to use for yaw
 
 if (Input.Get$$anonymous$$ey("a"))
 {
     role-= 0.5f// what ever value you want to use, the higher it is the faster it will be;
 }
 
 if (Input.Get$$anonymous$$ey("d"))
 {
     role+=0.5f;
 }
 
 if (Input.Get$$anonymous$$ey("w"))
 {
    pitch +=1;
 }
 
 if (Input.Get$$anonymous$$ey("s"))
 {
    pitch -=1;
 }
 
 
 if (roll>45)
 {
    roll = 45;
 }
  
 else if (roll<-45)
 {
    roll = -45;
 }
 
 transform.eulerAngles = new Vector3(pitch, yaw,roll);
avatar image gathos · Oct 01, 2013 at 11:39 PM 0
Share

that is the only thing i have been working with is keyboard controls. i don't use a mouse to control it, but i would like it to be "modular" to potentially work with any type of input.

Show more comments

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Fattie · Oct 02, 2013 at 08:41 AM

Dude - can you simply use Rotate

http://docs.unity3d.com/Documentation/ScriptReference/Transform.Rotate.html

it is unbelievably simple to use. You should very rarely be fooling with the underlying angles.

As a student, you should also study and experiment with

http://docs.unity3d.com/Documentation/ScriptReference/Transform.RotateAround.html

which is incredibly useful. You have to understand the concept of the "rotation axis" to use it, which is exactly what you have to learn here.

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 gathos · Oct 02, 2013 at 05:39 PM 0
Share

Thank you fattie i will use this from now on, the only i just read elsewhere on unity answers link text to only use angles. thank you for pointing me towards the right goal. I will continue to work on it.

avatar image Fattie · Oct 03, 2013 at 06:34 AM 0
Share

Have fun !!

avatar image
0

Answer by Acr1m · Oct 02, 2013 at 02:43 AM

I think your problem is that you are multiplying your "AddRot" value (which is essentially a deltaRotation) after you've clamped it, when you need to clamp it AFTER the addition of the rotation. Cuz as it is, it's clamping, then multiplying the AddRot with the -45 and the 45.

 //        Mathf.Clamp(AddRot.z,-45,45);
         rigidbody.rotation *= AddRot;
         
         if(transform.localRotation.z < -45)
             transform.localRotation.z = -45;
         else if(transform.localRotation.z > 45)
             transform.localRotation.z = 45;
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 raimon.massanet · Oct 02, 2013 at 01:31 PM

As Fattie said, you should use Transform.Rotate to rotate your object, instead of messing with its euler angles.

However, your code is not working as expected because your are not clamping your rotation value, because your are not assigning it.

You need to assign the result of Mathf.Clamp(AddRot.z,-45,45) somewhere and apply it.

Comment
Add comment · Show 1 · 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 gathos · Oct 02, 2013 at 05:41 PM 0
Share

I need to figure out how to use mathf.clamp before i can apply it anywhere. thank you for your time. I will continue to work on it.

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

20 People are following this question.

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

Related Questions

Can i Move/Rotate triggers without Rigidbodies? And other collider questions. 3 Answers

Pysics not working as expected 1 Answer

Oculus VR Wrist Rotation Relative to Arm 0 Answers

Objects Instantiating at wrong position 3 Answers

Help making orbiting planets,How to rotate around the objects with the largest mass? 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