At the moment I have this script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerScript : MonoBehaviour
{
public float speed = 5;
public Rigidbody2D myRigidbody2D;
void Start()
{
}
void Update()
{
//Player movement "W, A, S, D"
float x = Input.GetAxis("Horizontal");
float y = Input.GetAxis("Vertical");
Vector2 movement = new Vector2(x, y);
transform.Translate(movement * speed * Time.deltaTime);
//Player look at Cursor
Vector3 cursorPosition = Input.mousePosition;
cursorPosition = Camera.main.ScreenToWorldPoint(cursorPosition);
cursorPosition.z = transform.position.z;
Vector2 direction = cursorPosition - transform.position;
float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
}
}
I can move the player/object around with WASD and I can’t make the player/object look at the cursor.
But it seems the player/object sees the cursor as a sort of main object.
Example situation:
- My cursor is on the right of the screen and my player/object is on the left of the screen.
- If I press “D” the player/object moves to the right, towards my cursor.
- But if I place my cursor on the left of the screen and my player/object on the right of the screen and I press “D” again, the player/object moves to the left, towards my cursor.
I want to create the same movement used in a game like zombsroyale.io. In that game it doesn’t matter where the cursor is, the player will still go right even if the cursor is on the left of the screen.
Hopefully, someone can help me with this problem.