Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by Killbert · Mar 25, 2014 at 06:56 PM · proximity

Mutiple OnTriggerEnter Colliders in same script

I am trying to create a proximity detector in Unity but am facing a bit of a challenge. My code is C#. Note: I edited my original question for clarity.

In my game, the objective is to fly from city to city. The player helicopter flies into a target cube (representing the city). That trigger event works fine. On the collisison, the target cube gameobject (city) is deactivated and a new target cube (city) is spawnend elsewhere on the map.

This all works fine in the below script, BUT I want to make it more challenging. I want the target cube gameobject (city) to spawn only if the player gameobject (helicopter) is in the vicinity of the target cube gameobject (city).

See the code comments below for where in my script I am having difficulties.

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class PlayerController : MonoBehaviour 
 {
 public float speed;
 public float smooth = 2.0F;
 public GUIText countText;
 public GUIText targetCity;
 private int count;
 public GameObject cityPrefab;
 List<MyCity> mycities;
 
 void Start()
 {
 
 
     mycities = new List<MyCity>();
 
     mycities.Add( new MyCity("Maastricht", 3.635356F, 0.6F, -22.77141F )); 
     mycities.Add( new MyCity("Breda", -6.013169F, 0.6F, -10.64377F));
     mycities.Add( new MyCity("Amsterdam", -4.768597F, 0.6F, 2.610285F));
     mycities.Add( new MyCity("Assen", 12.89446F, 0.6F, 13.20904F));
     mycities.Add( new MyCity("Groningen", 12.89446F, 0.6F, 17.18825F));
     mycities.Add( new MyCity("Utrecht", -2.468696F, 0.6F, -2.110941F));
     mycities.Add( new MyCity("Leeuwarden", 4.773322F, 0.6F, 16.8759F));
     mycities.Add( new MyCity("Nijmegen", 5.137639F, 0.6F, -6.816391F));
     mycities.Add( new MyCity("Rotterdam", -9.139206F, 0.6F, -4.898618F));
     mycities.Add( new MyCity("Zwolle", 7.717577F, 0.6F, 5.111133F));
     mycities.Add( new MyCity("Den Helder", -6.195175F, 0.6F, 12.58271F));
     mycities.Add( new MyCity("Eindhoven", 1.262518F, 0.6F, -13.01812F));
     mycities.Add( new MyCity("Den Haag", -11.0655F, 0.6F, -2.512064F));
     mycities.Add( new MyCity("Arnhem", 5.79248F, 0.6F, -3.911978F));
 
 
 
     // scoring points & display on screen (works)
     count = 0;
     SetCountText ();
     SetTargetCity ();
 }
 
 
 
 
     void SetTargetCity ()
 {
             var randomCity = mycities [Random.Range (0, mycities.Count - 1)];
             targetCity.text = "Fly to: " + randomCity.name.ToString ();
 
     // HERE IS WHERE I NEED TO CREATE AN  OnTriggerEnter function, so that the below line is only executed if my helicopter is in the vicinity of the cityPrefab gameobject. The problem is that firther down in the script, I already use an OnTriggerEnter on the same object.
 
                     GameObject instancedCity = (GameObject)GameObject.Instantiate (cityPrefab);
                     instancedCity.transform.position = new Vector3 (randomCity.xcor, randomCity.ycor, randomCity.zcor);
 
     }
 
 // Player Movement (works)
 void FixedUpdate ()
 {
     float moveHorizontal = Input.GetAxis("Horizontal");
     float moveVertical = Input.GetAxis("Vertical");
 
     Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
 
     Vector3 moveDirection= new Vector3 (moveHorizontal, 0, moveVertical);  
     if (moveDirection != Vector3.zero){
         Quaternion newRotation = Quaternion.LookRotation(moveDirection * -1);
         transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, Time.deltaTime * smooth);
 
         rigidbody.AddForce(movement * speed * Time.deltaTime);
 
     }
 
 }
     // This part below works fine, but only takes care of deactivating the city cube instance after the helicopter has flown into it.... 
 
 void OnTriggerEnter(Collider other)
 {
             if (other.gameObject.tag == "City") {
                     other.gameObject.SetActive (false); 
 
         count = count + 1;
 
         SetCountText ();
         SetTargetCity ();
             }
 }
 
 void SetCountText ()
 {
     countText.text = "Passengers Picked up: " + count.ToString();
 }

}

Hope the above makes sense. I have made an empty gameobject as a child inside my Player object. The child has a Rigidbody and Boxcollider. The child is named ProxDect. How do I specify a OntriggerEnter for that particular box collider? And can I even use that in this part of my script as an if statement? Or am I trying to combine too many things in one function?

Comment
Add comment · Show 4
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 Dblfstr · Mar 25, 2014 at 08:04 PM 0
Share

I'm sorry. This is a bit hard to understand. But, I do not believe you can have two OnTriggerEnter() functions in one script. You CAN do multiple things with one trigger, or have different child objects with triggers and their own scripts.

Also, you should set the instance position in the Instantiate method. Also, just use

 GameObject instancedCity =(GameObject)Instantiate(cityPrefab,position, rotation);
avatar image Killbert · Mar 26, 2014 at 12:32 AM 0
Share

I am sorry, you are right...the above is perhaps a bit unclear. I will edit my original question.

avatar image getyour411 · Mar 26, 2014 at 01:00 AM 0
Share

First, grats on doing that List tut and taking the time to learn this stuff, much better than grabbing snippets without understanding what they mean.

AFAI$$anonymous$$, you cannot have multiple OnTriggers at the same level in the hierarchy; one way to solve that is with parent/child gameobjects where you can have triggers with different radius/layers/behavior. You could also use a Vector3.Distance check ins$$anonymous$$d.

avatar image Killbert · Mar 26, 2014 at 02:04 AM 0
Share

Getyour411....thanks...although I must admit that I already did the List tutorial before I asked my previous question re the random selection. That tut helped out in the end, but only after a somebody helped me out with a snippet...I understand what you are saying, but sometimes snippets in combination with a tut are what is needed to truly understand something and put it in perspective. At least that is how I learn my stuff. But granted, after your comment, I dove into the tut more deeply and in the end that was the right path to follow!

On topic: I was actually trying to do exactly what your are suggesting. But I am stuck. $$anonymous$$y player object has a boxcollider and rigidbody, That takes care of the ontriggerenter function that deactivates the object to which my player object collides. So far so good. Now the problem is that I want to spawn a new city object in the function SetTargetCity. But only spawn that object once my player object is in close proximity to that object. I have added a new child object in my player object. The child holds a boxcollider and a rigidbody. The child name is ProxDect. I was hoping I could do something like OntriggerEnter(ProxDect.Collider other) but that doesn't work. So Vector3.Distance might do the trick ins$$anonymous$$d. But what is the snippet for that? ;-)

In my code it would need to be:

If '$$anonymous$$y player object' Vector3.Distance to the 'city cube object' vector3 postion is less than X, Do the below:

GameObject instancedCity = (GameObject)GameObject.Instantiate (cityPrefab); instancedCity.transform.position = new Vector3 (randomCity.xcor, randomCity.ycor, randomCity.zcor);

However, since the distance check can only be done after the city object has been placed on the map, I am left with a chicken - egg situation....

0 Replies

· Add your reply
  • Sort: 

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

21 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

Related Questions

How to create an aura with selective collision detection 2 Answers

Preventing gameobjects from being spawned too close to each other 2 Answers

Getting a proximity mine to instantiate explosion to kill enemies 1 Answer

How to detect in a shader, if two objects are close 2 Answers

Order Objects Based on Proximity 3 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