Search Unity

Non Physics Hover

Discussion in 'Getting Started' started by CyberInteractiveLLC, Mar 20, 2019.

  1. CyberInteractiveLLC

    CyberInteractiveLLC

    Joined:
    May 23, 2017
    Posts:
    307
    Hi, i'm trying to implement a hover that is not physcis based, I have following simple code which just sets a ray down with hoverheight, if it's hit then move the object up roughly to the hoverHeight above the ground and if not then just start to fal, my problem with this code is if the object is going fast forward and the ground normal is changing, the hover will be very sluggish. i'm looking for ideas to improve it without using physics

    Code (CSharp):
    1. void Hover()
    2. {
    3.       Ray ray = new Ray (transform.position, -transform.uo);
    4.       RaycastHit hitInfo;
    5.       isOnGround = Physics.Raycast(ray, out hitInfo, hoverHeight, groundLayer);
    6.  
    7.      if (isOnGround)
    8.      {
    9.           Fall = 0;
    10.           float up = (hoverHeight - 1) - hitInfo.distance;
    11.           if (hitInfo.distance > 2.0f)
    12.               transform.position = transform.position + transform.up * up * Time.deltatime;
    13.      }
    14.      else
    15.      {
    16.          Fall += 1.0f;
    17.          transform.position = transform.position + Vector3.up * -Fall * Time.deltatime;
    18.      }
    19. }
    20. }
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    What you probably want for something like this is a PID Loop.