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…
you can use bezier curve to move your projectile along the path.
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));
}