i’m very new to C# and trying to learn , so i often get my code from questions like this and try to understand them, but i hit a roadblock, i’m trying to make a 2D top-down game,and i don’t want my objects to rotate, only change animations based on my mouse location, but the code i have is rotating the rigidbody, here’s my code
public class movement_mouse : MonoBehaviour
{
[SerializeField]
private float offset, clampDistance, moveSpeed;
public Transform anchor;
public Animator anim;
void Update()
{
Aiming();
}
public Transform Aiming()
{
//Position dans l'écran
transform.position = Vector2.Lerp(anchor.position, Camera.main.ScreenToWorldPoint(Input.mousePosition), moveSpeed);
//Rotation
Vector3 difference = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
float angle = (Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg) + offset;
transform.rotation = Quaternion.Euler(0f, 0f, angle);
angle += 22.5f;
angle %= 360;
if (angle < 0)
angle += 360;
// find the frame and show it
int frameIndex = (int)angle / 45;
anim.SetFloat("x", frameIndex);
return transform;
}
}
any help is good, thx! also, sorry for bad english, not my primary language