pushback player on collision
Okay I have a 2D topdown game where my player moves anywhere by mouse click. I want my enemies to push back my player on collision. I have a movement script on my enemies as well. Maybe the problem is that i need to disable the movement script on my player once it collides with the enemy so that my player would get pushed back then re-enable it after it get pushed back. But how would I do that? I need a script that bounce my player of my enemy on collision! I've looked on Google but I couldn't find any that match my description. Could anybody help me please! Thank you! here's my player movement script:
   using UnityEngine;
   using System.Collections;
   using UnityEngine.UI;
   public class MainPlayer : MonoBehaviour {
 public float speed = 5f; 
 public Text ScoreText;
 public AudioClip Coinsound;
 private Vector3 target;
 private int Score;
 
 void Start () 
 {
     target = transform.position;
     Score = 0;
     SetScoreText ();
 }
 void OnTriggerEnter2D(Collider2D other)
 {
     if (other.gameObject.CompareTag ("Pick Up")) 
     {
         other.gameObject.SetActive (false);
         Score = Score + 1;
         SetScoreText ();
         AudioSource.PlayClipAtPoint (Coinsound, transform.position);
     }
 }
 
 void SetScoreText ()
 {
     ScoreText.text = "Score: " + Score.ToString ();
 }
 void Update () {
     if (Input.GetMouseButtonDown (0)) {
         target = Camera.main.ScreenToWorldPoint (Input.mousePosition);
         target.z = transform.position.z;
     }
     transform.position = Vector3.MoveTowards (transform.position, target, speed * Time.deltaTime);
 }
 void LateUpdate () {
     var left = Camera.main.ViewportToWorldPoint (Vector3.zero).x;
     var right = Camera.main.ViewportToWorldPoint (Vector3.one).x;
     var top = Camera.main.ViewportToWorldPoint (Vector3.zero).y;
     var bottom = Camera.main.ViewportToWorldPoint (Vector3.one).y;
     float x = transform.position.x, y = transform.position.y;
     if (transform.position.x <= left + GetComponent<Renderer> ().bounds.extents.x) {
         x = left + GetComponent<Renderer> ().bounds.extents.x;
     } else if (transform.position.x >= right - GetComponent<Renderer> ().bounds.extents.x) {
         x = right - GetComponent<Renderer> ().bounds.extents.x;
     }
     if (transform.position.y <= top + GetComponent<Renderer> ().bounds.extents.y) {
         y = top + GetComponent<Renderer> ().bounds.extents.y;
     } else if (transform.position.y >= bottom - GetComponent<Renderer> ().bounds.extents.y) {
         y = bottom - GetComponent<Renderer> ().bounds.extents.y;
     }
     transform.position = new Vector3 (x, y, transform.position.z);
 }
}
Answer by Statement · Oct 24, 2015 at 10:39 AM
Maybe the problem is that i need to disable the movement script on my player once it collides with the enemy so that my player would get pushed back then re-enable
Yes.
 if (canMove)
     transform.position = Vector3.MoveTowards (transform.position, target, speed * Time.deltaTime);
I don't know how you want to do the pushback though.
 void OnTriggerEnter2D(Collider2D other)
  {
     if (canMove && other.CompareTag ("Hurt")) 
     {
         StartCoroutine(Knockback());
     }
 }
 IEnumerator Knockback() {
     canMove = false;
     // coroutine implementation for knocking back the player etc... 
     // maybe play animation and wait for animation to finish?
     canMove = true;
     yield break;
 }
Thank you for replaying back, i'll give it a try!!!
Um, I've tried it out, but an error comes up saying "The name `can$$anonymous$$ove' does not exist in the current context" Then what should i change it to? Thank you!! @Statement
@Creative Inventory You need to make a variable for can$$anonymous$$ove.
 bool can$$anonymous$$ove = true;
Right up the top of your class where your other variables are like target and score.
I've tried it out, it works perfectly, but I don't think it's what I'm looking for. What I want is that once my player collides with an enemy, my player should bounce back, kinda like when sonic (2D) bounce back when he come in contact with this:
![alt text][1] [1]: /storage/temp/56906-sonic-bounce.jpg
I want my player to do the same whilst my enemies moves as well.
Sorry if I didn't mention it before!!! Thank you anyway!! @Statement 
I don't see the problem. I dont know how that button interacts with sonic. But anyway, if you hit a monster, play an animation to bounce you back.
Your enemies can move as well. Just make sure they have a rigidbody (possibly $$anonymous$$inematic if you are not using physics responses) and a collider (possibly a trigger).
Once you detected you hit an enemy, do something. Play an animation. $$anonymous$$ake a puff of smoke. Enable physics and make a ragdoll. Anything goes.
Your answer
 
 
             Follow this Question
Related Questions
Death from collision 1 Answer
Is there a way to make an Object pass over a Text? 0 Answers
What is the best way to script a trigger that moves a object from point a to point b in C# (unity 5) 1 Answer
How to put an object in front of the player when clicked 0 Answers
I'm trying to create an object I can pick up and move in a 2D platformer. 0 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                