Hi,
Im currently working on a 2d sidescroll shooter and now i have the problem that my
Bullets flying to the right direction. My idea is it to tell my "Fire" script to check if the player is currently facing to the left or right and give the bullet speed in this direction. But i dont have an idea to implement this in my script. : /
hope somebody can help me out with this.
Here is the script:
using UnityEngine;
using System.Collections;
public class FirePistol : MonoBehaviour {
//Firing variables
public float fireFrequency = 0.1f;
public float bulletVelocity = 1;
float lastShot;
public bool isFiring = true;
//Holds a link to the object that is to be fired
public GameObject bulletObject;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(Input.GetButton("Fire1"))
{
fireGun();
}
}
//Checks the time since the last shot and whether the gun can fire, if it can it will call Fire()
void fireGun()
{
//print ("fired");
if(Time.time > lastShot + fireFrequency)
{
if(isFiring == true)
{
Fire();
}
}
}
//Instantiates a new bullet object and assignes its starting variables
//Can also handle guided munitions if you want to fire rocket prefabs
void Fire() {
lastShot = Time.time;
//Quarternion.Identity = default rotation
GameObject newBullet = Instantiate(bulletObject, (transform.position + (transform.up/20)), Quaternion.identity) as GameObject;
newBullet.transform.rotation = gameObject.transform.rotation; //Rotate the same direction as the ship it is fired from
newBullet.rigidbody2D.AddForce(transform.up * bulletVelocity); //Give initial speed, needs to be high for bullets and lower for self guided munitions.
}
//Toggles the isFiring bool variable.
void ToggleFiring()
{
//print ("TOGGLE");
isFiring = !isFiring;
}
}
on this line: newBullet.rigidbody2D.AddForce(transform.up * bulletVelocity);
i tested what happens if i change / bulletVelocity to -bulletVelocity
and the bullet flews to the left so as is sad with simple left right check it should work.
sorry for my english.
↧