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 /
avatar image
0
Question by gamer_2199 · Jun 07, 2018 at 11:11 AM · rigidbodytransformdistance

Maintaining specific distance between two objects

So my game has two cars connected to each other via a script. The wheels of the cars have wheel colliders. Another script controls the movement of the cars such that when i press the forward button car1 goes ahead and car2 goes behind it in reverse (My game is only based on z axis). And when i press the reverse button car2 goes ahead and car1 in reverse. Now when i press the forward button, car2 reverses faster and collides with car1. Same goes when i press the reverse button. I want to maintain a specific distance between the two cars so that they dont collide at all. I am not able to figure how i can make this possible. Should I try by controlling the speed of the car?

This is my CarControllerScript. I have made two classes which contain reference for wheel colliders and wheels. Both of these classes contain Top Speed, current Speed, max torque and forward variables. (I know the rigidbody parameter in the function is not being used. I am going to remove it later. )

The Car on the left is Car1.alt text

 public Car car1;  
 public Car2 car2;  

 public void Forward (Rigidbody rb) {

     //Formula for calculating speed
     car1.currentSpeed = 2 * 22 / 7 * car1.WheelBL.radius * car1.WheelBL.rpm * 60 / 1000;
     car2.currentSpeed = 2 * 22 / 7 * car2.WheelBL.radius * car2.WheelBL.rpm * 60 / 1000; 

     if (car1.currentSpeed < car1.topSpeed && car2.currentSpeed < car2.topSpeed){

         // Run the back wheels of Car1
         car1.WheelBL.motorTorque = car1.maxTorque * car1.forward;
         car1.WheelBR.motorTorque = car1.maxTorque * car1.forward;

         // Reverse Car2
         car2.WheelBL.motorTorque = -car2.maxTorque * car2.forward;
         car2.WheelBR.motorTorque = -car2.maxTorque * car2.forward;
     }
 }

 
 public void Reverse (Rigidbody rb) {

     //Formula for calculating speed
     car2.currentSpeed = 2 * 22 / 7 * car2.WheelBL.radius * car2.WheelBL.rpm * 60 / 1000;
     car1.currentSpeed = 2 * 22 / 7 * car1.WheelBL.radius * car1.WheelBL.rpm * 60 / 1000;

     if (car2.currentSpeed < car2.topSpeed && car1.currentSpeed < car1.topSpeed){

         // Run the back wheels of Car2
         car2.WheelBL.motorTorque = car2.maxTorque * car2.forward;
         car2.WheelBR.motorTorque = car2.maxTorque * car2.forward;
                     // Reverse Car1
         car1.WheelBL.motorTorque = -car1.maxTorque * car1.forward;
         car1.WheelBR.motorTorque = -car1.maxTorque * car1.forward;
     }
 }


untitled.png (40.7 kB)
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 BIO_K · Jun 07, 2018 at 01:00 PM

I can give you bit from a script I made... I check all my distances using this... You can ofcourse tweak it however...

if (Vector3.Distance(player.position, thisTransform.position) < 3)

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 gamer_2199 · Jun 09, 2018 at 11:45 AM 0
Share

I will try it out and let you know!

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

126 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

Related Questions

Parenting not working with massive force to rigidbody. Does having rigidbody on child screw things up? 1 Answer

Get direction of moving sphere? 1 Answer

Move object upon hitting camera ray 1 Answer

Moving object with transform.position ignore other objects even if they collided 1 Answer

Instantiated projectile always floats up 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