Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by megustalp26 · Feb 17, 2021 at 06:44 PM · rigidbodycarracing gameaddforce movement

My Car keeps not moving when its already at 0 Velocity HELP

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`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;
 

}`

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by MUG806 · Feb 17, 2021 at 09:19 PM

I think your problem is perhaps in this section:

 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);

Looks like you are trying to remove all sideways velocity of the car? But depending on the roll angle of the car you might be having strange interactions with the vertical component of the velocity. Maybe try removing that code temporarily and see if the problem goes away.

Comment
Add comment · Share
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

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

160 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

Related Questions

How do I check the direction a rigidbody is facing from North and then addforce so it moves in that direction? 1 Answer

changing gravity for one object. 5 Answers

question with car physics; car slowly falls 1 Answer

Raycast car rig going up ramps/slopes. 0 Answers

Are y-axis wheelcolliders possible? 1 Answer


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