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 dave 2 · Apr 03, 2011 at 12:33 PM · rotationjavascriptlimit

How can I set a limit to my object rotation???(Here's the code)

var velocit = 3.0;

function Update () {

          var xtrans = Input.GetAxis("Horizontal") * velocit * Time.deltaTime;
          var ytrans = Input.GetAxis("Vertical")    * velocit * Time.deltaTime;
          var ztrans = Input.GetAxis("Fire1")        * velocit * Time.deltaTime;

      transform.Translate(-ytrans,ztrans,xtrans,Space.World);
      transform.Rotate(xtrans,0,ytrans);

}

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Peter G · Apr 03, 2011 at 12:48 PM

You can try directly testing EulerAngles, but that will give you issues so you should probably try storing them in your own variables. You could do it using transform.Rotate(), but you would need several conditionals to do all the checks and I don't like how it looks. So this is method is different but should do the same thing.

var velocit : float;

//Change the value of these two fields to control the max angles. var maxRotX = 90.0; var maxRotY = 180.0;

private var curRotX : float; private var curRotY : float;

function Update () {

  //This is all still the same.
  var xtrans = Input.GetAxis("Horizontal") * velocit * Time.deltaTime;
  var ytrans = Input.GetAxis("Vertical")   * velocit * Time.deltaTime;
  var ztrans = Input.GetAxis("Fire1")      * velocit * Time.deltaTime;

  transform.Translate(-ytrans,ztrans,xtrans,Space.World);

  //Set the curRot_ to the curRot_ + the _trans and clamp it between the min and max so we will never be larger than some angle.
  curRotX = Mathf.Clamp(curRotX + xtrans, -maxRotX, maxRotX);
  curRotY = Mathf.Clamp(curRotY + ytrans, -maxRotY, maxRotY);

  transform.rotation = Quaternion.Euler(curRotX, 0, curRotY);
  //This constructs an angle (x, y, z) and assigns it to the rotation.
  //EDIT:
  //And you called it ytrans, but you put it in the z pos so I kept that convention.

}

The problem with reading the euler angles directly is that Unity internally stores rotations as quaternions. When it converts them to euler angles, there are multiple values is can give. For example, 90 degrees is the same as -270 degrees. And Unity occasionally changes the value it gives you.

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 Statement · Apr 03, 2011 at 01:10 PM

An alternative approach to the same problem.

var velocity = 3.0;

var minXAngle : float = -45; var maxXAngle : float = 45; var minZAngle : float = -45; var maxZAngle : float = 45;

private var xAngle : float = 0.5f; private var zAngle : float = 0.5f;

function Update () { var vdt = velocity Time.deltaTime; var xtrans = Input.GetAxis("Horizontal") vdt; var ytrans = Input.GetAxis("Vertical") vdt; var ztrans = Input.GetAxis("Fire1") vdt;

 transform.Translate(-ytrans, ztrans, xtrans, Space.World);
 Rotate(xtrans, ytrans);

}

function Rotate (x : float, z : float) { xAngle = Mathf.Clamp01(xAngle + x); zAngle = Mathf.Clamp01(zAngle + z);

 var euler = transform.eulerAngles;
 euler.x = Mathf.Lerp(minXAngle, maxXAngle, xAngle);
 euler.z = Mathf.Lerp(minZAngle, maxZAngle, zAngle);
 transform.eulerAngles = euler;

}

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

No one has followed this question yet.

Related Questions

Need help with limiting rotation. 1 Answer

puzzle with rotating statues 1 Answer

Euler angle problems with rotation limit script 3 Answers

Character Rotation - Script not functioning correctly 1 Answer

Transform.rotate limits 3 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