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
1
Question by Coreyf716 · Dec 31, 2012 at 05:08 AM · vector3clamp

Vector3 Clamp

I tried limiting a Vector3 with Mathf.Clamp, but it caused a jittery effect on the gameobject.

 public var moveSpeed : float;
 public var minimumX : float;
 public var maximumX : float;
 public var minimumY : float;
 public var maximumY : float;
 public var resetTimerX : float;
 public var resetTimerY : float;
 public var defaultPos : Vector3;
 public var newPos : Vector3;
 
 function Start() {
     defaultPos = gameObject.transform.localPosition;
     newPos = gameObject.transform.localPosition;
 }
 
 function Update() {
     newPos.x += Input.GetAxis("Mouse X") * moveSpeed * Time.deltaTime;
     newPos.y += Input.GetAxis("Mouse Y") * moveSpeed * Time.deltaTime;
     gameObject.transform.localPosition = newPos;
     newPos.x = Mathf.Clamp(newPos.x, minimumX, maximumX);
     newPos.y = Mathf.Clamp(newPos.y, minimumY, maximumY);
 }

I found that it's the clamp causing this. Can vector3's not be clamped? What other meathods can be used to limit position?

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

3 Replies

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

Answer by Bunny83 · Dec 31, 2012 at 04:02 PM

Sure there's a jitter since you clamp after you assigned the values...

So the change that happens in the current frame is always transferred to the objects position. You have to clamp before you assign the position to the object.

 function Update()
 {
     newPos.x += Input.GetAxis("Mouse X") * moveSpeed * Time.deltaTime;
     newPos.y += Input.GetAxis("Mouse Y") * moveSpeed * Time.deltaTime;
     newPos.x = Mathf.Clamp(newPos.x, minimumX, maximumX);
     newPos.y = Mathf.Clamp(newPos.y, minimumY, maximumY);
     gameObject.transform.localPosition = newPos;
 }
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 Bunny83 · Dec 31, 2012 at 04:40 PM 0
Share

$$anonymous$$eep in $$anonymous$$d that newPos is a Vector3 so it contains also a z value which is never changed but always assigned. You can not change the z value from another script unless you update newPos every frame:

 newPos = gameObject.transform.localPosition;
 // apply your changes to newPos and clamp it afterwards.
 gameObject.transform.localPosition = newPos;
avatar image Coreyf716 · Dec 31, 2012 at 04:53 PM 0
Share

The amazing things that I can do when I think :) thanks man so much for the help!

avatar image
1

Answer by alexfeature · Dec 31, 2012 at 07:25 AM

Hey Corey,

The reason for this is probably coming form interference of other scripts on the position of this object.

Make sure that movement of this object is handled only from one script, and preferably one function.

Also, keep in mind that if you make the object move with physics (ie have a rigid body and apply force etc.) Messing with that object's movement in scripts will cause jitters.

This is because the physics engine is trying to move it at the same time as your are.

Hope this helps, Alex

Comment
Add comment · Show 3 · 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 Coreyf716 · Dec 31, 2012 at 02:52 PM 0
Share

You're right in saying that I have another script controlling the object's movement, but it's a long a different axis. After none of that worked, I tried controlling the object's localPosition directly rather than positioning it by a Vector3 and it worked.

avatar image alexfeature · Dec 31, 2012 at 04:44 PM 0
Share

Cool. Problem solved.

You should mark the question as answered then.

avatar image Coreyf716 · Dec 31, 2012 at 04:49 PM 0
Share

Well yeah haha, but it's more complicated than it has to be that way.

avatar image
1

Answer by RJ45 · Aug 20, 2016 at 08:53 AM

/// /// Simple helper to clamp a range to vector3 /// public class Vector3Range { public Vector3 Min; public Vector3 Max;

     /// <summary>
     /// Clamp a range
     /// </summary>
     public Vector3 Clamp(ref Vector3 value)
     {
         value.x = Mathf.Clamp(value.x, Min.x, Max.x);
         value.y = Mathf.Clamp(value.y, Min.y, Max.y);
         value.z = Mathf.Clamp(value.z, Min.z, Max.z);
         return value;
     }
 }

For most usages you would want a simple static with min and max as parameters rather than a class but I had a use.

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

11 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

Related Questions

object transform and clamp in local direction 0 Answers

Limit Camera movement by distance from target 1 Answer

How to limit a movement vector without affecting gravity 2 Answers

Clamping Vector3 2 Answers

Clamp X position of object 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