Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

3D Skateboard Movement Problem

Discussion in 'Scripting' started by Piegamer521, Dec 12, 2019.

  1. Piegamer521

    Piegamer521

    Joined:
    Jul 4, 2018
    Posts:
    6
    Hello I am a beginner at unity and i cant seem to find a tutorial on making movement for a skateboard game I am making for a competition. Usually i would search around the internet and look for a helpful tutorial but nothing seems to pop up. I am looking for a movement system that is fast and reactive and can be adaptable to multiple environments. I have tried using the wheel collider tutorial ( from the unity website ) but it was a disaster. I want something similar to the tony hawk games or this guys prototype. https://forum.unity.com/threads/some-skate-physics-problem.544537/
    Any ways any help is appreciated : )
     
  2. Serinx

    Serinx

    Joined:
    Mar 31, 2014
    Posts:
    785
    I wouldn't use actual wheel colliders for this. Skateboards don't really have any spring in the wheels so you could just treat it as a single rigidbody with a box collider and use force to push it forward/backwards and rotation on the up axis.
    Give the rigidbody a low friction which you can increase for braking.
    You can use a simple animation for rotating the wheels if you want that detail.
    You might want a custom collider with curved edges to help it get up ramps easier.

    Sometimes using "real" physics is a bit excessive for the use case and ends up causing problems rather than adding any value to your game.
     
    Piegamer521 likes this.
  3. Piegamer521

    Piegamer521

    Joined:
    Jul 4, 2018
    Posts:
    6
    So you think I should use the rigidbody for how the movement functions via using forces. That makes sense. but how would i keep the board above the ground as i do not want it to collide with anything (unless the player bails) but rather float above the ground so i can have it maneuver around complex environments with relative ease?
     
  4. Serinx

    Serinx

    Joined:
    Mar 31, 2014
    Posts:
    785
    You can reduce the friction on the board so that it slides along the ground.

    The problem with my suggested method, I just realised, is that your skateboard will drift when you turn unless you add some code to only allow velocity on the forward vector of your skateboard transform.

    The feel of the movement is the most important part of your game so you should try a bunch of different methods and work out what feels the best.

    My suggestion might not pan out, it was just an idea for something to try.
     
    Piegamer521 likes this.
  5. Piegamer521

    Piegamer521

    Joined:
    Jul 4, 2018
    Posts:
    6
    Do you think i should use a script like this for keeping the board above the ground?
    using UnityEngine;

    public class Example : MonoBehaviour
    {
    // Movable, levitating object.

    // This works by measuring the distance to ground with a
    // raycast then applying a force that decreases as the object
    // reaches the desired levitation height.

    // Vary the parameters below to
    // get different control effects. For example, reducing the
    // hover damping will tend to make the object bounce if it
    // passes over an object underneath.

    // Forward movement force.
    float moveForce = 1.0f;

    // Torque for left/right rotation.
    float rotateTorque = 1.0f;

    // Desired hovering height.
    float hoverHeight = 4.0f;

    // The force applied per unit of distance below the desired height.
    float hoverForce = 5.0f;

    // The amount that the lifting force is reduced per unit of upward speed.
    // This damping tends to stop the object from bouncing after passing over
    // something.
    float hoverDamp = 0.5f;

    // Rigidbody component.
    Rigidbody rb;

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

    // Fairly high drag makes the object easier to control.
    rb.drag = 0.5f;
    rb.angularDrag = 0.5f;
    }

    void FixedUpdate()
    {
    // Push/turn the object based on arrow key input.
    rb.AddForce(Input.GetAxis("Vertical") * moveForce * transform.forward);
    rb.AddTorque(Input.GetAxis("Horizontal") * rotateTorque * Vector3.up);

    RaycastHit hit;
    Ray downRay = new Ray(transform.position, -Vector3.up);

    // Cast a ray straight downwards.
    if (Physics.Raycast(downRay, out hit))
    {
    // The "error" in height is the difference between the desired height
    // and the height measured by the raycast distance.
    float hoverError = hoverHeight - hit.distance;

    // Only apply a lifting force if the object is too low (ie, let
    // gravity pull it downward if it is too high).
    if (hoverError > 0)
    {
    // Subtract the damping from the lifting force and apply it to
    // the rigidbody.
    float upwardSpeed = rb.velocity.y;
    float lift = hoverError * hoverForce - upwardSpeed * hoverDamp;
    rb.AddForce(lift * Vector3.up);
    }
    }
    }
    }

    And then aplly forces to the rigid body for movement ( I found the script here: https://docs.unity3d.com/ScriptReference/RaycastHit-distance.html )
     
  6. Piegamer521

    Piegamer521

    Joined:
    Jul 4, 2018
    Posts:
    6
    So I tested this script out and it is working out for the most part except for a few problems.
    These include:
    It hovers in a way that it bobs up and down.
    When the skate board collides with something it rotates all over the place in a unpredictable way.
    And lastly it will occasionally fall through the floor if it is dropped from a high place.