Question by 
               $$anonymous$$ · May 15, 2018 at 09:51 PM · 
                c#2dpositionchanging  
              
 
              position the player on the same position as an object
Hi, im building a 2D Game and I want that the player position is changing to the position of the object of which the players gets hit. The object position shall not change. Here is my C# Script Code:
 public class CollisionDamage : MonoBehaviour
 {
     public float damage = 1;
 
     void OnTriggerEnter2D(Collider2D collision)
     {
         if (collision.CompareTag("Player"))
         {
             collision.SendMessage("ApplyDamage", damage);
             // Here I tried to change the position of the player
             Walk.walk.transform.position = new Vector3(MummyWalk.mummywalk.transform.position.x, MummyWalk.mummywalk.transform.position.y, MummyWalk.mummywalk.transform.position.z);
 
         }
     }
 
     void OnTriggerStay2D(Collider2D collision)
     {
         if (collision.CompareTag("Player"))
         {
             collision.SendMessage("ApplyDamage", damage);
          
             Walk.walk.transform.position = new Vector3(MummyWalk.mummywalk.transform.position.x, MummyWalk.mummywalk.transform.position.y, MummyWalk.mummywalk.transform.position.z);
         }
     }
 }
 
               Here is the script of the players walk:
  public static Walk walk;
     private Animator anim;
     public float moveSpeed;
     private bool moving;
     private Vector2 lastMove;
 
     void Start()
     {
         anim = GetComponent<Animator>();
     }
 
     void Update()
     {
         moving = false;
         if (Input.GetAxisRaw("p1_Horizontal") > 0.5f || Input.GetAxisRaw("p1_Horizontal") < -0.5f)
         {
             transform.Translate(new Vector3(Input.GetAxisRaw("p1_Horizontal") * moveSpeed * Time.deltaTime, 0f, 0f));
             moving = true;
             lastMove = new Vector2(Input.GetAxisRaw("p1_Horizontal"), 0f);
         }
         if (Input.GetAxisRaw("p1_Vertical") > 0.5f || Input.GetAxisRaw("p1_Vertical") < -0.5f)
         {
             transform.Translate(new Vector3(0f, Input.GetAxisRaw("p1_Vertical") * moveSpeed * Time.deltaTime, 0f));
             moving = true;
             lastMove = new Vector2(0f, Input.GetAxisRaw("p1_Vertical"));
         }
         anim.SetFloat("MoveX", Input.GetAxisRaw("p1_Horizontal"));
         anim.SetFloat("MoveY", Input.GetAxisRaw("p1_Vertical"));
         anim.SetBool("moving", moving);
         anim.SetFloat("LastMoveX", lastMove.x);
         anim.SetFloat("LastMoveY", lastMove.y);
 
               And here is the script of the enemy which has the collider box:
 public class MummyWalk : MonoBehaviour
 {
     public static MummyWalk mummywalk;
     public float moveSpeed;
     private bool moving;
     private Transform target;
     public Animator anim;
     public Vector2 firstPos;
     
     void Start ()
     {
         target = GameObject.FindGameObjectWithTag("Player").GetComponent<Transform>();
         anim = GetComponent<Animator>();
 
         
 
    
     }
 
     void Update()
     {
        
         moving = false;
         firstPos = transform.position;
         
         StartCoroutine("WaitOne");
         if (Vector2.Distance(transform.position, target.position) > .7)
         {
             transform.position = Vector2.MoveTowards(transform.position, target.position, moveSpeed * Time.deltaTime);
             moving = true;
         }
         anim.SetBool("moving", moving);
         if(transform.position.x < firstPos.x )
         {
             sr.flipX = true;
         }
         else if(transform.position.x > firstPos.x)
         {
             sr.flipX = false;
         }
 
  
     }
 
     IEnumerator WaitOne()
     {
         yield return new WaitForSeconds(1);
     }
 }
 
              
               Comment
              
 
               
              Your answer
 
             Follow this Question
Related Questions
Changing Player Position when hit by collider 0 Answers
Game Object doesn't instantiate at Mouse Position 3 Answers
Help with Unity Networking and teleporting player 0 Answers
Random position, overlap problem. 1 Answer
Trying to Generate Different Random Values for Position of Game Object Instances [C#] 1 Answer