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 kkrac · Jan 17, 2017 at 11:47 AM · 2draycast2d gameraycastingprojectile

Raycasting not working as expected: rays are perpendicular to projectile's trajectory and collision doesn't occur

Hi,

I'm having a problem with raycasting and object colliding and not a clue what it is. It's difficult to explain, so I've uploaded my sample project. This is the link:

https://drive.google.com/open?id=0B8Gqj5xq7RSnaUlGNjFUM0JJcVE

The problem is the following: when the player gameObject shoots a bullet, the ray the bullet casts is perpendicular to the bullet instead of being in the same path as the bullet. That causes the collisions not to work as expected.

This is a screen capture of the view from above that shows what I'm talking about.

alt text

I thought that how the projectile's ray was being instantiated in the Projectile.cs class (line 93) could be the problem. So I tried changing that from this...

Ray ray = new Ray (transform.position, transform.forward);

to this... Ray ray = new Ray (transform.position, Vector3.forward);

When doing that, the enemy diagonal to the player is actually hit. BUT the enemy that's positioned in z=0 isn't: the bullet just goes through him.

Again, not sure why the raycasts are perpendicular to the bullet's trajectory. I guess that's the problem but not sure why it happens.

Any ideas? Thanks in advance!

ray1.png (71.2 kB)
Comment
Add comment · Show 1
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 doublemax · Jan 17, 2017 at 11:52 AM 1
Share
 Ray ray = new Ray (transform.position, transform.forward);

Is this a 2D game? Then try transform.right. If it's a 3D game make sure the bullet object points in the right direction, the blue arrow in the editor shows the "forward" vector.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by kkrac · Jan 17, 2017 at 01:54 PM

Thanks @doublemax

Yes, some minutes later after posting the question, I tried transform.right and it worked. It is a platformer with an ortographic camera, but I work with the "z" axis too to shoot enemies not only sideways but also in the distance.

I will check the projectile is pointing in the right direction. I was pretty sure it was, but I will double check.

dumb question: in a 2d game doesn't the projectile have to point to its forward vector too, which would be to the right of the screen? In which case, transform.forward shouldn't it work in that case?

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 doublemax · Jan 17, 2017 at 01:59 PM 0
Share

I don't know what exactly Unity does in the background in 2D mode, but in general it's the same 3D engine running. $$anonymous$$y guess is that transform.forward always points in Z direction.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Raycast hits and then doesn't fire again? 2D Game Raycasting Problem 1 Answer

Physics.Raycast doesn't hit anything even though Debug.DrawRay works 1 Answer

2D click on object with a raycast not working 2 Answers

Problems with 2D RaycastHit.Collider 2 Answers

Raycast to ignore caster (Not by layer) 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