- Home /
player shoot projectile in one direction (x)
Hello, I have a small problem. My player is shooting in one direction, and I use this script for projectile:
using UnityEngine; using System.Collections;
public class Shooting : MonoBehaviour {
public Transform shootOutOf;
public GameObject shootObj;
public bool shooting;
public float speed;
public float rateOfFire;
// Use this for initialization
void Start () {
}
void Update () {
if (Input.GetButtonDown ("Shoot"))
{
StartCoroutine(Fire());
}
}
IEnumerator Fire()
{
if (!shooting)
{
shooting = true;
GameObject projectile = Instantiate(shootObj, shootOutOf.position, shootOutOf.rotation) as GameObject;
projectile.rigidbody2D.velocity = new Vector2(3, speed);
yield return new WaitForSeconds(rateOfFire);
shooting = false;
}
} }
On the prefab of the projectile I have 2d box collider and rigidbody. I assume that solution is in "IEnumerator Fire()" but I dont know the magic sentence. If you can help that will be great. Thanks in advance.
Pc. In player script this is the method I used for moving the player:
void Movement ()
{
anim.SetFloat ("speed", Mathf.Abs (Input.GetAxis ("Horizontal")));
if (Input.GetAxisRaw ("Horizontal") > 0) {
transform.Translate (Vector3.right * speed * Time.deltaTime);
transform.eulerAngles = new Vector2 (0, 0);
}
if (Input.GetAxisRaw ("Horizontal") < 0) {
transform.Translate (Vector3.right * speed * Time.deltaTime);
transform.eulerAngles = new Vector2 (0, 180);
}
if (Input.GetKeyDown (KeyCode.Space) && grounded) {
rigidbody2D.AddForce (transform.up * 200f);
jumpTime = jumpDelay;
anim.SetTrigger ("Jump");
jumped = true;
}
jumpTime -= Time.deltaTime;
if (jumpTime <= 0 && grounded && jumped) {
anim.SetTrigger ("Land");
jumped = false;
}
So when I go to the right player shoots normal, but when I go left, player still shoots right.
Comment