I’m trying to make a Legend of Zelda-esque game, with the same kind of in-combat movement.
I’m trying to do it purely with Rigidbody, but unfortunately, nothing seems to have worked smoothly - transform.LookAt() didn’t yield any positive results.
Current code is as follows. Thank you:
using System;
using UnityEngine;
public class Movement : MonoBehaviour
{
internal Rigidbody rb;
private float x, y, z;
//Float Scalar for the Vector Movement Speed
public float vel;
private float angleVel;
private float hAxis, vAxis;
internal Vector3 moveVec;
//Boolean to control whether or not Movement is possible, based on other states.
public bool bCanWalk;
void Start()
{
rb = GetComponent<Rigidbody>();
rb.constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationZ;
}
void Update()
{
//Move once per frame
Move();
}
void Move()
{
//If the Character is even able to walk, then do the following:
if (bCanWalk)
{
//If the Game Object to which this Script is attached is tagged as "Player", then do the following:
if (gameObject.tag == "Player")
{
//Refresh the Player's Movement
RefreshPlayerMove();
}
}
}
void RefreshPlayerMove()
{
y = rb.velocity.y;
hAxis = Input.GetAxisRaw("Horizontal");
vAxis = Input.GetAxisRaw("Vertical");
switch (hAxis)
{
case -1:
case 1:
angleVel += vel;
x = (float) Math.Sin(angleVel) + hAxis * vel;
z = (float) Math.Cos(angleVel) + vAxis * vel;
break;
default:
angleVel = 0;
x = 0;
z = vAxis * vel;
break;
}
SetMoveVec();
rb.velocity = moveVec;
}
private void SetMoveVec()
{
moveVec.x = x;
moveVec.y = y;
moveVec.z = z;
}
}