Hi, I have been trying to learn the new input system and have been running into a problem with my top down 2d shooter.
My gun rotates towards the mouse position and that works great until I use the attack action which then locks up the z rotation. I debug logged all the numbers and they are still being changed but the rotation is no longer being effected.
This is the rotation script
public class FlipAndPoint : MonoBehaviour
{
[SerializeField]
private PlayerInput playerInput;
private Rigidbody2D rb;
private Vector2 aimPosition;
private InputAction mousePosition;
private bool pointingRight = true;
private SpriteRenderer sr;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
rb = GetComponent<Rigidbody2D>();
sr = GetComponent<SpriteRenderer>();
mousePosition = playerInput.actions["AimPosition"];
}
// Update is called once per frame
void Update()
{
aimPosition = Camera.main.ScreenToWorldPoint(mousePosition.ReadValue<Vector2>());
Vector3 diff = aimPosition - rb.position;
if (diff.x >= 0 && !pointingRight)
{
transform.localScale = new Vector3(1, 1, 1);
pointingRight = true;
}
else if (diff.x < 0 && pointingRight)
{
transform.localScale = new Vector3(1, -1, 1);
pointingRight = false;
}
Vector2 lookDir = aimPosition - rb.position;
float angle = Mathf.Atan2(lookDir.y, lookDir.x) * Mathf.Rad2Deg;// - 90f;
rb.rotation = angle;
}
}
Not that I think it effects it but this is the fire weapon script (seperate so that I can easily attach the roation script to any weapon)
public class RifleBehaviour : MonoBehaviour
{
[SerializeField]
private PlayerInput playerInput;
//Inputs
private InputAction fireAction;
private InputAction reloadAction;
public Transform firePoint;
public GameObject normalBullet;
public float fireRateAmount = 0.5f;
private float fireRate;
void Start()
{
fireAction = playerInput.actions["Attack"];
reloadAction = playerInput.actions["Reload"];
}
void Update()
{
//Firerate
fireRate -= 1f * Time.deltaTime;
if (fireAction.IsPressed())
{
if(fireRate <= 0){
Fire();
}
}
if(reloadAction.IsPressed())
{
Reload();
}
}
public void Fire()
{
fireRate = fireRateAmount;
Instantiate(normalBullet, firePoint.position, firePoint.rotation);
}
public void Reload()
{
Debug.Log("reloadActioning!");
}
}
Any help would be appriciated. I’ve tried changing things in and out of fixed update and change around how it rotates but that didn’t seem to help