How do I make a player shoot right and left 2D platformer firing left and right, help!
Here is my script
I’m making the game for android
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;
public class movo : MonoBehaviour
{
float bulletSpeed = 500f;
public Animator anim;
public Transform barrel;
public Rigidbody2D bullet;
private Rigidbody2D rb;
private bool moveLeft;
private bool moveRight;
private float horizontalMove;
public float speed = 5;
public SpriteRenderer sp;
private void Start()
{
anim = GetComponent<Animator>();
rb = GetComponent<Rigidbody2D>();
sp = GetComponent<SpriteRenderer>();
moveLeft = false;
moveRight = false;
}
public void PointerDownLeft()
{
anim.SetBool("run", true);
moveLeft = true;
}
public void PointerUpLeft()
{
anim.SetBool("run", false);
moveLeft = false;
}
public void PointerDownRight()
{
anim.SetBool("run", true);
moveRight = true;
}
public void PointerUpRight()
{
anim.SetBool("run", false);
moveRight = false;
}
void Update()
{
MovementPlayer();
if (CrossPlatformInputManager.GetButtonDown ("Fire1"))
Fire ();
}
void MovementPlayer()
{
if (moveLeft)
{
sp.flipX = true;
horizontalMove = -speed;
}
else if (moveRight)
{
sp. flipX = false;
horizontalMove = speed;
}
else
{
horizontalMove = 0;
}
}
private void FixedUpdate()
{
rb.velocity = new Vector2(horizontalMove, rb.velocity.y);
}
void Fire()
{
var firedBullet = Instantiate (bullet, barrel.position, barrel.rotation);
firedBullet.AddForce (barrel.up * bulletSpeed);
}
and this code to Bullet
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DestroyInTime : MonoBehaviour
{
[SerializeField]
float destroyTime = 2f;
void Start() {
Destroy(gameObject, destroyTime);
}
}