Question by
tsapapolis · Jul 08, 2021 at 04:27 PM ·
2drotationweapontopdown
Rotating weapon in top-down 2D
I'm working on a 2D top-down shooter, and I want to use controller to rotate weapon direction. For now I got only mouse controls.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class WeaponRotation : MonoBehaviour
{
public Rigidbody2D rb;
public Camera cam;
Vector2 movement;
Vector2 mousePos;
void Update()
{
mousePos = cam.ScreenToWorldPoint(Input.mousePosition);
}
void FixedUpdate()
{
Vector2 lookDir = mousePos - rb.position;
float angle = Mathf.Atan2(lookDir.y, lookDir.x) * Mathf.Rad2Deg - 0f;
rb.rotation = angle;
}
}
Comment