Search Unity

How can I make the drone float in air idle ?

Discussion in 'Scripting' started by DubiDuboni, May 24, 2020.

  1. DubiDuboni

    DubiDuboni

    Joined:
    Feb 5, 2019
    Posts:
    131
    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class FloatInAir : MonoBehaviour
    7. {
    8.    public float amplitude;          //Set in Inspector
    9.    public float speed;              //Set in Inspector
    10.  
    11.    private float tempValY;
    12.    private float tempValX;
    13.    private Vector3 tempPos;
    14.  
    15.    void Start()
    16.    {
    17.        tempValY = transform.localPosition.y;
    18.        tempValX = transform.localPosition.x;
    19.    }
    20.  
    21.    void Update()
    22.    {
    23.        tempPos.y = tempValY + amplitude * Mathf.Sin(speed * Time.time);
    24.        tempPos.x = tempValX + amplitude * Mathf.Sin(speed * Time.time);
    25.        transform.localPosition = new Vector3(tempPos.x, tempPos.y, transform.localPosition.z);
    26.    }
    27. }
    28.  
    Now it's moving on y and x but I think making it also moving on minus - amplitude too it will give it more realistic floating idling. Maybe even doing it random moving all 4 directions randomly.

    How can I move it all directions and also make it random ?

    I want to make the drone floating in the air in place like idle animation.
    The script make it just floating up down it's fine but I want to make it also move other directions make it more idle realistic.

     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,749
    You're on a good track with using that Mathf.Sin() method to get a procedural bobble, but if you vary the rates of each axis it can be more interesting, such as using 2.0f, 3.0f, and 5.0f as your Time.time multipliers for each axis.

    ALSO, you can have two different time rates for each axis, one fast and one slow, and combine the outputs of two calls to Mathf.Sin() for a given axis... scale the faster one down by 0.2f or so and it will give the drone a kind of twitchiness.

    Additionally you can multiply the output of one Sin() call with the result of another running at a slower speed. This can make the motion temporarily die out as the second slower sine output crosses the zero line, then starts going the other way. You get a lot of nice bobble and reversal, and if you do this on separate axes with different rates of time, it can be quite cool.

    Do you care about rotation? If you watch a real drone you know when the software senses it has moved too far to the right, it tilts the ship left so it flies back left a bit, then stops. If you want to simulate this you can, but it can get very involved very quickly, as you have to so some control loop filtering to keep it from going out of control.

    In other news, I'm a bit of a helicopter nerd, as I have captured in my Jetpack Kurt Space Flight game, which is a first person helicopter-ish flight simulator for iPad / Android:



    There's a bunch of links under the video for trying it on browser too.
     
    DubiDuboni likes this.