Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question player !pointing and !rotating at mouse position(2DGame)

Discussion in 'Scripting' started by Kriovulkan, Aug 24, 2023.

  1. Kriovulkan

    Kriovulkan

    Joined:
    Aug 24, 2023
    Posts:
    3
    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<Rigidbody2D>();

    inputActions.Enable();

    currentHealth = maxHealth;

    swordDamage = sword.GetComponentInChildren<SwordDamage>();
    }

    private void Update()
    {
    mousePos = Input.mousePosition;
    mousePos = Camera.main.ScreenToWorldPoint(lookAtPointer.action.ReadValue<Vector2>());

    }

    void FixedUpdate()
    {
    var movementVector = movementAction.action.ReadValue<Vector2>();

    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();
    }
    }
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,468
    Please edit your post to use code-tags when posting code.

    Thanks.
     
  3. Kriovulkan

    Kriovulkan

    Joined:
    Aug 24, 2023
    Posts:
    3
    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.UI;
    6. using UnityEngine.InputSystem;
    7.  
    8.  
    9. public class PlayerVampire : MonoBehaviour
    10. {
    11.     public float speed = 10f;
    12.     [SerializeField] private float maxHealth = 10f;
    13.     private float currentHealth;
    14.  
    15.     public Camera cam;
    16.    
    17.     private Rigidbody2D rb;
    18.  
    19.     Vector2 mousePos;
    20.     Vector2 lookDir;
    21.  
    22.     private bool canAttack = true;
    23.     public GameObject sword;
    24.     private SwordDamage swordDamage;
    25.  
    26.     public InputActionReference movementAction;
    27.     public InputActionReference lookAtPointer;
    28.     public InputActionReference swordAttackAction;
    29.     public InputActionAsset inputActions;
    30.  
    31.     public Image healthBar;
    32.  
    33.     public Vector2 Direction { get; private set; } = Vector2.right;
    34.  
    35.     private void Awake()
    36.     {
    37.         rb = GetComponent<Rigidbody2D>();
    38.  
    39.         inputActions.Enable();
    40.  
    41.         currentHealth = maxHealth;
    42.  
    43.         swordDamage = sword.GetComponentInChildren<SwordDamage>();
    44.     }
    45.  
    46.     private void Update()
    47.     {
    48.         Debug.Log("yes,Update works!");
    49.         mousePos = Input.mousePosition;
    50.         mousePos = Camera.main.ScreenToWorldPoint(lookAtPointer.action.ReadValue<Vector2>());
    51.         Debug.Log("Oh, my godness a mice!");
    52.     }
    53.  
    54.     void FixedUpdate()
    55.     {
    56.         var movementVector = movementAction.action.ReadValue<Vector2>();
    57.         //transform.Translate(movementVector * speed * Time.deltaTime);
    58.  
    59.         //Das ist für den Abfeüern vom Projectile _I_ vom playerPosition aus
    60.         if (movementVector.magnitude > 0)//return the lenght of this Vector
    61.             Direction = movementVector.normalized;
    62.  
    63.         rb.MovePosition(rb.position + movementVector * speed * Time.deltaTime);
    64.         Vector2 lookDir = (mousePos - rb.position).normalized;
    65.         Debug.DrawRay(rb.position, lookDir, Color.red);
    66.         float angle = Mathf.Atan2(lookDir.y, lookDir.x) * Mathf.Rad2Deg - 90f;
    67.         rb.rotation = angle;
    68.  
    69.         if (canAttack && swordAttackAction.action.triggered)
    70.         {
    71.             Debug.Log("Fire2 Pressed");
    72.             swordDamage.PerformAttack();
    73.         }
    74.     }
    75.  
    76.     public void TakeDamage(float damage)
    77.     {
    78.         currentHealth -= damage;
    79.         healthBar.fillAmount = Math.Clamp(currentHealth / maxHealth, 0, 1);
    80.  
    81.         if (currentHealth <= 0)
    82.             Debug.Log("YouAreDead");
    83.     }
    84. }
     
  4. Kriovulkan

    Kriovulkan

    Joined:
    Aug 24, 2023
    Posts:
    3
    thank to you, now it´s much clearer
     
  5. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,468
    Note, you could simply edit your initial post but yes, it's beter.

    Code (CSharp):
    1.         var movementVector = movementAction.action.ReadValue<Vector2>();
    2.         //transform.Translate(movementVector * speed * Time.deltaTime);
    3.         //Das ist für den Abfeüern vom Projectile _I_ vom playerPosition aus
    4.         if (movementVector.magnitude > 0)//return the lenght of this Vector
    5.             Direction = movementVector.normalized;
    Is the above code redundant because this sets "Direction" to be normalized, it doesn't make "movementVector" normalized.

    Code (CSharp):
    1. rb.rotation = angle;
    There's MovePosition for a reason but there's also MoveRotation too. You shouldn't really set this directly but if you don't want interpolation then it'll be fine here but this can cause overlaps with other things.

    Most likely is that you're not checking the input values correctly so they're wrong. Check that the debug-ray is being draw correctly. If it is then it'll rotate. The angle calculation is correct although all you've said is:
    ... which I guess could mean anything.

    Try the following code in a simple test project and it'll work:
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class PlayerVampire : MonoBehaviour
    4. {
    5.     public Camera cam;
    6.  
    7.     private Rigidbody2D rb;
    8.     Vector2 mousePos;
    9.  
    10.     private void Awake()
    11.     {
    12.         rb = GetComponent<Rigidbody2D>();
    13.     }
    14.  
    15.     private void Update()
    16.     {
    17.         mousePos = cam.ScreenToWorldPoint(Input.mousePosition);
    18.     }
    19.  
    20.     private void FixedUpdate()
    21.     {
    22.         var lookDir = (mousePos - rb.position).normalized;
    23.         Debug.DrawRay(rb.position, lookDir, Color.red);
    24.         var angle = Mathf.Atan2(lookDir.y, lookDir.x) * Mathf.Rad2Deg - 90f;
    25.         rb.MoveRotation(angle);
    26.     }
    27. }
    28.  
    https://gyazo.com/88dad3cbc24b92955192763a47dbf112