- Home /
Stop moving when colliding. 2D
Hello everyone, hope you're doing awesome!
So, I'll make this quick.
Im moving a sprite using this script.
moveSprite.cs (Attached to moving object).
if(buttonIsPushed)
{
transform.Translate (Vector3.left * speed * Time.deltaTime);
//Clamping user from movign outside map bounds.
tempPosition = transform.position;
tempPosition.y = Mathf.Clamp(tempPosition.y, minY, maxY);
tempPosition.x = Mathf.Clamp(tempPosition.x, minX, maxX);
transform.position = tempPosition;
//Button false
gui.leftClicked = false;
}
Quick scan.
1.Transform moves sprite, 2.Map clamp so user doesn't go outside map bounds.
Now I want to restrict the sprite from moving if he's colliding with another gameObject.
How could I achieve this?
Thanks for your tips & help!
Have an awesome day!
:D
Answer by robertbu · May 20, 2014 at 03:45 PM
You can use OnCollisionEnter2D() and OnCollisionExit2D() to set a flag line 'isColliding'. Then change line 1 to:
if (buttonIsPushed && !isColliding)
Alternately, if your sprite is a square or a circle (or you can live with a square or circle detection), you can use Physics.OverlapCircle() or Physics.OverlapArea() to detect if your object is colliding.
Hey! I've added this to the same script:
void OnCollisionEnter2D (Collision2D other) {
Debug.LogWarning ("TriggerEnter");
Debug.Log (collider.gameObject.name);
Debug.Log (other.gameObject.name);
canWalk = false;
}
void OnCollisionExit2D (Collision2D other) {
Debug.LogWarning ("TriggerExit");
canWalk = true;
}
Added boxcollider 2d component to the player and obstacle..but neither is being called...
Do you have a non-kinematic Rigidbody2D on either object? You use the word 'Trigger' in your code. If you have the isTrigger flag set, you want to use OnTriggerEnter2D() and OnTriggerExit2D().
@robertbu sorry for late repliance, and thanks for your time.
I added BoxCollider2D component to the player, the obstacle and clicked 'isTrigger'
Then changed the function to OnTriggerEnter2D() { Debug.Log("Enter")}
Still nothing.
Where can I see if there's something like non-kinematic. As far as Im concerned I haven't added any component of the likes. Just boxCollider, transform and sprite renderer.
Is it possible that its because they're on different sorting layers on the sprite renderer?
All this is inside the moving script. Should I create a script and add it to both? or should it run either way?
Ok, so after I sent my message I forgot to add RigidBody, I added it. Now my question is. From the original code, I added the same with OnTrigger, why is OnCollisionEnter not being called? Plus another question to your solution.
When player touches object, 'isColliding' is true. So I can't set isColliding to false because player can't move.(Due to it colliding with object). What would you do to get over this problem...?
Thanks in advance, you always have time for us dumdums haha, cheers! :D
@$$anonymous$$ii hey can tell me in brief what scenario you are trying to achieve. I would love to help. Because from above conversations i cant get clear idea abt the scenario.