Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by albertovillena10 · Mar 30, 2020 at 04:47 PM · physicscar gameracing game

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);
         }
     }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

279 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges