Hello,
I am making a top-down arcade 2d game in which the player’s rotation in based on the mouse position(so player looks at mouse all the time) and move towards mouse upon any axes input. Now if the mouse is not moved and if the player gets to the mouse position it flips. I don’t want this to happen and I don’t even want my gamers to move mouse all the time as it will distract them from the objective of the game.
Code here →
using UnityEngine;
using System.Collections;
public class MovementController : MonoBehaviour {
public float speed;
public float stamina;
public float force;
Rigidbody2D rb;
void Awake()
{
rb = gameObject.GetComponent <Rigidbody2D>();
}
void FixedUpdate()
{
//float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
Vector3 verticalMovement = -transform.up * v;
Vector2 velocity = transform.position + verticalMovement;
transform.position = Vector3.Lerp(transform.position, velocity, speed * Time.deltaTime);
LookAtMouse();
}
void LookAtMouse()
{
Vector3 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
float dist = Vector3.Distance(transform.position, mousePosition);
Vector3 direction = mousePosition - transform.position;
direction.Normalize();
float rotZ = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(0f, 0f, rotZ + 90);
}
}
Please help!