Question by
KostasVar · Jul 29, 2021 at 11:23 AM ·
2d-platformershootingprojectile
2D projectiles not rotating
My fire point does not rotate while my player does. I know it is a movement script problem but I can not figure it out.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
private Rigidbody2D rb;
private BoxCollider2D coll;
private SpriteRenderer sprite;
private Animator anim;
private float dirX = 0f;
[SerializeField]private float moveSpeed = 7f;
[SerializeField]private float JumpForce = 14f;
[SerializeField]private LayerMask jumpableGround;
private enum MovementState { idle,running,jumping,falling}
[SerializeField] private AudioSource jumpSoundEffect;
// Start is called before the first frame update
private void Start()
{
rb = GetComponent<Rigidbody2D>();
coll = GetComponent<BoxCollider2D>();
sprite = GetComponent<SpriteRenderer>();
anim = GetComponent<Animator>();
}
// Update is called once per frame
private void Update()
{
dirX = Input.GetAxisRaw("Horizontal");
rb.velocity = new Vector2(dirX * moveSpeed, rb.velocity.y);
if (Input.GetButtonDown("Jump") && IsGrounded())
{
jumpSoundEffect.Play();
rb.velocity = new Vector2(rb.velocity.x, JumpForce);
}
UpdateAnimationState();
}
private void UpdateAnimationState()
{
MovementState state;
if (dirX > 0f)
{
state = MovementState.running;
sprite.flipX = false;
}
else if (dirX< 0f)
{
state = MovementState.running;
sprite.flipX = true;
}
else
{
state = MovementState.idle;
}
if (rb.velocity.y > .1f)
{
state = MovementState.jumping;
}
else if (rb.velocity.y < -.1f)
{
state = MovementState.falling;
}
anim.SetInteger("state",(int)state);
}
private bool IsGrounded()
{
return Physics2D.BoxCast(coll.bounds.center, coll.bounds.size, 0f, Vector2.down, .1f,jumpableGround);
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613040926im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to fire bullets from multiple guns at the same time? 2 Answers
My enemy AI shoots projectile only to certain degrees. Apparently related to transform.position? 0 Answers
making a boomerang effect in a 2D enviorment 1 Answer
Creating a MegaMan style game, writing bullet script, need help moving it across screen. 2 Answers
Enemy Shooting problem with flip 0 Answers