I’m desperate with this i need to make a rocket launcher code to my game.It should work like this:
When player press Fire1->Create some object to be a rocket->Give a force or a velocity or something to change the rocket position.
Looks so simple but i’m breaking my head in that task.Can someboy help me:face_with_spiral_eyes:
here is my rocket launcher code…
public Vector3 muzzlePoint = null; // weapon's muzzle point
public GameObject rocket = null; // the rocket round
private float[] bulletInfo = new float[6];// all of the info sent to a fired bullet
void Update()
{
if (Input.GetButtonDown("Fire1"))
{
Launcher_Fire();
}
}
Launcher_Fire()
{
Vector3 position = muzzlePoint.position; // position to spawn rocket / grenade is at the muzzle point of the gun
// set the gun's info into an array to send to the bullet
bulletInfo[0] = damage; //gun's damage to send to rocket
bulletInfo[1] = impactForce; // force applied to a hit object
bulletInfo[2] = maxPenetration; // not used in rockets since most rockets don't penetrate anything.... set to 1
bulletInfo[3] = maxSpread; // maximum amount of spread allowed
bulletInfo[4] = spread; // current spread, Max and current spreads are mostly for machine guns
bulletInfo[5] = bulletSpeed; // how fast do you want the rocket to be
GameObject newRocket = Instantiate(rocket, position, transform.parent.rotation) as GameObject; // create rocket
newRocket.SendMessageUpwards("SetUp", bulletInfo); // send info to the new rocket
if (audio)
{
audio.Play(); // if there is a gun shot sound....play it
}
}
this will spawn a rocket game object at your muzzlepoint of your weapon. My rocket has its own script to handle itself.
public float lifetime = 5.0f;
public GameObject explosion; // instanced explosion
private float damage; // damage bullet applies to a target
//private float maxHits; // number of collisions before bullet gets destroyed
private float impactForce; // force applied to a rigid body object
private float maxInaccuracy; // maximum amount of inaccuracy
private float variableInaccuracy; // used in machineguns to decrease accuracy if maintaining fire
private float speed; // bullet speed
private string ownersName = ""; // name of player that launched missle
private Vector3 velocity = Vector3.zero; // bullet velocity
private Vector3 newPos = Vector3.zero; // bullet's new position
private Vector3 direction; // direction bullet is travelling
private string[] rocketInfo = new string[2];
public void SetUp(float[] info)
{
damage = info[0];
impactForce = info[1];
//maxHits = info[2];
maxInaccuracy = info[3];
variableInaccuracy = info[4];
speed = info[5];
direction = transform.TransformDirection(Random.Range(-maxInaccuracy, maxInaccuracy) * variableInaccuracy, Random.Range(-maxInaccuracy, maxInaccuracy) * variableInaccuracy, 1);
newPos = transform.position;
velocity = speed * transform.forward;
// schedule for destruction if bullet never hits anything
Invoke("Kill", lifetime);
}
void Update()
{
direction = transform.TransformDirection(Random.Range(-maxInaccuracy, maxInaccuracy) * variableInaccuracy, Random.Range(-maxInaccuracy, maxInaccuracy) * variableInaccuracy, 1);
// assume we move all the way
newPos += (velocity + direction) * Time.deltaTime;
transform.position = newPos;
}
void OnCollisionEnter(Collision collision)
{
if (collision.transform.tag == "Projectile")
{
return; // ignore collisions with other projectiles
}
// Instantiate explosion at the impact point and rotate the explosion
// so that the y-axis faces along the surface normal
ContactPoint contact = collision.contacts[0];
Quaternion rotation = Quaternion.FromToRotation(Vector3.up, contact.normal);
Instantiate(explosion, contact.point, rotation);
rocketInfo[0] = ownersName;
rocketInfo[1] = damage.ToString();
collision.collider.SendMessageUpwards("ImHit", rocketInfo, SendMessageOptions.DontRequireReceiver);
if (collision.rigidbody)
{
collision.rigidbody.AddForce(transform.forward * impactForce, ForceMode.Impulse);
}
// And kill our selves
Kill();
}
void Kill()
{
// Stop emitting particles in any children
ParticleEmitter emitter = (ParticleEmitter)GetComponentInChildren<ParticleEmitter>();
if (emitter)
emitter.emit = false;
// Detach children - We do this to detach the trail rendererer which should be set up to auto destruct
transform.DetachChildren();
// Destroy the projectile
Destroy(gameObject);
}
set it up similar to this and it will work fine. I left out a few variables… like damage and those, but you should get the idea. Check the gun script in the link in my sig if you need a downloadable project version to look over.
Thank you!Coming up next i’ll test it(i’m not at home).
Tanks:)