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
-1
Question by ianmendez · Nov 04, 2016 at 12:52 PM · scripting problemrigidbodyvelocity

Need help fixing a java script

Hey guys, I'm basically new with scripting and I have a scripting problem that a I cannot solve in my java script. What I'm trying to do is to create a car simulation, there is still a lot that I will add to the script in the future to make it more realistic. Also, there is a tiny part of a commentary section that I plan to use with a steering wheel rather than the keyboard as my input.

Here is the code:

 #pragma strict
 var WheelLFCollider : WheelCollider;
 var WheelRFCollider : WheelCollider;
 var WheelLRCollider : WheelCollider;
 var WheelRRCollider : WheelCollider;
 var WheelLF : Transform;
 var WheelRF : Transform;
 var WheelLR : Transform;
 var WheelRR : Transform;
 var lowestSteerAtSpeed : float = 50;
 var lowSpeedSteerAngle : float = 10;
 var highSpeedSteerAngle : float = 1;
 var decelerationSpeed : float = 30;
 var maxTorque : float = 50;
 var currentSpeed : float;
 var topSpeed : float = 150;
 var rb: Rigidbody;
 
 function Start () {
 rb = GetComponent.<Rigidbody>() ;    
 }
 
 function FixedUpdate () {
 Controle ();
 }
 
 function Update() {
 WheelLF.Rotate(WheelLFCollider.rpm/60*360*Time.deltaTime,0,0);
 WheelRF.Rotate(WheelRFCollider.rpm/60*360*Time.deltaTime,0,0);
 WheelLR.Rotate(WheelLRCollider.rpm/60*360*Time.deltaTime,0,0);
 WheelRR.Rotate(WheelRRCollider.rpm/60*360*Time.deltaTime,0,0);
 WheelLF.localEulerAngles.y = WheelLFCollider.steerAngle - WheelLF.localEulerAngles.z;
 WheelRF.localEulerAngles.y = WheelRFCollider.steerAngle - WheelRF.localEulerAngles.z;
 }
 
 function Controle () {
 //var Accel = Input.GetAxisRaw("Accelerator") ;
 //Debug.Log(Accel);
 currentSpeed = 2*22/7*WheelLRCollider.radius*WheelLRCollider.rpm*60/1000;
 currentSpeed = Mathf.Round(currentSpeed);
 
 if (currentSpeed < topSpeed) {
 WheelRRCollider.motorTorque = maxTorque * Input.GetAxis("Vertcial");
 WheelLRCollider.motorTorque = maxTorque * Input.GetAxis("Vertical");
 }
 
 else {
 WheelRRCollider.motorTorque = 0;
 WheelLRCollider.motorTorque = 0;
 }
 
 if (Input.GetButton("Vertical")==false) {
 WheelRRCollider.brakeTorque = decelerationSpeed;
 WheelLRCollider.brakeTorque = decelerationSpeed;
 }
 
 else{
 WheelRRCollider.brakeTorque = 0;
 WheelLRCollider.brakeTorque = 0;
 }
 
 if (Input.GetAxis("Vertical")){
         rb.velocity = Vector3(50,0,0);
         }
 
 var speedFactor = GetComponent<Rigidbody(rb.velocity.magnitude/lowestSteerAtSpeed);
 var currentSteerAngle = Mathf.Lerp(lowSpeedSteerAngle,highSpeedSteerAngle,speedFactor);
 currentSteerAngle *= Input.GetAxis("Horizontal");
 WheelLFCollider.steerAngle = currentSteerAngle;
 WheelRFCollider.steerAngle = currentSteerAngle;
 }
Comment
Add comment · Show 4
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 Zodiarc · Nov 04, 2016 at 12:53 PM 0
Share

Please format the code properly.

avatar image ianmendez · Nov 04, 2016 at 12:55 PM 0
Share

pragma strict

ar WheelLFCollider : WheelCollider; var WheelRFCollider : WheelCollider; var WheelLRCollider : WheelCollider; var WheelRRCollider : WheelCollider; var WheelLF : Transform; var WheelRF : Transform; var WheelLR : Transform; var WheelRR : Transform; var lowestSteerAtSpeed : float = 50; var lowSpeedSteerAngle : float = 10; var highSpeedSteerAngle : float = 1; var decelerationSpeed : float = 30; var maxTorque : float = 50; var currentSpeed : float; var topSpeed : float = 150; var rb: Rigidbody;

function Start () { rb = GetComponent.() ; }

function FixedUpdate () { Controle (); }

function Update() { WheelLF.Rotate(WheelLFCollider.rpm/60*360*Time.deltaTime,0,0); WheelRF.Rotate(WheelRFCollider.rpm/60*360*Time.deltaTime,0,0); WheelLR.Rotate(WheelLRCollider.rpm/60*360*Time.deltaTime,0,0); WheelRR.Rotate(WheelRRCollider.rpm/60*360*Time.deltaTime,0,0); WheelLF.localEulerAngles.y = WheelLFCollider.steerAngle - WheelLF.localEulerAngles.z; WheelRF.localEulerAngles.y = WheelRFCollider.steerAngle - WheelRF.localEulerAngles.z; }

function Controle () { //var Accel = Input.GetAxisRaw("Accelerator") ; //Debug.Log(Accel); currentSpeed = 2*22/7*WheelLRCollider.radius*WheelLRCollider.rpm*60/1000; currentSpeed = $$anonymous$$athf.Round(currentSpeed);

if (currentSpeed < topSpeed) { WheelRRCollider.motorTorque = maxTorque Input.GetAxis("Vertcial"); WheelLRCollider.motorTorque = maxTorque Input.GetAxis("Vertical"); }

else { WheelRRCollider.motorTorque = 0; WheelLRCollider.motorTorque = 0; }

if (Input.GetButton("Vertical")==false) { WheelRRCollider.brakeTorque = decelerationSpeed; WheelLRCollider.brakeTorque = decelerationSpeed; }

else{ WheelRRCollider.brakeTorque = 0; WheelLRCollider.brakeTorque = 0; }

if (Input.GetAxis("Vertical")){ rb.velocity = Vector3(50,0,0); }

var speedFactor = GetComponent

avatar image Zodiarc ianmendez · Nov 04, 2016 at 12:59 PM 0
Share

There's a "Code Sample" button while creating a post (the zeros and ones). Use this. It provides code highlighting. Please edit your original question with this changes.

avatar image ianmendez Zodiarc · Nov 04, 2016 at 01:06 PM 0
Share

I'm sorry about that, here is on of the problems Unity detected:

Assets/Scripts/Carmovement.js(66,41): BCE0024: The type 'UnityEngine.Rigidbody' does not have a visible constructor that matches the argument list '(float)'.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Zodiarc · Nov 04, 2016 at 01:08 PM

Line 66 is wrong. Change it to

 var speedFactor = rb.velocity.magnitude/lowestSteerAtSpeed;

Comment
Add comment · Show 1 · 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 ianmendez · Nov 04, 2016 at 01:12 PM 0
Share

Zodiarc thanks a lot! Now I don't have compiler errors.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Figuring out what the acceleration will be before applying force to RigidBody2D 0 Answers

Using rb.velocity causes low gravity. 2 Answers

Bullet not inheriting speed of player. 0 Answers

C# keep previous velocity while jumping, previous solutions not working 0 Answers

Modifying mass of all objects 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