- Home /
2d shooting Left / Right
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.
Answer by tebandesade · Jun 24, 2014 at 07:31 PM
public bool facingright=True;
void FixedUpdate ()
{
GetComponent().velocity = new Vector2(move(where the character is moving, try calling the method of the class of the character) * maxSpeed( bullet velocity), GetComponent().velocity.y);
if(move > 0 && !FacingRight)
{
Flip();
}
else if ( move < 0 && FacingRight)
{
Flip();
}
void Flip()
{
FacingRight = !FacingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
something like that. Just call the method that moves the character . To do so just reference it at the beginning making the type the name of the script.
Your answer
Follow this Question
Related Questions
Projectile move towards mouse cursor 3 Answers
Shoot Bullet At Touch Position : 2D 1 Answer
Problem creating a 2D scroller shooting game 2 Answers
How do I make my sprite go left or right when it hits a trigger? 0 Answers
Shoot 10 bullets at once 1 Answer