Hello ,
I am super noob and new to programming.
I am trying to prevent my Player from going beyond specific X & Y coordinates.
Any suggestions?
Thank you for your time!
Hello ,
I am super noob and new to programming.
I am trying to prevent my Player from going beyond specific X & Y coordinates.
Any suggestions?
Thank you for your time!
Use the Clamp method of the Mathf class, this allows you to clamp between a min and max value:
If you are using a physics based character controller you can just add invisible colliders.
For any future errors, check at the error code and look it up. It usually explains why and how to fix it. (error CS1612 , it’s there for a reason)
A Vector3 is a struct and you therefore can’t assigning its values directly. You need to create your own instance of Vector3 , set the values and then feed it back to transform.position.
transform.position = new Vector3(transform.position.x , Mathf.Clamp(transform.position.y, -3, 0) ,transform.position.z);
or
Vector3 tmp = new Vector3(transform.position.x , Mathf.Clamp(transform.position.y, -3, 0) ,transform.position.z);
transform.position = tmp;