Untitled

 avatar
unknown
csharp
3 years ago
1.3 kB
3
Indexable
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player_Control : MonoBehaviour
{
    Rigidbody rb;

    private float RideHeight = 1;
    private float RideSpringStrenth = 1.8f;
    private float RideSpringDamper = 0.3f;

    private void Start()
    {
        rb = GetComponent<Rigidbody>();
    }

    private void Update()
    {
        RaycastHit _rayHit;

        if(_rayDidHit)
        {
            Vector3 vel = rb.velocity;
            Vector3 rayDir = transform.TransformDirection(Vector3.down);

            Vector3 otherVel = Vector3.zero;
            Rigidbody hitBody = _rayHit.rigidbody;
                if(hitBody != null)
            {
                otherVel = hitBody.velocity;
            }

            float rayDirVel = Vector3.Dot(rayDir, vel);
            float otherDirVel = Vector3.Dot(rayDir, otherVel);

            float relVel = rayDirVel - otherDirVel;
            float x = _rayHit.distance - RideHeight;

            float springFroce = (x * RideSpringStrenth) - (relVel * RideSpringDamper);

            rb.AddForce(rayDir * springFroce);

            if(hitBody != null)
            {
                hitBody.AddForceAtPosition(rayDir * -springFroce, _rayHit.point);
            }
        }
    }