Coming soon - Get a detailed view of why an account is flagged as spam!
view details

This post has been de-listed (Author was flagged for spam)

It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.

1
raycasting wheels flip vehicle when hitting something or going off a jump
Post Body

i dont know why this is happening i followed an example for a raycasting supsension system

wheel.cs:

public Transform graphic;

    public float mass = 1.0f;
    public float radius = 1.0f;
    public float maxSuspension = 0.2f;
    public float spring = 100.0f;
    public float damper = 0.0f;

    public float wheelAngle = 0f;

    private float circumference;

    private float contactPatchArea;
    private Rigidbody parent;
    private bool grounded = false;

    public bool IsGrounded
    {
        get
        {
            return grounded;
        }
    }

    void Awake()
    {
        circumference = (radius * 2) * Mathf.PI;
        parent = transform.root.GetComponent<Rigidbody>();
    }

    void FixedUpdate()
    {
        GetGround();
    }

    void GetGround()
    {
        grounded = false;
        Vector3 downwards = transform.TransformDirection(-Vector3.up);
        RaycastHit hit;

        // down = local downwards direction
        Vector3 down = transform.TransformDirection(Vector3.down);

        if (Physics.Raycast(transform.position, downwards, out hit, radius   maxSuspension))
        {

            grounded = true;
            Vector3 velocityAtTouch = parent.GetPointVelocity(hit.point);


            float compression = hit.distance / (maxSuspension   radius);
            compression = -compression   1;

            Vector3 force = -downwards * compression * spring;

            Vector3 t = transform.InverseTransformDirection(velocityAtTouch);

            t.z = t.x = 0;

            Vector3 damping = transform.TransformDirection(t) * -damper;
            Vector3 finalForce = force   damping;

            t = parent.transform.InverseTransformDirection(velocityAtTouch);
            t.y = 0;
            t.z = 0;

            t = transform.TransformDirection(t);

            parent.AddForceAtPosition(finalForce   (t), hit.point);

            if (graphic) graphic.position = transform.position   (down * (hit.distance - radius));

        }
        else
        {
            if (graphic) graphic.position = transform.position   (down * maxSuspension);
        }

        float speed = parent.velocity.magnitude;

        if (graphic)
        {
            graphic.transform.localEulerAngles = new Vector3 (graphic.transform.localEulerAngles.x, wheelAngle, graphic.transform.localEulerAngles.z); 
            graphic.transform.Rotate (360 * (speed / circumference) * Time.fixedDeltaTime, 0, 0); 
        }
    }

    void OnDrawGizmosSelected()
    {
        if (grounded)
        {
            Gizmos.color = new Color(0, 1, 0, 1);
        }
        else
        {
            Gizmos.color = new Color(1, 0, 0);
        }
        Vector3 direction = transform.TransformDirection(-Vector3.up) * (this.radius);
        Gizmos.DrawRay(transform.position, direction);

        Gizmos.color = new Color(0, 0, 1, 1);
        direction = transform.TransformDirection(-Vector3.up) * (this.maxSuspension);
        Gizmos.DrawRay(new Vector3(transform.position.x, transform.position.y - radius, transform.position.z), direction);
    }

Kart.cs:

Rigidbody rb;

    public float Throttle;
    public float SteerAxis;

    public float Acceleration = 2.0f;
    public float MaxSpeed = 10.0f;
    public float MaxReverseSpeed = 3.0f;
    public float Speed = 0.0f;
    RaycastHit hit;
    public AudioSource EngineSource;

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

    }
    void Update () 
    {
        Throttle = Input.GetAxis("Vertical");
        SteerAxis = Input.GetAxis("Horizontal");

        if(Throttle > 0)
        {
            Speed  = Acceleration * Time.deltaTime;
        }
        else
        {
            Speed *= 0.98f;
        }
        EngineSource.pitch = 1.0f   (Speed / MaxSpeed);
        Speed = Mathf.Clamp(Speed, -MaxReverseSpeed, MaxSpeed);
        transform.Rotate(new Vector3(0, 1, 0), 30.0f * SteerAxis * Time.deltaTime);
        Vector3 velo = transform.forward * Speed;
        rb.velocity = new Vector3(velo.x, rb.velocity.y, velo.z);\
   }

edit: it also starts to spin randomly after a while

Author
Account Strength
0%
Account Age
7 years
Verified Email
Yes
Verified Flair
No
Total Karma
34,548
Link Karma
9,973
Comment Karma
23,263
Profile updated: 7 months ago
Posts updated: 8 months ago
15 year old gamedev

Subreddit

Post Details

We try to extract some basic information from the post title. This is not always successful or accurate, please use your best judgement and compare these values to the post title and body for confirmation.
Posted
5 years ago