The video above says all.
Here is the following code I used if you cannot see the video in HD yet:
public class CameraManager : MonoBehaviour
{
public float followSpeed;
public Transform target;
public Transform cam;
public static CameraManager singleton;
void Awake()
{
singleton = this;
}
public void Init(Transform t)
{
target = t;
cam = Camera.main.transform;
}
public void FixedTick(float d)
{
FollowTarget (d);
}
void FollowTarget(float d)
{
Vector3 targetPosition = Vector3.Lerp (transform.position, target.position, d * followSpeed);
transform.position = targetPosition;
}
}
public class InputManager : MonoBehaviour
{
public float horizontal;
CameraManager cam;
StatesManager states;
[HideInInspector]
public float delta;
void Start ()
{
cam = CameraManager.singleton;
states = GetComponent<StatesManager> ();
states.Init();
cam.Init(this.transform);
}
void FixedUpdate()
{
delta = Time.fixedDeltaTime;
GetInput ();
UpdateStates ();
states.FixedTick (delta);
cam.FixedTick (delta);
}
void Update ()
{
delta = Time.deltaTime;
}
void GetInput()
{
horizontal = Input.GetAxis ("Horizontal");
}
void UpdateStates()
{
states.horizontal = horizontal;
states.moveDir = new Vector3 (horizontal, 0, 0);
states.moveAmount = horizontal;
states.moveAmount = Mathf.Abs (states.moveAmount);
}
}
public class StatesManager : MonoBehaviour
{
[Header ("Initialization")]
public GameObject activeModel;
[Header ("Inputs")]
public float horizontal;
public float moveAmount;
public Vector3 moveDir;
[Header ("Stats")]
public float moveSpeed;
public float dashSpeed;
public float rotateSpeed;
[Header ("States")]
public bool onGround;
public bool inAction;
public bool canMove;
public bool dashing;
public bool jumping;
[HideInInspector]
public Animator anim;
[HideInInspector]
public Rigidbody rigid;
public void Init ()
{
SetUpAnim ();
SetUpRigid ();
}
public void FixedTick(float d)
{
HandleMovementAndRotations (d);
HandleAnims (d);
}
public void Tick (float d)
{
}
void HandleAnims(float d)
{
anim.SetFloat ("vertical", moveAmount, 0.1f, d);
}
void HandleMovementAndRotations(float d)
{
rigid.drag = (moveAmount > 0 || onGround == false) ? 0 : 4;
rigid.velocity = moveDir * (moveSpeed * moveAmount);
if (moveDir == Vector3.zero)
moveDir = Vector3.forward;
Quaternion tr = Quaternion.LookRotation (moveDir);
Quaternion targetRotation = Quaternion.Slerp (transform.rotation, tr, d * moveAmount * rotateSpeed);
transform.rotation = targetRotation;
}
void SetUpAnim()
{
if (activeModel == null) {
anim = GetComponentInChildren<Animator> ();
if (anim == null)
{
Debug.Log ("No model found.");
}
else
{
activeModel = anim.gameObject;
}
}
if (anim == null)
anim = activeModel.GetComponent<Animator> ();
anim.applyRootMotion = false;
}
void SetUpRigid()
{
rigid = GetComponent<Rigidbody> ();
rigid.angularDrag = 999;
rigid.drag = 4;
rigid.constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationZ;
}
}
And yes, the gravity is at it’s default -9.81 setting, but I’d like to avoid messing with that option since it might complicate things down the road.
pls help me. ive tried for a week. im desperate. thank you.