I was following a tutorial on making a Sonic game (https://www.youtube.com/watch?v=raIX3znrik4) when I ran into an error saying “The type or namespace name ‘IEnumerator’ could not be found (are you missing a using directive or an assembly reference?)”. I’m pretty sure I did everything right.
Here’s my code:
using UnityEngine;
public class PlayerPhysics : MonoBehaviour
{
public Rigidbody RB;
public LayerMask layerMask;
public Vector3 horizontalVelocity => Vector3.ProjectOnPlane(RB.velocity, RB.transform.up);
public Vector3 verticalVelocity => Vector3.Project(RB.velocity, RB.transform.up);
// Update
void Update()
{
if (Input.GetButtonDown("Jump"))
Jump();
}
// Jump
[SerializeField] float jumpForce;
void Jump()
{
if(!ground) return;
RB.velocity = (Vector3.up * jumpForce)
+ horizontalVelocity;
}
// Fixed Update
void FixedUpdate()
{
Move();
if(!ground)
Gravity();
StartCoroutine(LateFixedUpdateRoutine());
IEnumerator LateFixedUpdateRoutine()
{
yield return new WaitForFixedUpdate();
LateFixedUpdate();
}
}
// Move
[SerializeField] float speed;
void Move()
{
RB.velocity = (Vector3.right * Input.GetAxis("Horizontal") * speed) + (Vector3.forward * Input.GetAxis("Vertical") * speed)
+ verticalVelocity;
}
// Gravity
[SerializeField] float gravity;
void Gravity()
{
RB.velocity -= Vector3.up * gravity * Time.deltaTime;
}
// Late Fixed Update
void LateFixedUpdate()
{
Ground();
Snap();
}
// Ground
[SerializeField] float groundDistance;
Vector3 point;
Vector3 normal;
bool ground;
void Ground()
{
ground = Physics.Raycast(RB.worldCenterOfMass, -RB.transform.up, out RaycastHit hit, groundDistance, layerMask, QueryTriggerInteraction.Ignore);
point = ground ? hit.point : RB.transform.position;
normal = ground ? hit.normal : Vector3.up;
}
// Snap
void Snap()
{
RB.transform.up = normal;
Vector3 goal = point;
Vector3 difference = goal - RB.transform.position;
if(RB.SweepTest(difference, out _, difference.magnitude, QueryTriggerInteraction.Ignore)) return;
RB.transform.position = goal;
}
}
I would appreciate if anyone wants to help.
Thanks,
Aiden