im new to coding and unity2D in general and ive been trying to figure this out for a while now.
i know there are lots of tutorials on how to shoot bullets, but not with this type of specific script ive wrote. any help would be appreciative! heres my script…
[SerializeField] float runSpeed = 5f;
[SerializeField] float jumpSpeed = 5f;
[SerializeField] float projetileSpeed = 10f;
[SerializeField] Transform groundCheck;
[SerializeField] Transform groundCheckL;
[SerializeField] Transform groundCheckR;
[SerializeField] GameObject bullet;
[SerializeField] Transform bulletSpawnPos;
Animator animator;
Rigidbody2D rb;
SpriteRenderer spriterenderer;
bool isGrounded;
// Start is called before the first frame update
void Start()
{
animator = GetComponent<Animator>();
rb = GetComponent<Rigidbody2D>();
spriterenderer = GetComponent<SpriteRenderer>();
}
private void Update()
{
Run();
FlipSprite();
Jump();
Shoot();
}
private void FixedUpdate()
{
if ((Physics2D.Linecast(transform.position, groundCheck.position, 1 << LayerMask.NameToLayer("Ground"))) ||
(Physics2D.Linecast(transform.position, groundCheckL.position, 1 << LayerMask.NameToLayer("Ground"))) ||
(Physics2D.Linecast(transform.position, groundCheckR.position, 1 << LayerMask.NameToLayer("Ground"))))
{
isGrounded = true;
}
else
{
isGrounded = false;
animator.Play("Jump");
}
}
private void Run()
{
float controlThrow = CrossPlatformInputManager.GetAxis("Horizontal");
Vector2 PlayerVelocity = new Vector2(controlThrow * runSpeed, rb.velocity.y);
rb.velocity = PlayerVelocity;
bool playerHasHorizontalSpeed = Mathf.Abs(rb.velocity.x) > Mathf.Epsilon;
if(isGrounded)
animator.SetBool("Run", playerHasHorizontalSpeed);
}
private void Jump()
{
if (CrossPlatformInputManager.GetButtonDown("Jump") && isGrounded)
{
Vector2 jumpVelocityToAdd = new Vector2(0, jumpSpeed);
rb.velocity = rb.velocity + jumpVelocityToAdd;
if(isGrounded)
animator.Play("Jump");
}
}
private void Shoot()
{
if (Input.GetKeyDown(KeyCode.Mouse0)){
animator.Play("Shoot");
GameObject BULLET = Instantiate(
bullet,
bulletSpawnPos.transform.position,
Quaternion.identity) as GameObject;
BULLET.GetComponent<Rigidbody2D>().velocity = new Vector2(projetileSpeed, 0f);
}
}
private void FlipSprite()
{
bool playerHasHorizontalSpeed = Mathf.Abs(rb.velocity.x) > Mathf.Epsilon;
if (playerHasHorizontalSpeed)
{
transform.localScale = new Vector2(Mathf.Sign(rb.velocity.x), 1f);
}
}
},i recently started learning c# and unity and ive been stuck trying to figure this out
My player only shoots right when facing right, but still shoots right when facing left.
Lots of tutorials out there but not with the specific script ive built.
If anyone can chime in and help it would be appreciative! heres my script!!!
SerializeField] float runSpeed = 5f;
[SerializeField] float jumpSpeed = 5f;
[SerializeField] float projetileSpeed = 10f;
[SerializeField] Transform groundCheck;
[SerializeField] Transform groundCheckL;
[SerializeField] Transform groundCheckR;
[SerializeField] GameObject bullet;
[SerializeField] Transform bulletSpawnPos;
Animator animator;
Rigidbody2D rb;
SpriteRenderer spriterenderer;
bool isGrounded;
// Start is called before the first frame update
void Start()
{
animator = GetComponent<Animator>();
rb = GetComponent<Rigidbody2D>();
spriterenderer = GetComponent<SpriteRenderer>();
}
private void Update()
{
Run();
FlipSprite();
Jump();
Shoot();
}
private void FixedUpdate()
{
if ((Physics2D.Linecast(transform.position, groundCheck.position, 1 << LayerMask.NameToLayer("Ground"))) ||
(Physics2D.Linecast(transform.position, groundCheckL.position, 1 << LayerMask.NameToLayer("Ground"))) ||
(Physics2D.Linecast(transform.position, groundCheckR.position, 1 << LayerMask.NameToLayer("Ground"))))
{
isGrounded = true;
}
else
{
isGrounded = false;
animator.Play("Jump");
}
}
private void Run()
{
float controlThrow = CrossPlatformInputManager.GetAxis("Horizontal");
Vector2 PlayerVelocity = new Vector2(controlThrow * runSpeed, rb.velocity.y);
rb.velocity = PlayerVelocity;
bool playerHasHorizontalSpeed = Mathf.Abs(rb.velocity.x) > Mathf.Epsilon;
if(isGrounded)
animator.SetBool("Run", playerHasHorizontalSpeed);
}
private void Jump()
{
if (CrossPlatformInputManager.GetButtonDown("Jump") && isGrounded)
{
Vector2 jumpVelocityToAdd = new Vector2(0, jumpSpeed);
rb.velocity = rb.velocity + jumpVelocityToAdd;
if(isGrounded)
animator.Play("Jump");
}
}
private void Shoot()
{
if (Input.GetKeyDown(KeyCode.Mouse0)){
animator.Play("Shoot");
GameObject BULLET = Instantiate(
bullet,
bulletSpawnPos.transform.position,
Quaternion.identity) as GameObject;
BULLET.GetComponent<Rigidbody2D>().velocity = new Vector2(projetileSpeed, 0f);
}
}
private void FlipSprite()
{
bool playerHasHorizontalSpeed = Mathf.Abs(rb.velocity.x) > Mathf.Epsilon;
if (playerHasHorizontalSpeed)
{
transform.localScale = new Vector2(Mathf.Sign(rb.velocity.x), 1f);
}
}
}