hello fiends i have a cube that is constantly moving, whenever my cube collides with an object tagged “Wall”,it should change its rotation.it works with first collision but sometimes it won’t work :(..? whats the reason…?..need help frnds..thanks
There are a couple of things you could try to improve it. Move the object back to a known ‘safe’ position after a collision. Choose a new rotation relative to the current rotation after a collision.
var walkingSpeed : float = 10;
var safePosition : Vector3;
function Start ()
{
safePosition = transform.position;
}
function Update ()
{
safePosition = transform.position;
transform.Translate(transform.forward * walkingSpeed * Time.deltaTime);
}
function OnTriggerEnter (otherObject : Collider)
{
if(otherObject.transform.tag == "Wall")
{
// Move back to safe position
transform.position = safePosition;
var rotationAngle = Array(90,180,270);
var angleDelta = rotationAngle[Random.Range(0,rotationAngle.length)];
// Relative rotation
var eulerAngles : Vector3 = transform.eulerAngles;
eulerAngles.y += angleDelta;
transform.eulerAngles = eulerAngles;
}
}
Is there a reason you aren’t using Unity’s built in collision detection?
thanks very much for your time…i got my mistake…actually the problem was…for eg…if my object’s current rotation is 90 and according to my code on hitting the wall new random rotation that will be assigned will be 90,180 or 270.if new random rotation will also be 90 then my object tends to be in the same direction…i need to add few if statements to rectify it…i am a beginner in coding… and no there is no special reason for not using Unity’s built in collision detection. thanks very much