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 /
  • Help Room /
avatar image
0
Question by antonionae8 · May 23 at 01:51 PM · c# tutorial

Somethin wrong with my script?

Hi! I've recently made a script for my car movement and discovered some issues with it. Maybe you have some solutions?

  1. The car acceleration is very inconsistent ( accelerates slow and the top speed is way too high ) and also my brakes respond too late.

  2. The rigidbody is making my car move weirdly( and if I increase the mass my car just goes straight through the ground )

Maybe I've done something wrong with my code or I didn't implement something into it? This is the code :

using UnityEngine; public class player : MonoBehaviour { [SerializeField] private Transform rsf; [SerializeField] private Transform rdf; [SerializeField] private Transform rsb; [SerializeField] private Transform rdb; [SerializeField] private WheelCollider crsf; [SerializeField] private WheelCollider crdf; [SerializeField] private WheelCollider crsb; [SerializeField] private WheelCollider crdb; [SerializeField] private float turnAngle = 5; [SerializeField] private float speed = 1; private float forwardValue; private float rightValue; private float currentSpeed; [SerializeField] private Rigidbody rigidbody; public float GetCurrentSpeed() { return currentSpeed; } public static player Player; void MovePlayer() { //transform.Translate(Vector3.forward * forwardValue * speed * Time.deltaTime); //transform.Translate(Vector3.right * rightValue * speed * Time.deltaTime); //transform.Rotate(Vector3.up * rightValue * rotationSpeed * Time.deltaTime); } void GetInput() { forwardValue = Input.GetAxis("Vertical"); rightValue = Input.GetAxis("Horizontal"); } // Update is called once per frame void Update() { GetInput(); Accelerate(); Steer(); WheelsUpdate(); } void Accelerate() { crsb.motorTorque = forwardValue * speed; crdb.motorTorque = forwardValue * speed; currentSpeed = rigidbody.velocity.magnitude; } void Steer() { crsf.steerAngle = rightValue * turnAngle; crdf.steerAngle = rightValue * turnAngle; } void WheelsUpdate() { GetSingleWheel(crsf, rsf); GetSingleWheel(crdf, rdf); GetSingleWheel(crsb, rsb); GetSingleWheel(crdb, rdb); } void GetSingleWheel(WheelCollider wheel, Transform wheelTransform) { Vector3 pos; Quaternion rot; wheel.GetWorldPose(out pos, out rot); wheelTransform.position = pos; wheelTransform.rotation = rot; } private void Start() { Player = this; if (rigidbody == null) { rigidbody = transform.GetComponent<Rigidbody>(); } } }

And this is a video with the car and its weird movement : https://imgur.com/a/smfwSfB

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by antonionae8 · May 24 at 06:35 PM

Here is the same code but shown better:

using UnityEngine;

public class player : MonoBehaviour {

 [SerializeField] private Transform rsf;
 [SerializeField] private Transform rdf;
 [SerializeField] private Transform rsb;
 [SerializeField] private Transform rdb;


 [SerializeField] private WheelCollider crsf;
 [SerializeField] private WheelCollider crdf;
 [SerializeField] private WheelCollider crsb;
 [SerializeField] private WheelCollider crdb;

 [SerializeField] private float turnAngle = 5;
 [SerializeField] private float speed = 1;
 private float forwardValue;
 private float rightValue;
 private float currentSpeed;
 [SerializeField] private Rigidbody rigidbody;

 public float GetCurrentSpeed()
 {
     return currentSpeed;
 }

 public static player Player;
 void MovePlayer()
 {
     //transform.Translate(Vector3.forward * forwardValue * speed * Time.deltaTime);
     //transform.Translate(Vector3.right * rightValue * speed * Time.deltaTime);
     //transform.Rotate(Vector3.up * rightValue * rotationSpeed * Time.deltaTime);
 }

 void GetInput()
 {
     forwardValue = Input.GetAxis("Vertical");
     rightValue = Input.GetAxis("Horizontal");
 }
 // Update is called once per frame
 void Update()
 {
     GetInput();
     Accelerate();
     Steer();
     WheelsUpdate();
 }
 void Accelerate()
 {
     crsb.motorTorque = forwardValue * speed;
     crdb.motorTorque = forwardValue * speed;

     currentSpeed = rigidbody.velocity.magnitude;
 }
 void Steer()
 {
     crsf.steerAngle = rightValue * turnAngle;
     crdf.steerAngle = rightValue * turnAngle;
 }
 void WheelsUpdate()
 {
     GetSingleWheel(crsf, rsf);
     GetSingleWheel(crdf, rdf);
     GetSingleWheel(crsb, rsb);
     GetSingleWheel(crdb, rdb);
 }
 void GetSingleWheel(WheelCollider wheel, Transform wheelTransform)
 {
     Vector3 pos;
     Quaternion rot;

     wheel.GetWorldPose(out pos, out rot);

     wheelTransform.position = pos;
     wheelTransform.rotation = rot;
 }
 private void Start()
 {
     Player = this;

     if (rigidbody == null)
     {
         rigidbody = transform.GetComponent<Rigidbody>();
     }
 }

}

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
avatar image
0

Answer by rh_galaxy · May 24 at 11:25 AM

Your code is not readable (please select it and press the 101010 button), but if you say your car goes through ground if you increase mass seems like something is wrong with the colliders or collision detection. Rigidbody Collision Detection continuous and interpolate is probably what you want, now you have Discrete and No interpolation, be sure to change that, and also don't set the Project Settings->Physics->Default Contact Offset to low. The rest is a matter of adjusting Engine Power, Mass, Drag and Gravity to get the movement you want, and the wheel physics. Before you do that be sure to have the scale of your world and objects right, because if you change distances all of the above parameters need to be readjusted to make the car move the same, and it is not an easy task.

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

185 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

Related Questions

getting unexpected symbol ';' error 1 Answer

Anyone have any experience coding 5DT gloves in C# for functions in Unity or know where I could get examples? 0 Answers

find valid words in randomly generated characters in c# unity 0 Answers

A way to move "Highscore" Text to Game Over Screen? 1 Answer

Is there a way to run Destroy(gameObject) after the game has run the previous code? 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