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 Abdou23 · May 09, 2015 at 09:14 AM · rotationclamped rotation

Clamp rotation value.

I have a 3D object that rotates according to mouse position, but I want to clamp it so it doesn't get further or lower than certain values. Here is my code:

 void LookAt () {
 
         float distance = transform.position.z - Camera.main.transform.position.z;
 
         Vector3 position = new Vector3(Input.mousePosition.x, Input.mousePosition.y, distance);
         position = Camera.main.ScreenToWorldPoint(position);

         position.x = Mathf.Clamp(position.x, -70, 70); 
         position.z = Mathf.Clamp(position.z, -70, 70);

         Vector3 target = new Vector3 (position.x, transform.position.y, position.z); // Use current object positin.y
         transform.LookAt(target);
     }

But unfortunately it doesn't work, it keeps rotating 360.

Edit: I tried this and partially worked, I changed this line :

  Vector3 position = new Vector3(Input.mousePosition.x, Input.mousePosition.y, distance);

To this:

 Vector3 position = new Vector3(Mathf.Clamp( Input.mousePosition.x, Input.mousePosition.y, distance), 350, 40);

It now locks perfectly on the left side, but not on the right side, tried playing with the values but didn't succeed.

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 siaran · May 09, 2015 at 11:48 AM 0
Share

There doesn't seem to be any code that pertains to rotation other than transform.LookAt(), which is the last thing you do so even if there where the only rotation you would see would be the one calculated by transform.LookAt().

avatar image OctoMan · May 09, 2015 at 11:58 AM 0
Share

I used something simlar a while ago, where i wanted to bank/tilt my spaceship. This might help you out:

 public float ztilt = 1.0f;

 void Update()
 {
 rigidbody.rotation = Quaternion.Euler (0.0f, 0.0f, rigidbody.velocity.x * ztilt);
 }

you just need to find the right axises. in this case, x and y are not affected.

Edit: the rigidbody.velocity for my ship was calculated like this:

 rigidbody.velocity = movement * CameraSpeed;

you want to calculate it on you mouse or whatever. Hope it helps somehow.

avatar image Abdou23 · May 09, 2015 at 03:41 PM 0
Share

Problem is, my object doesn't have a rigidbody and i don't need it. $$anonymous$$y codes runs fine, it's just a matter of where to or which value t oexactly clamp.

avatar image _dns_ · May 09, 2015 at 04:08 PM 0
Share

Hi, using Debug.Drawline or Drawray usually helps a lot to figure out what happens in those kind of situations.

0 Replies

· Add your reply
  • Sort: 

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How to use mathf.clamp? 2 Answers

How to Clamp a Quaternion Camera Rotation? 0 Answers

Bizzare problem with limiting camera veritcal rotation 2 Answers

Clamping Vector3 2 Answers

How to clamp rotation in degrees in relation to another GameObject 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