Search Unity

Smart missile that detects terrain

Discussion in 'Scripting' started by antiant777, Sep 19, 2014.

  1. antiant777

    antiant777

    Joined:
    Aug 29, 2013
    Posts:
    5
    Hi
    I have the script below to fly a missile towards my plane and destroy it. That's great. But.. the missile flies directly into the terrain if there is a hill in the way. How can I make it smart and have it smoothly fly over terrain mountains to get to the target. It would also need to rotate on its axis when going up a mountain for eg.
    I was going to use something like this..

    terrainHeightWhereWeAre = Terrain.activeTerrain.SampleHeight( transform.position );
    if (terrainHeightWhereWeAre > transform.position.y) {
    transform.position = new Vector3 (transform.position.x,
    terrainHeightWhereWeAre,
    transform.position.z);

    But for some reason transform.position.y is 0 when i read it's value ?

    any help would be great.
    Thanks!




    using UnityEngine;
    using System.Collections;

    public class missile_follow: MonoBehaviour {


    public float hover = 0.0f;
    public float terrainHeightWhereWeAre = 0.0f;
    public string searchTag;
    private GameObject closetMissle;
    private Transform target;
    public GameObject missileExpObject;

    void Start()
    {
    closetMissle = FindClosestEnemy();

    if(closetMissle)
    target = closetMissle.transform;
    }

    void Update()
    {

    if(target == null)
    {
    closetMissle = FindClosestEnemy();

    if(closetMissle)
    {
    target = closetMissle.transform;
    }
    }


    transform.LookAt(target);
    transform.Translate(Vector3.forward * 280.0f * Time.deltaTime);
    }

    GameObject FindClosestEnemy()
    {
    GameObject[] gos;
    gos = GameObject.FindGameObjectsWithTag(searchTag);

    GameObject closest = null;
    float distance = Mathf.Infinity;


    // terrainHeightWhereWeAre = Terrain.activeTerrain.SampleHeight( transform.position );




    Vector3 position = transform.position ;


    terrainHeightWhereWeAre = transform.position.y;

    foreach(GameObject go in gos)
    {
    Vector3 diff = go.transform.position - position ;
    float curDistance = diff.sqrMagnitude;

    if(curDistance < distance)
    {
    closest = go;
    distance = curDistance;
    }
    }

    return closest;
    }
    void OnTriggerStay(Collider otherObject)
    {
    if(otherObject.tag == "Enemy") //&& playerEntered)
    {

    var expPrefab = Instantiate(missileExpObject, this.transform.position, Quaternion.identity);
    Destroy (this.gameObject);
    Destroy(otherObject.gameObject);
    //Player.Score +=100;
    print ("explode enemy");
    }


    }

    }
     
  2. secondbreakfast

    secondbreakfast

    Joined:
    Jan 5, 2013
    Posts:
    98
    Try Raycasting in front the direction it is heading and if it sees the terrain, have it adjust it's path up
     
  3. antiant777

    antiant777

    Joined:
    Aug 29, 2013
    Posts:
    5
    Thanks mate, that put me on the right track :)
     
    GarthSmith likes this.
  4. elmar1028

    elmar1028

    Joined:
    Nov 21, 2013
    Posts:
    2,359
    Take a look at navmesh. Or A* pathfinding plugin (which is free)