- Home /
 
Mathf.Clamp() not working when changing transform.localScale?; Difference between rigidbody.position and transform.position?;
Hi, I have 2 versions of a code snippet: ver1:
 void Awake () {
     rigid = GetComponent<Rigidbody>();
     trans = GetComponent<Transform>();
 }
 
 // Update is called once per frame
 void Update () {
     //MOVE
     movement = new Vector3(Input.GetAxis("P2Horizontal"),Input.GetAxis("P2Vertical"),0);
     rigid.velocity = movement;
     rigid.position = new Vector3(Mathf.Clamp(rigid.position.x, xMin, xMax),Mathf.Clamp(rigid.position.y, yMin, yMax),0);//RIGID.POSITION
     //SIZE
     size = Input.GetAxis("Size");
     //changes to sizeMin immediately, automatically gradually returns to sizeMax
     if (Input.GetButtonDown("Size"))
     {
         trans.localScale = minSize;
     }
     trans.localScale += new Vector3(0.1f, 0.1f, 0.1f)  * Time.deltaTime;
 
               ver2:
 void Awake () {
     rigid = GetComponent<Rigidbody>();
     trans = GetComponent<Transform>();
 }
 
 // Update is called once per frame
 void Update () {
     //MOVE
     movement = new Vector3(Input.GetAxis("P2Horizontal"),Input.GetAxis("P2Vertical"),0);
     rigid.velocity = movement;
     trans.position = new Vector3(Mathf.Clamp(rigid.position.x, xMin, xMax),Mathf.Clamp(rigid.position.y, yMin, yMax),0); //TRANS.POSITION
     //SIZE
     size = Input.GetAxis("Size");
     //changes to sizeMin immediately, automatically gradually returns to sizeMax
     if (Input.GetButtonDown("Size"))
     {
         trans.localScale = minSize;
     }
     trans.localScale += new Vector3(0.1f, 0.1f, 0.1f)  * Time.deltaTime;
 
               The only difference between them are the "rigid.position" and "trans.position". When I use trans.position, the clamp works, however the rigid case doesn't. Why is that? Also, if the snippet code is only like this:
short ver:
 void Awake () {
     rigid = GetComponent<Rigidbody>();
     trans = GetComponent<Transform>();
 }
 
 // Update is called once per frame
 void Update () {
     //MOVE
     movement = new Vector3(Input.GetAxis("P2Horizontal"),Input.GetAxis("P2Vertical"),0);
     rigid.velocity = movement;
     rigid.position = new Vector3(Mathf.Clamp(rigid.position.x, xMin, xMax),Mathf.Clamp(rigid.position.y, yMin, yMax),0);
 
               Then both trans.position and rigid.position work just fine with Clamp(). Why does changing the trans.localScale effect the Clamp()?
Thank you very much
Your answer
 
             Follow this Question
Related Questions
NullReferenceException: Object reference not set to an instance of an object CameraFollow.Update () 0 Answers
Help clamping player movement boundaries 1 Answer
transform.position return the wrong value 0 Answers
collider position wrong object position in collider 0 Answers
Change position of an object on a keystroke within a trigger only? 1 Answer