- Home /
 
The question is answered, right answer was accepted
how to make an 'S' Shaped projectile in 2D ?
I'm creating a space shooter game in 2D. I want the enemyship to generate a projectile that travels towards playership in 'S' path(Curvy path). Please help me...
Answer by RisheelK · Apr 04, 2018 at 05:01 AM
you can use bezier curve to move your projectile along the path.
Answer by Cornelis-de-Jager · Apr 04, 2018 at 05:29 AM
How I would go about this is split the forward and sideways movement into two parts. One part that just moves it forward and a second that moves it sideways.
Here is an example script I wrote:
 /* Public Variables */
 public float s_amp = 0.01f;
 public float S_speed = 1f;
 
 public Transform target;
 public bool tracking = false;
 
 public missleSpeed = 1;
 
 /* Private Variables */
 Vector3 targetLocation;
 float timeAlive;
 
 void Start () {
     targetLocation = target.position;
     transform.LookAt (targetLocation);
     timeAlive = 0;
 }
 
 void FixedUpdate () {
     // Forward motion
     MoveToPlayer ();
     
     //Do the Snake motion
     DoSMotion ();
 }
 
 void MoveToPlayer () {
     // Rotate towards target of tracking
     if (tracking) {
         transform.LookAt (target.position);
     }
     
     // Move forward
     transform.Translate (transform.up * Time.DeltaTime * missleSpeed);
 }
 
 void DoSMotion () {
     // Calculate the S motion
     float sideMotion = s_amp * Mathf.Sin(timeAlive);
     
     // Increase the time alive
     timeAlive += Time.DeltaTime * S_speed;
     
     transform.Translate(new Vector3(sideMotion, 0, 0));
 }
 
 
              Follow this Question
Related Questions
How to shoot a bullet and animate at the same time??? Its a 2D shooter c# 3 Answers
Firing projectiles on the XZ Plane 2 Answers
[SOLVED] Top down shooter projectiles can shoot side to side, but not up and down, why? 0 Answers
Setting the velocity of a projectile based on the angle of the cannon 2 Answers
How can I achieve precise projectile shooting towards the crosshair in Unity? 1 Answer