Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Paratrooper82 · Jan 28, 2018 at 02:35 PM · scripting problemcolliderunity 2dcollision2d

Unity2d move enemy in opposite direction on collision

Hey guys, I can't for the love of me figure out how to move my, let's say "Spider" enemy in the opposite direction once it starts moving and collides with a .tag named "Wall".

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class SpiderController : MonoBehaviour {
 
     public float moveSpeed; 
     private bool canMove; 
 
     public GameObject deatSpiderExplosion; 
 
     private Rigidbody2D myRigidBody; 
 
     //new with radius
     public Transform playerCheck;
 
     public float playerCheckRadius;
 
     public LayerMask whatIsPlayer;
 
 
 
 
 
     // Use this for initialization
     void Start () {
 
         myRigidBody = GetComponent<Rigidbody2D> ();
 
     }
     
     // Update is called once per frame
     void Update () {
 
 
 
         canMove = Physics2D.OverlapCircle(playerCheck.position, playerCheckRadius, whatIsPlayer);
 
         if (canMove) {
 
             myRigidBody.velocity = new Vector3 (-moveSpeed, myRigidBody.velocity.y, 0f);
 
         }
 
     }
 
     //void OnBecameVisible(){
 
         //canMove = true;
 
     //}
 
     void OnTriggerEnter2D(Collider2D other){
 
         if (other.tag == "KillPlane") {
 
             //Destroy (gameObject);
 
             gameObject.SetActive (false);
         
             Instantiate (deatSpiderExplosion, other.transform.position, other.transform.rotation);
 
         }
 
         if (other.tag == "Water") {
 
             //Destroy (gameObject);
 
             gameObject.SetActive (false);
 
             Instantiate (deatSpiderExplosion, other.transform.position, other.transform.rotation);
         }
     }
 
 
     void OnEnable(){
 
         canMove = false; 
 
     }
 
 }

So basically I have my spider move left once the player becomes visible. Within my world it will keep going left until it hits either the "killPlane" or "water" .tag, and it dies/gets disabled.

I've been trying to maybe make a OnCollisionEnter2D and change its x direction/movespeed to be positive, hence moving to the right, but it simply doesn't work. I don't necessarily know if it doesn't work because I put the movement into the Update function.

Any help would be much appreciated. :)

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image PersianKiller · Jan 28, 2018 at 02:38 PM 0
Share

hi

      if (other.tag == "Water") {
  
        moveSpeed*=-1;
      }

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Paratrooper82 · Jan 28, 2018 at 02:43 PM

Wow... My bad, instant brain fart immediately after I posted this question. I hope my own answer helps anyone out there trying to do the same with an enemy. Within the OnTriggerEnter2D just add what I show below.

 void OnTriggerEnter2D(Collider2D other){
 
         if (other.tag == "KillPlane") {
 
             //Destroy (gameObject);
 
             gameObject.SetActive (false);
         
             Instantiate (deatSpiderExplosion, other.transform.position, other.transform.rotation);
 
         }
 
         if (other.tag == "Water") {
 
             //Destroy (gameObject);
 
             gameObject.SetActive (false);
 
             Instantiate (deatSpiderExplosion, other.transform.position, other.transform.rotation);
         }
 
         if (other.tag == "Wall") {
 
             moveSpeed *= -1; 
 
         }
 
     }

The -1 in my case makes the moveSpeed positive, hence moving right on the X axis, and then it will collide again, making the moveSpeed negative again, hence moving left on the X axis. :)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

191 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to set box collider to a gizmos cube size 0 Answers

Physics.OverlapSphere does not detect any 2D Box Colliders! 0 Answers

[ Help!! ] Issue with position Z in object 1 Answer

Delete prefab clone when collided with the player, Unity 2D 1 Answer

How to create a "trap" when ground is tiled with collider. 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges