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 CaptainIcy281 · Mar 21, 2012 at 02:37 PM · movementraycastcollider

Raycasts left/right problem. One works, other doesn't...?

Hello again. I'm back with yet another problem. I have a good move script that is working pretty well, using Translate, Raycasts, and a rigidbody for some other things. It works great! Except for one problem with the Raycasts.

I'm telling it to check left and right every frame to see if there's a collider right next to the ship and if so, the ship's speed = 0, else it equals 15 (the normal speed). So anyway, the problem is this.

See this video I made:

http://youtu.be/AO6w-EhzI7A

The only part of my script that matters:

 var lft = transform.TransformDirection (Vector3.left);
 var rgt = transform.TransformDirection (Vector3.right);

  if (Physics.Raycast (transform.position, lft, 1.8))
  {
  speed = 0;
  }else
  {
  speed = 15;
  }
 
  if (Physics.Raycast (transform.position, rgt, 1.8))
  {
  speed = 0;
  }else
  {
  speed = 15;
  }
  
 }

If I take off either the LEFT raycast or the RIGHT raycast, the one that is actually in there works fine, as seen in the video. If they're both in there together (as they are above in my code) then one works and the other doesn't. It just glitches like in the video.

Anyone got any ideas? Surely I'm doing something wrong, but it's weird that one of them works perfectly fine and the other doesn't, when they are the EXACT same code, just reversed…

Thanks!

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
2
Best Answer

Answer by by0log1c · Mar 21, 2012 at 03:04 PM

Just pointing out that both raycast are fighting one another. Whatever the first one does will be overwritten by the second. Try putting both Raycast in the same if() condition, IE:

 if(Raycast1() || Raycast2())
 {
     DoStuff();
 }
 else
 {
     DoOtherStuff();
 }
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 Bunny83 · Mar 21, 2012 at 03:09 PM 0
Share

And that's exactly the problem. No matter what the first raycast is doing, only the second one will be effective. The second one decides if the speed is 0 or 15.

ps. @CaptainIcy: just out of curiosity, is there a reason why you don't want to use colliders / physics? The CharacterController or a Rigidbody will handle collisions automatically.

avatar image CaptainIcy281 · Mar 21, 2012 at 03:25 PM 0
Share

You know what? That's exactly what was wrong. I should've realized. I had a reason I wasn't doing it that way, because before I started setting the horizontal speed to 0, I was telling it to Translate it the opposite way a certain speed, so that it would continually just bounce back. So it using this method wouldn't have worked with that. It's working now. Thanks. :P

Also, I want the kind of control I can only get using Translate. Rigidbodies are a little too hard to deal with, and it's physics-based. I don't need physics in my game. Just simple colliding. And as for the character controller, it would work fine, except it uses a capsule collider, and for my ship, that just wasn't going to work. I spent a week trying to get it to work and finally realized it wasn't feasible with a capsule collider handling the gravity/collisions. I needed a precise box collider to make it work correctly.

Thanks for the help. I guess I forgot to turn on my brain today. :p

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Questions about movement on uneven/slopped terrain and terrain coliders 0 Answers

Raycasting on Moving Colliders 0 Answers

Move player to specific location and avoid obstacle. 2 Answers

Moving objects using raycasting? 1 Answer

Making object found with raycast a child of the object the ray is cast from 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