Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 megustalp26 · Feb 17, 2021 at 04:23 PM · carrigidbody.addforcerigidbody.velocitycar gamecar physics

Car getting Stuck when having 0 velocity,Rigidbody getting Stuck while having 0 velo.

Hi My Game is an Arcade Racing game. thats why i want to use addforce for forward and backward movement. My Problem is that the Car/Rigidbody is getting stuck if the Car has 0 velocity. it start turning upside down and may get unstuck after a couple of forward/backward keystrokes. Making the player spawn in mid air and dropping down fixes the issue if playerinput is there before hitting the ground. I am Using addforce only for forward and backwards. Turning is determind by the steeringangle with wheel colliders (2 wheels in front). rear wheels for force.

I am pretty new to C# and Unity in general. And i couldnt find anything that would make the player not get stuck there. maybe you guys can help me

Comment
Add comment · Show 1
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
avatar image megustalp26 · Feb 17, 2021 at 03:28 PM 0
Share

Here is the Script im using for the back wheels

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class SimpleCarController : MonoBehaviour { private Rigidbody rb;

 void Start()
 {
     rb = GetComponent<Rigidbody>();
 }
 public void GetInput()
 {
     m_horizontalInput = Input.GetAxis("Horizontal");
     m_verticalInput = Input.GetAxis("Vertical");
 }

 
 public void Accelerate()

 {

 
     if (Input.GetKey(KeyCode.W) || ButtonF.Forward == 1)
     {
         Vector3 forceToAdd = transform.forward;
         forceToAdd.y = 0;
         rb.AddForce(forceToAdd * speed * 10);
     }
     else if (Input.GetKey(KeyCode.S) || ButtonB.Back == 1)
     {
         Vector3 forceToAdd = -transform.forward;
         forceToAdd.y = 0;
         rb.AddForce(forceToAdd * speed * 10);
     }

     Vector3 locVel = transform.InverseTransformDirection(rb.velocity);
     locVel = new Vector3(0, locVel.y, locVel.z);
     rb.velocity = new Vector3(transform.TransformDirection(locVel).x, rb.velocity.y, transform.TransformDirection(locVel).z);




     // Abruf des Gameobjektes 
     var gmj = GameObject.Find("CountdownText");
     //Nullcheck um Nullrefex zu vermeiden
     if (gmj != null)
     {
         //Abruf des Script objetes aus dem Gameobject
         var comp = gmj.GetComponent<CountdownTimer>();
         if (comp != null)
         {
             //Script Aktivitätscheck
             if (!comp?.countdownActive ?? false)
             {
                 rb.velocity = new Vector3(0, 0, transform.TransformDirection(locVel).z);
             }
         }
     }



 }



 private void UpdateWheelPoses()
 {
     UpdateWheelPose(rearDriverW, rearDriverT);
     UpdateWheelPose(rearPassengerW, rearPassengerT);
 }

 private void UpdateWheelPose(WheelCollider _collider, Transform _transform)
 {
     Vector3 _pos = _transform.position;
     Quaternion _quat = _transform.rotation;

     _collider.GetWorldPose(out _pos, out _quat);

     _transform.position = _pos;
     _transform.rotation = _quat;
 }


 private void FixedUpdate()
 {
     Accelerate();
     UpdateWheelPoses();
     GetInput();



 }


 private float m_horizontalInput;
 private float m_verticalInput;
 private float m_steeringAngle;
 public float speed;

 
 public WheelCollider rearDriverW, rearPassengerW;
 
 public Transform rearDriverT, rearPassengerT;
 
 public float motorForce = 50;
 public float gravityMultiplier;
 

}

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

155 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

Related Questions

car with wheel collider fall thought road collider 1 Answer

simple car drift system 0 Answers

How to stop the Unity Skycar Car without ending up in reversing? 0 Answers

Ball control like kula world 2 Answers

How can I make the character rotating the steering wheel inside the car? 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