简体   繁体   中英

Proximity detector in Unity

I am trying to create a proximity detector in Unity but am facing a bit of a challenge. My code is C#. <>

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 further 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();
    }
} 

How do I do that? 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?

So, in the box colider component there is a little box that says Trigger. Check that. Then the object that you want the former to collide with make sure they have a collider and create a new script. Add this method:

void OnTriggerEnter(Collider obj){
    print ("Object Hit");
}

Then do whatever else you want in this script. Attach this script to the object that does not have the trigger. Usually, If you have a player running into a ball, lets say, and when he hits the ball is disappears. You will want the ball to be the trigger and the method above on the player to hide the ball. ps. obj would the be trigger object. So going Destroy(obj); would destroy the ball not the player. IF attached to the player.

Then if you want a proximity you can just change the size of the collider on the player. GL you'll figure it out.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM