Hello everyone, here is a basic script for the charakter-Player of my Top-Down-2DGame, and after different experiment I still haven´t been able to let the player rotate in the direction of the mouseCursor., although shooting is actually working. Player is actually rotating around WorldCenter, or not rotating at all!
I am actually using the new InputSystem(ActionType = value; ControlType = Delta; BindingPath = Delta(mouse).
The Camera.Main has a Virtualmachine, following the Player( just as info)
Could you maybe take a look at this. Would me happy to know if I am mistaking something in the script:
_________________________________________________________THANKYOU!
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.InputSystem;
public class Player : MonoBehaviour
{
public float speed = 10f;
[SerializeField] private float maxHealth = 10f;
private float currentHealth;
public Camera cam;
private Rigidbody2D rb;
Vector2 mousePos;
Vector2 lookDir;
private bool canAttack = true;
public GameObject sword;
private SwordDamage swordDamage;
public InputActionReference movementAction;
public InputActionReference lookAtPointer;
public InputActionReference swordAttackAction;
public InputActionAsset inputActions;
public Image healthBar;
public Vector2 Direction { get; private set; } = Vector2.right;
private void Awake()
{
rb = GetComponent();
inputActions.Enable();
currentHealth = maxHealth;
swordDamage = sword.GetComponentInChildren();
}
private void Update()
{
mousePos = Input.mousePosition;
mousePos = Camera.main.ScreenToWorldPoint(lookAtPointer.action.ReadValue());
}
void FixedUpdate()
{
var movementVector = movementAction.action.ReadValue();
if (movementVector.magnitude > 0)//return the lenght of this Vector
Direction = movementVector.normalized;
rb.MovePosition(rb.position + movementVector * speed * Time.deltaTime);
Vector2 lookDir = (mousePos - rb.position).normalized;
float angle = Mathf.Atan2(lookDir.y, lookDir.x) * Mathf.Rad2Deg - 90f;
rb.rotation = angle;
if (canAttack && swordAttackAction.action.triggered)
{
swordDamage.PerformAttack();
}
}