moving object

Hi all,

I try to code moving or shooting an object (bullet, missile etc… ) tp a target when the object reach a distance from origin it should make a loop then resume original direction to the target.

here is an image to show what im after:
2189603--145239--motionPath.jpg

Any Tip on how to code this kind of motion with unity JS ?
Thank you for any help.

You can use Mathf.sin() to get numbers between -1 and 1. Sin, Cos, and Tan are all great math tools for solving problems involving circles and curves.

Try to figure out how you can use Mathf.sin(elapsedTime) to get an object moving in a circle, then see if you can make that circle movement happen in the middle of your forward motion.

I’d try to explain, but I’m short on time right now.

@MSplitz-PsychoK
Thank you for your reply.

i was able to make the motion with simple eulerAngles that being called with a distance switch,
then with a timer limit on the rotation the forward motion restored.

awesome day

Seems like a fun little challenge so I wrote a script for you. All I ended up doing was lerping a 0-360 value and adding it to the original heading.

Here’s the script, it’s in C# but should be super easy to convert:

public class FlyLoop : MonoBehaviour
{
   public float timeDelay = 1.0f;
   public float timeLoop = 2.0f;

   protected Quaternion originalHeading;
   protected float time;
   protected float lerp;

   protected void Start()
   {
     originalHeading = transform.rotation;
     time = 0.0f;
     lerp = 0.0f;
   }

   protected void Update()
   {
     time += Time.deltaTime;

     if(time >= timeDelay)
     {
       lerp += Time.deltaTime;

       if (lerp > timeLoop)
         lerp = timeLoop;

       transform.rotation = originalHeading * Quaternion.Euler(new Vector3(Mathf.Lerp(0.0f, -360.0f, lerp / timeLoop), 0.0f, 0.0f));

     }

     transform.Translate(transform.forward * 10.0f * Time.deltaTime, Space.World);
   }
}

EDIT: Whoops you solved it already. My bad.

1 Like

@GroZZleR
Thank you very much for the time you give to it and for the code and demo video,
I will try your script and will learn from it.

Awesome day and thank you :slight_smile: