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
2
Question by Stormizin · May 04, 2012 at 01:18 PM · collisionraycastcolliderray

Someone can help with Ray/Collide?

Hello people, im getting some issues with my script, i have a prefab called Car, i instantiate in some positions in the streets of the city, and we have semaphores, i have a script who can check every frame in Update() if the car have some collider in front he stop. But the car stop, and the other hits the car ahead, then the other stops, then the other hits, stop, hit, stop, hit, i dont know what is going on. I want only the cars to stop in every collider.

     void Update(){
     if(carSpeed){
         transform.Translate(0,0,Time.deltaTime * -10);
     }
     if(collideDetect){
         carSpeed = false;    
     }
     else{ carSpeed = true; }
     
     RaycastHit hit;
     if (Physics.Raycast(transform.position, new Vector3(0,0,20), out hit)) 
     {
         if(hit.collider.gameObject.name.Contains("Semaphore Trigger") && (hit.distance < 2.5f) ||
             hit.collider.gameObject.tag.Contains("Car") && (hit.distance < 2.5f))
         {
             collideDetect = true;
         }
     }
         else { collideDetect = false; }
 }

someone have an idea?

Comment
Add comment · Show 6
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 Stormizin · May 04, 2012 at 01:13 PM 0
Share

It's like a "Traffic System".

avatar image syclamoth · May 04, 2012 at 02:01 PM 0
Share

Set up something for synchronising it better. Try making the cars slow down gradually as they approach objects- the speed they move at should be proportionate to the distance to the nearest obstacle. That way, when you have cars travelling behind each other, you don't get the stop-start thing (except for a subtle speed change).

avatar image Stormizin · May 04, 2012 at 02:04 PM 0
Share

maybe a rigidbody.velocity will be a great helper here or you have any better idea?

avatar image fafase · May 04, 2012 at 02:10 PM 0
Share

You can easily retrieve the distance between two cars, using raycast and then hit.distance. Then you can apply a speed depending on that distance, simply

if(hit.distance

If you have a constant speed for the car, simply apply a stronger counterForce as it gets closer. So that

if (distance < 2m) counterforce = pushing force -> 0

avatar image Stormizin · May 04, 2012 at 02:34 PM 0
Share

Dosen't work with force or something like that, the cars continue sometimes to hit the others.

Show more comments

1 Reply

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

Answer by aldonaletto · May 05, 2012 at 04:12 PM

You're translating the object in its forward direction at -10 units/second (Translate assumes local space by default), but you're doing a raycast in the world forward direction - thus when the car isn't going in the world +Z direction the raycast fail miserably. You could use the car's forward direction instead:

 if (Physics.Raycast(transform.position, transform.forward, out hit, 20)){
   ...

Another thing: you must specify the raycast range in the 4th parameter - the direction vector length doesn't make any difference.
NOTE: The car seems to be moved backwards (you're using velocity = -10). No matter why (and if) you're doing this, the ray must be cast in the direction the object is going - in other words, maybe you should use:

 if (Physics.Raycast(transform.position, -transform.forward, out hit, 20)){
   ...
Comment
Add comment · Show 2 · 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 aldonaletto · May 05, 2012 at 05:25 PM 0
Share

$$anonymous$$y bad! The answer was posted incomplete - now it's ok.

avatar image Stormizin · May 06, 2012 at 05:23 AM 0
Share

Thanks buddy this help me. But i make a simple small cube collider below the car to the line hit the collider. Thanks for your time (:

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How can I determine how many colliders exist along a line? 1 Answer

Help with collision detection with specific colliders 1 Answer

Fast object on collider not working 1 Answer

How would I find the closest Vector3 point to a given Ray? 2 Answers

Raycast goes through 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