#code
1 messages · Page 1 of 1 (latest)
nodes
public class Motion : MonoBehaviour
{
public float speed;
public float sprintModifier;
public float jumpForce;
public Camera normalCam;
private Rigidbody rig;
private float baseFOV;
private float sprintFOVModifier = 1.5f;
private void Start()
{
baseFOV = normalCam.fieldOfView;
Camera.main.enabled = false;
rig = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
//States
bool isJumping = jump;
bool isSprinting = t_sprint && t_vmove > 0 && !isJumping;
//Jumping
if (isJumping)
{
rig.AddForce(Vector3.up * jumpForce);
}
//Movement
Vector3 t_direction = new Vector3(t_hmove, 0, t_vmove);
t_direction.Normalize();
float t_adjustedSpeed = speed;
if (isSprinting) t_adjustedSpeed *= sprintModifier;
Vector3 t_targetVelocity = transform.TransformDirection(t_direction) * t_adjustedSpeed * Time.fixedDeltaTime;
t_targetVelocity.y = rig.velocity.y;
rig.velocity = t_targetVelocity;
//Field Of View
if (isSprinting)
{
normalCam.fieldOfView = Mathf.Lerp(normalCam.fieldOfView, baseFOV * sprintFOVModifier, Time.deltaTime * 8f);
}
else
{
normalCam.fieldOfView = Mathf.Lerp(normalCam.fieldOfView, baseFOV, Time.deltaTime * 8f);
}
}
private void Update()
{
//Axis
float t_hmove = Input.GetAxisRaw("Horizontal");
float t_vmove = Input.GetAxisRaw("Vertical");
//Controls
bool t_sprint = Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift);
bool jump = Input.GetKeyDown(KeyCode.Space);
}
}
}````