Search Unity

How make an object move to the player's position and then land in the ground?

Discussion in 'Scripting' started by the_Bad_Brad, Sep 26, 2017.

  1. the_Bad_Brad

    the_Bad_Brad

    Joined:
    Nov 2, 2014
    Posts:
    278
    This is for a supply drone that will arrive when the player hits a button.

    I got the approach code but I have no idea how to make it land

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class SupplyDrop : MonoBehaviour {
    5.    public Transform dropzone;
    6.    public float speed;
    7.    void Update() {
    8.        float step = speed * Time.deltaTime;
    9.        transform.position = Vector3.MoveTowards(transform.position, dropzone.position, step);
    10.    }
    11. }
     
  2. Rob21894

    Rob21894

    Joined:
    Nov 21, 2013
    Posts:
    309
    Since you already have the code for it approaching the drop zone, can you not check the distance between and the drone and the dropzone position then if its within a certain range then proceed to move downwards

    So i advise looking up Vector3.Distance , and a raycast to check if u hit the floor to stop moving
     
    the_Bad_Brad likes this.
  3. the_Bad_Brad

    the_Bad_Brad

    Joined:
    Nov 2, 2014
    Posts:
    278
    I like the raycast idea i didn't think of that. So I will have a raycast set to 5 so when it hits the floor collider, i just disable the script. Also use for my helicopter landing. Alright I'm set...
     
  4. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,550
    Instead of making your dropzone position up
    You could also just put your drop zones at the actual landing spot, but don't start setting your Y value until your X and Z values actually reach the drop zone.

    Or you could even make a DropZone class for your drop zones that you would "GetComponent<DropZone>()" (only once to get the reference and hold it), and then your movement code would have access to whatever properties you had in it, like a "float safeEntryHeightOffset;" that your movement code would use until it was directly above the drop zone, and then start flying directly down to the proper position.