- Home /
How do I make a player shoot right and left 2D platformer firing left and right, help!
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);
}
}
as i already posted below your last comment: Be more specific with your problem! if your description is less then 5 lines then it is probably not enough. Provide details: What exactly is not working? How do you see it is not working?
Answer by N-8-D-e-v · Sep 03, 2020 at 12:41 PM
This should work perfectly, I don't see what's wrong with it
Your answer
Follow this Question
Related Questions
Changing transform.localScale of a prefab in the script not working. Help pls! 1 Answer
scale based on position(help) 1 Answer
2d Circle won't change size 0 Answers
Syncing localScale 0 Answers
How could I shoot a chemical in a fire extinguisher? 1 Answer