Hi,
Bottom line I would like to imitate movement of a game like Battlerite.
3D, WASD movement of character while always looking at the position of the mouse.
So if the mouse position is on the right side of the character and you would be pressing D the player would have the Walk Forwards animation, if they would be pressing A they would have the Walk Backwards animation.
If the mouse would be above the player and they would press D they would strafe to the right, if they press S they would walk backwards.
I am not sure how to approach this. Any help would be very much appreciated. Thank you.
This is the code i am using, it is from a YouTube tutorial by Broken Knights Games.
public class Input : MonoBehaviour
{
public Vector2 InputVector { get; private set; }
public Vector2 relativeDirection;
public Vector3 MousePosition { get; private set; }
float h, v;
Animator anim;
private void Start()
{
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
h = UnityEngine.Input.GetAxisRaw("Horizontal");
anim.SetFloat("h", h);
v = UnityEngine.Input.GetAxisRaw("Vertical");
anim.SetFloat("v", v);
InputVector = new Vector2(h, v).normalized;
MousePosition = UnityEngine.Input.mousePosition;
}
}
public class MovementAndRotation : MonoBehaviour
{
private Input _input;
[SerializeField]
private bool RotateTowardMouse;
[SerializeField]
private float MovementSpeed;
[SerializeField]
private float RotationSpeed;
[SerializeField]
private Camera Camera;
private void Awake()
{
_input = GetComponent<Input>();
}
// Update is called once per frame
void FixedUpdate()
{
var targetVector = new Vector3(_input.InputVector.x, 0, _input.InputVector.y);
var movementVector = MoveTowardTarget(targetVector);
if (!RotateTowardMouse)
{
RotateTowardMovementVector(movementVector);
}
if (RotateTowardMouse)
{
RotateFromMouseVector();
}
}
private void RotateFromMouseVector()
{
Ray ray = Camera.ScreenPointToRay(_input.MousePosition);
if (Physics.Raycast(ray, out RaycastHit hitInfo, maxDistance: 300f))
{
var target = hitInfo.point;
target.y = base.transform.position.y;
transform.LookAt(target);
}
}
private Vector3 MoveTowardTarget(Vector3 targetVector)
{
var speed = MovementSpeed * Time.deltaTime;
// transform.Translate(targetVector * (MovementSpeed * Time.deltaTime)); Demonstrate why this doesn't work
//transform.Translate(targetVector * (MovementSpeed * Time.deltaTime), Camera.gameObject.transform);
targetVector = Quaternion.Euler(0, Camera.gameObject.transform.rotation.eulerAngles.y, 0) * targetVector;
var targetPosition = transform.position + targetVector * speed;
transform.position = targetPosition;
return targetVector;
}
private void RotateTowardMovementVector(Vector3 movementDirection)
{
if (movementDirection.magnitude == 0) { return; }
var rotation = Quaternion.LookRotation(movementDirection);
transform.rotation = Quaternion.RotateTowards(transform.rotation, rotation, RotationSpeed);
}
}