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 /
This question was closed May 06, 2016 at 07:38 PM by TJRiker for the following reason:

I was able to solve the problem myself, but it was in a much different way that probably would not be helpful to others with the same type of question.

avatar image
0
Question by TJRiker · May 06, 2016 at 10:53 AM · scripting problem2d gamecollider2ddodge

Help with making an object dodge other objects?

Hello, I'm very new here, and as my first project, I've been making an Asteroids style game. But I've come across a little problem.

I have an Alien object in the game that flies around and shoots at the player, but I am trying to get him to also try and dodge asteroids and the player bullets.

What I have is the Alien object, and that has a child object that is just a CircleCollider2D. If an object comes within the CircleCollider2D of the child object, it should cause the Alien to move in the opposite direction. But what actually happens is if an object collides with the CircleCollider2D, the Alien explodes.

The AlienScript is the code that goes directly on the Alien Object. That is here :

using UnityEngine;

using System.Collections;

public class AlienScript : MonoBehaviour {

 public float MinForce = 20f;
 public float MaxForce = 40f;
 public float DirectionChangeInterval = 1f;
 public float ShotInterval = 1f;
 public GameObject AlienBullet;
 public GameObject AlienExplosion;

 private GameObject ship;
 private float directionChangeInterval;
 private float shotInterval;


 // Use this for initialization
 void Start ()
 {
     directionChangeInterval = DirectionChangeInterval;
     shotInterval = ShotInterval;
     Push();
 }
 
 // Update is called once per frame
 void Update ()
 {
     directionChangeInterval -= Time.deltaTime;
     if(directionChangeInterval < 0)
     {
         Push();
         directionChangeInterval = DirectionChangeInterval;
     }
     shotInterval -= Time.deltaTime;
     if(shotInterval < 0)
     {
         Shoot();
         shotInterval = ShotInterval;
     }
 }

 void Push()
 {
     float force = Random.Range(MinForce, MaxForce);
     float x = Random.Range(-1f, 1f);
     float y = Random.Range(-1f, 1f);

     GetComponent<Rigidbody2D>().AddForce(force * new Vector2(x, y));
 }

 void Shoot()
 {
     ship = GameObject.FindGameObjectWithTag("Ship");

     if (ship != null)
     {

         float angle = (Mathf.Atan2(ship.transform.position.y - transform.position.y, ship.transform.position.x - transform.position.x) - Mathf.PI / 2) * Mathf.Rad2Deg;

         Instantiate(AlienBullet, transform.position, Quaternion.Euler(new Vector3(0f, 0f, angle)));
     }
 }

 void OnTriggerEnter2D(Collider2D collider)
 {
     if(collider.gameObject.tag == "ShipBullet")
     {
         Instantiate(AlienExplosion, transform.position, new Quaternion());
         Destroy(collider.gameObject);
         Destroy(gameObject);
     }
     else if(collider.gameObject.tag == "Asteroid")
     {
         Instantiate(AlienExplosion,transform.position, new Quaternion());
         Destroy(gameObject);
     }
 }

}

And the DodgeScript goes on the child of the Alien, along with the CircleCollider2D :

using UnityEngine; using System.Collections;

public class DodgeScript : MonoBehaviour {

 public GameObject AlienShip;
 public float DodgeCooldown = 0.5f;

 private float Force;
 private float dodgeCooldown;

 // Use this for initialization
 void Start ()
 {
     Force = AlienShip.GetComponent<AlienScript>().MaxForce;
     dodgeCooldown = -1f;
 }
 
 // Update is called once per frame
 void Update ()
 {
     dodgeCooldown -= Time.deltaTime;
 }

 void OnTriggerEnter2D(Collider2D collider)
 {
     if(dodgeCooldown < 0)
     {
         AlienShip.GetComponent<Rigidbody2D>().AddForce(-AlienShip.GetComponent<Rigidbody2D>().velocity.normalized * Force);
         dodgeCooldown = DodgeCooldown;
     }
 }

}

So I think the problem is that somehow the collider on the Alien and the DodgeCollider collider are somehow both triggering as the Alien collider, and therefore both killing the alien.

Is there any way for the DodgeCollider to not kill the Alien, but instead actually have him dodge?

Sorry for the really long question, but thanks for all of your help!

Thank you!

Comment
Add comment
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

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

SImple but IMPOSSIBLE Enemies keep dying way ahead of time ! 0 Answers

Display UI Panel on 2D Box Collider Trigger 0 Answers

Problem with hitboxes and hurtboxes 0 Answers

Main Menu Help! 1 Answer

Gameobject goes missing after second iteration of event 1 Answer


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