Search Unity

Skate Boarding Physics Problem

Discussion in 'Physics' started by Piegamer521, Jan 11, 2020.

  1. Piegamer521

    Piegamer521

    Joined:
    Jul 4, 2018
    Posts:
    6
    Hello! I am having trouble with a skateboarding video game I am making and I got the movement to function on flat ground and it works pretty good (here is a link to the script I found)
    https://docs.unity3d.com/ScriptReference/RaycastHit-distance.html
    Its very loose and I like how it moves. The way it works is that it floats above the ground in a way that it slides all over the place. It only works on flat ground though and be fore i build anything else i want to Try and fix the issue of it interacting with uneven surfaces of the terrain or ramps as when it rotates on the x axis or the y axis it no longer works and it doesn't move right and flies all over the place if is not frozen on those axis's but then it cant go up ramps and inclines and such. Here is the code i use for the movement.

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    public class Movement : MonoBehaviour
    {
    public float Move_Force = 1.0f; // Forwad movement force.
    public float Rotate_Tourqe = 1.0f; // Touque Left/Right rotation.
    public float Hover_Height = 4f; //Desired Hovering Height
    public float Hover_Force = 4f; //The force applied per unit of distance below desired hieght
    public float Hover_Damp = 0.05f; //The amount of lifting fore is reduced the unit of upward speed. The dampening stops the object bouncing after passing over something.
    Rigidbody RB;
    // Start is called before the first frame update
    void Start()
    {
    RB = GetComponent<Rigidbody>();
    RB.drag = 1f;
    RB.angularDrag = 1f;
    }
    // Update is called once per frame
    void FixedUpdate()
    {
    //Push and rotates the skateboard based on arrow and keybord input.
    RB.AddForce(Input.GetAxis("Vertical") * Move_Force * transform.forward);
    RB.AddTorque(Input.GetAxis("Horizontal") * Rotate_Tourqe * Vector3.up);
    RaycastHit Hit;
    Ray downRay = new Ray(transform.position, -Vector3.up);
    if (Physics.Raycast(downRay, out Hit)) // Cast a ray straight downward
    {
    float HoverError = Hover_Height - Hit.distance; // The "error" in height is the diffrence in the desired height and the distance of the height measured by the raycast.
    if (HoverError > 0) //Aplies lifting force if the object is to low (ex. If the object is to high let gravity pull it down.)
    {
    // This code subtracks the dampning force from the lifting force and applies it.
    float UpwardSpeed = RB.velocity.y;
    float lift = HoverError * Hover_Force - UpwardSpeed * Hover_Damp;
    RB.AddForce(lift * Vector3.up);
    }
    }
    }
    }


    After this i tried to make a script that makes the wheels float above the ground so that it could stabilizes itself at those for positions on the skate board and staying above the ground and terrain at a set position. So i took the float script on the movement and only used that to try and keep the wheels set at a specific point above the terrain. While it dose stay above the ground the board leaves it behind as the movement script is on the parent of the model and this floating script is on the models of the wheels and i want the floating script to stay on the wheels ( so it would stay at a set position above the terrain but still feels smooth ). I think the rigid body on the wheels might have something to do with it but i don't know and i need your help.

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    public class FloatingWheels : MonoBehaviour
    {
    public float Hover_Height = 4f; //Desired Hovering Height
    public float Hover_Force = 4f; //The force applied per unit of distance below desired hieght
    public float Hover_Damp = 0.05f; //The amount of lifting fore is reduced the unit of upward speed. The dampening stops the object bouncing after passing over something.
    Rigidbody RB;
    // Start is called before the first frame update
    void Start()
    {
    RB = GetComponent<Rigidbody>();
    RB.drag = 1f;
    RB.angularDrag = 1f;
    }
    // Update is called once per frame
    void Update()
    {
    RaycastHit Hit;
    Ray downRay = new Ray(transform.position, -Vector3.up);
    if (Physics.Raycast(downRay, out Hit)) // Cast a ray straight downward
    {
    float HoverError = Hover_Height - Hit.distance; // The "error" in height is the diffrence in the desired height and the distance of the height measured by the raycast.
    if (HoverError > 0) //Aplies lifting force if the object is to low (ex. If the object is to high let gravity pull it down.)
    {
    // This code subtracks the dampning force from the lifting force and applies it.
    float UpwardSpeed = RB.velocity.y;
    float lift = HoverError * Hover_Force - UpwardSpeed * Hover_Damp;
    RB.AddForce(lift * Vector3.up);
    }
    }
    }
    }

    So what do i do to fix my problem?