- Home /
Simple - Blocking movement
Hello everyone. I've created 2 gameObjects.
One of them is the player and the other one is an obstacle, I've 4 buttons for movement, I want the object to move around but if he collides with the obstacle I want to prevent movement(While the movement is towards that obstacle, if movement is not towards the obstacle then allow the movement..)
Is there any quick, simple and clean solution to this?
What I've got so far is this script attached to the player.
public void OnTriggerEnter2D (Collider2D other) {
Debug.LogError ("TriggerEnter");
Debug.Log (other.gameObject.name);
canWalk = false;
}
public void OnTriggerExit2D (Collider2D other) {
Debug.LogError ("TriggerExit");
Debug.Log (other.gameObject.name);
canWalk = true;
}
But once it collides the player can't move so I can't really exit the collision....
Thank you for your time
Is there a reason you're not just using a conventional collider? Blocking movement is pretty much their job description.
By that, I mean a collider which is not set up as a trigger. By default, colliders block movement and triggers do not.
Oh, ok, so how would I set it up. I should only click isTrigger to 'false' and use onCollision, right?
Answer by Kaz_Yamof · May 22, 2014 at 07:53 PM
Well... when you sets canWalk = false in OnTriggerEnter is logical that you can't walk anymore, according your movement logic.
You should try implement something like canWalkFront, canWalkBack, if you don't want to use boxcolliders to control this.
public void OnTriggerEnter2D (Collider2D other)
{
canWalkFront = false;
canWalkBack = true;
}
public void OnTriggerExit2D (Collider2D other)
{
canWalkFront = true;
}
@daybson.paisante
Thanks for your time but what I simply did was turning is$$anonymous$$inematic to true and turn isTrigger to false :D
Your answer
Follow this Question
Related Questions
Need insight on strange (simple) 2D object transform bug 0 Answers
If you change the rigidBody2D.position, you also need to change the gameObject.transform.position? 0 Answers
Take final position after a force applied to a GameObject 1 Answer
Detect a GameObject Passing Between Other Two GameObjects? 4 Answers
Player movement in the direction of rotation INACCURATE? 3 Answers