Snowboarding
unknown
csharp
2 years ago
1.1 kB
10
Indexable
using UnityEngine;
public class Snowboarding : MonoBehaviour
{
private Rigidbody rb => GetComponent<Rigidbody>();
[SerializeField] private float maxSpeed = 5;
RaycastHit slopeHit;
private void FixedUpdate()
{
if(OnSlope())
{
rb.velocity = GetSlopeDirection() * rb.velocity.magnitude;
}
else rb.velocity = new Vector3(rb.velocity.x, rb.velocity.y, rb.velocity.z);
//Limit velocity speed
float clampSpeed = Mathf.Clamp(rb.velocity.magnitude, 0f, maxSpeed);
rb.velocity = rb.velocity.normalized * clampSpeed;
}
private bool OnSlope()
{
if(Physics.Raycast(transform.position, Vector3.down, out slopeHit, 1))
{
float angle = Vector3.Angle(Vector3.up, slopeHit.normal);
return angle < 45 && angle != 0;
}
return false;
}
private Vector3 GetSlopeDirection()
{
return Vector3.ProjectOnPlane(rb.transform.forward, slopeHit.normal).normalized;
}
}Editor is loading...
Leave a Comment