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 murkertrer · Apr 22, 2014 at 06:58 PM · rotationmathfclampturrettilt

Help clamping a Rotation

I know this has been asked before, but, I can´t implement the other answers within my code (very noob here). Any help, greatly apreciated:

I have a turret and want to clamp the rotation, at 90° an 0°, so that it can only fire within that range.

 var angle:float = 0.0f;
 
 function Tilt (){
 
 var spinAmount = Time.deltaTime*100;    
 if (Input.GetKey(KeyCode.Q))
 {    
    angle = spinAmount; 
    transform.Rotate(angle,0,0);
 }

thankx!

Comment
Add comment · Show 4
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 xortrox · Apr 22, 2014 at 08:03 PM 0
Share

Can I see what the turret looks like in-game? Is the whole thing rotating or just the turret head etc?

avatar image murkertrer · Apr 22, 2014 at 08:50 PM 0
Share

Hi xortrox, thanks for your interest:)

Is is actually an empty object that i have it attached to my character ( a simple sphere) from where im generating the cannonballs.i figure this would make things simpler and more efective.

avatar image xortrox · Apr 22, 2014 at 10:05 PM 0
Share

You may want to make sure that the initial angle can go from 0 to 90 in your case without the euler angles exceeding above 360 or under 0, then you could use $$anonymous$$athf.Clamp:

 angle = spinAmount;
 transform.Rotate(angle,0,0);
 Vector3 cannonAngles = transform.eulerAngles;
 cannonAngle.x = $$anonymous$$athf.Clamp(cannonAngle.x, 0, 90);
 transform.eulerAngles = cannonAngles;


This is only one of many ways to do it, you could also be using Quaternions

avatar image xortrox · Apr 23, 2014 at 03:19 PM 0
Share

I could help you if you got $$anonymous$$mviewer.

1 Reply

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

Answer by ToxxicSin · Apr 23, 2014 at 03:39 AM

The simplest way in my opinion is to do Mathf.Clamp.

     funtion Update(){
     var anglesToEdit : type;
     angleToEdit.angle = Mathf.Clamp(angleToEdit.angle), -45, 45);
 //the angle you're clamping is probably the .y 
 //because that is the vertical axis which is used 
 //to rotate left and right
     }


and personally I would clamp the angle between -45 and 45 because your turret more than likely originally spawns with the center facing forward at 0

Comment
Add comment · Show 8 · 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 xortrox · Apr 23, 2014 at 01:46 PM 0
Share

Clamping euler angles from -45 to 45 will get troublesome because euler angles are simply horrible in comparison to quaternions when it comes to clamping, you most likely have to make sure your rotation never exceeds 360 degrees or goes below 0 to clamp a euler angle.

avatar image ToxxicSin · Apr 23, 2014 at 02:30 PM 0
Share

@xortrox Can't you do a $$anonymous$$imum of 315 and a maximum of 45?

avatar image xortrox · Apr 23, 2014 at 03:06 PM 0
Share

Try your code with clamping -45 to 45 do a quick setup in unity you'll notice it probably wont work and most likely only uses 0-45 degrees as euler angles dont go below 0 degrees You can however control the angle yourself and have a "starting" rotation like this:

float angleOffset = 0.0f; float startAngle = 0.0f;

void Update() {

angleOffset += Input.GetAxis("$$anonymous$$ouse X"); $$anonymous$$athf.Clamp(angleOffset,-45,45);

Vector3 v = transform.eulerAngles; v.x = angleOffset; //or v.x = $$anonymous$$athf.Lerp(v.x, angleOffset, Time.deltaTime);

transform.eulerAngles = v;

}

I'm not sure if this would work well at all

avatar image xortrox · Apr 23, 2014 at 03:13 PM 0
Share

Behold my glorious illustration, Glorious

In this example the 315 to 45 rotation involves a much larger range than my 0 to 90 degrees rotation, yet you would probably expect -45 to 45 to be the same as the 0 to 90 rotation, this is how euler angles would interpret it

glorious.png (8.7 kB)
avatar image ToxxicSin · Apr 23, 2014 at 06:41 PM 0
Share

If 315 is the $$anonymous$$imum, I thought the angle would be -45 to 45. If 315 was the maximum then your explanation would make sense, but I was unaware it would still interpret it like that.

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

22 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 avatar image avatar image

Related Questions

How to clamp Lookat? 2 Answers

Turret rotation clamp. 2 Answers

Clamping a wrapping rotation. 6 Answers

limit accelerometer controlled rotation 1 Answer

How to use Mathf.Clamp with child objects? 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