how to make my character controller stick to a surface?
Hello everyone, i'm making an arcade driving game, one game I have been inspired by is Distance. I want my car to be always stick to the ground, but i'm having some issues.
The main problem is that if i'm going too fast, and the track goes down, the car just act as normal physics, it falls, but I want it to stick to the track always.
In that way, it could stay stick into the "ceiling" without falling and make all crazy loops and twists I want.
This is my main controller code, I hope you could help me, thanks :)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(Rigidbody))]
public class PlayerController : MonoBehaviour
{
#region Variables
[Header("Rigidbody")]
public Rigidbody rb;
[Header("Movement")]
public float velocidad;
float velocidadOG;
public float maxSpeed;
float maxSpeedOG;
[Header("Rotation")]
public float speedRotate = 2;
public float NormalGiroVelocidad = 10;
[Header("Bools")]
public bool isGrounded;
[Header("Raycast")]
public Vector3 halfBoxRaycast;
RaycastHit hit;
[Header("Layer Mask")]
public LayerMask myFloor;
[Header("Otros")]
public float temp;
public float tempOriginal = 1;
#endregion
// Start is called before the first frame update
void Start()
{
rb = rb.GetComponent<Rigidbody>();
//StartCoroutine(ActivateCooldown());
}
// Update is called once per frame
void FixedUpdate()
{
Physics.gravity = -transform.up * 500;
//Do I touch the floor?
if (Physics.BoxCast(transform.position, halfBoxRaycast, -transform.up, out hit, transform.rotation, 2f, myFloor))
{
isGrounded = true;
}
else
{
isGrounded = false;
}
if (isGrounded == true)
{
rb.AddForce(transform.forward * Input.GetAxis("Vertical") * velocidad);
transform.Rotate(0f, Input.GetAxis("Horizontal") * speedRotate, 0);
}
else
{
//transform.Rotate(0f, Input.GetAxis("Horizontal") * speedRotate/4, -Input.GetAxis("Horizontal") * speedRotate / 4);
transform.Rotate(0f, 0f, Input.GetAxis("Horizontal") * speedRotate);
}
#region Limite de velocidad
if (rb.velocity.magnitude > maxSpeed)
{
rb.velocity = rb.velocity.normalized * maxSpeed;
}
#endregion
}
private void Update()
{
//Llamando a mis funciones
FloorOrientation();
}
public void FloorOrientation()
{
if (isGrounded == true)
{
temp = tempOriginal;
Quaternion rotationtemporal = Quaternion.FromToRotation(transform.up, hit.normal) * transform.rotation;
transform.rotation = Quaternion.SlerpUnclamped(transform.rotation, rotationtemporal, Time.deltaTime * NormalGiroVelocidad);
}
else
{
//This is to get it straight after a while in the air with a bad turn
temp -= Time.deltaTime;
if (temp <= 0)
{
Quaternion rotationtemporal = Quaternion.FromToRotation(transform.up, Vector3.up) * transform.rotation;
transform.rotation = Quaternion.Lerp(transform.rotation, rotationtemporal, Time.deltaTime * 1f);
// temp = 1;
}
Physics.Raycast(transform.position, -transform.forward, out hit, 3f, misuelo);
Quaternion rotationtemporalB = Quaternion.FromToRotation(transform.up, hit.normal) * transform.rotation;
transform.rotation = Quaternion.Slerp(transform.rotation, rotationtemporalB, Time.deltaTime * NormalGiroVelocidad);
Physics.Raycast(transform.position, transform.right, out hit, 3f, misuelo);
Quaternion rotationtemporalR = Quaternion.FromToRotation(transform.up, hit.normal) * transform.rotation;
transform.rotation = Quaternion.Slerp(transform.rotation, rotationtemporalR, Time.deltaTime * NormalGiroVelocidad);
Physics.Raycast(transform.position, -transform.right, out hit, 3f, misuelo);
Quaternion rotationtemporalL = Quaternion.FromToRotation(transform.up, hit.normal) * transform.rotation;
transform.rotation = Quaternion.Slerp(transform.rotation, rotationtemporalL, Time.deltaTime * NormalGiroVelocidad);
}
}
Your answer
Follow this Question
Related Questions
Best way to stop a car after a finish line? 1 Answer
Race Positioning system 0 Answers
SCORING DRIFT POINTS 0 Answers
How can we simulate VTEC ( yea the Honda Civic VTEC) ? 0 Answers
Unity & C# - car going a little bit unsmooth on ramp 0 Answers