- Home /
oncollision change mathf.clamp values
I'm trying to figure out how to change mathf.clamp values once a player hits a trigger.
but I have no idea how, my attempt looked something like this:
var shouldConstraint : boolean = true;
var bullet : Rigidbody;
function Update () {
if (shouldConstraint){
transform.position.x = Mathf.Clamp(transform.position.x, -3.9, 3.9);
transform.position.z = Mathf.Clamp(transform.position.z, -18.8,19);
}
}
function OnTriggerEnter(otherObject: Collider){
if(otherObject.gameObject.tag == "playercollision"){
transform.position.x = Mathf.Clamp(transform.position.x, -3.9, 3.9);
transform.position.z = Mathf.Clamp(transform.position.z, 13.7,19);
shouldMove = false;
}
but it doesn't quite work, but it's the only idea I can think of, any ideas?
Answer by aldonaletto · Jun 30, 2011 at 10:49 PM
I'm not sure about exactly what you're trying to do, but if all you want is to change the limits applied by Mathf.Clamp, you can store them in variables and change the limits when you want:
var minX = -3.9;
var maxX = 3.9;
var minZ = -18.8;
var maxZ = 19.0;
function Update () {
if (shouldConstraint){
transform.position.x = Mathf.Clamp(transform.position.x, minX, maxX);
transform.position.z = Mathf.Clamp(transform.position.z, minZ, maxZ);
}
}
function OnTriggerEnter(otherObject: Collider){
if(otherObject.gameObject.tag == "playercollision"){
minZ = 13.7;
shouldMove = false;
}
}
I really didn't get what's your intention, but this way you can set the limits whenever you need. Hope it helps!
Your answer
Follow this Question
Related Questions
Can someone explain this MouseMove script to me :) 1 Answer
tangents from angle gives weird results 1 Answer
float precision, again 0 Answers
How to find inverse cos 1 Answer