Question by
DanielB89 · Oct 05, 2014 at 04:05 PM ·
2dprojectilesine-wave
How to make projectile to shoot in a sine wave pattern
I'm making a 2d top down shooter like Galaga. I want the projectile to shoot from the ship and move up the screen with a sine wave pattern. This is all I got and I'm only able to move the projectile straight
public class Projectile : MonoBehaviour {
// Used to control how fast the game object moves
public float MoveSpeed = 5.0f;
// Use this for initialization
void Start () {
DestroyObject(gameObject, 1.0f);
}
// Update is called once per frame
void Update () {
transform.position +=
transform.up * Time.deltaTime * MoveSpeed;
}
}
Comment
Best Answer
Answer by robertbu · Oct 05, 2014 at 04:26 PM
using UnityEngine;
using System.Collections;
public class Projectile : MonoBehaviour {
public float MoveSpeed = 5.0f;
public float frequency = 20.0f; // Speed of sine movement
public float magnitude = 0.5f; // Size of sine movement
private Vector3 axis;
private Vector3 pos;
void Start () {
pos = transform.position;
DestroyObject(gameObject, 1.0f);
axis = transform.right; // May or may not be the axis you want
}
void Update () {
pos += transform.up * Time.deltaTime * MoveSpeed;
transform.position = pos + axis * Mathf.Sin (Time.time * frequency) * magnitude;
}
}
thanks for the temp code, i do believe this should be very similar in cos wave, thanks again for the post =)
Your answer
Follow this Question
Related Questions
Projectiles aren't shooting the direction they're supposed to with transform.up 0 Answers
2D Projectile Script Not Working 1 Answer
Spin Projectile? 0 Answers
Rigidbody projectile misses collider 1 Answer
Problem with projectile rotation - 2D 0 Answers