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 /
This question was closed Oct 22, 2015 at 11:29 PM by zckhyt.
avatar image
0
Question by zckhyt · May 11, 2015 at 12:17 PM · collisionvector3physics.raycast

Offsetting Raycasting

I'm experiencing an issue with 3D raycasting in a 2D setting. The player can create a projectile relative to their position, which destroys itself if it hits an enemy and passes damage information to the enemy it hits. The player and enemies cannot collide (enemies, however, cannot overlap each other). The projectile uses raycasting to check for collisions along the x-axis. So the issue is that when the projectile is spawned too close to an enemy, or even inside an enemy, the raycasting will not hit the object and just pass through it.

My attempted solution was to change the axis where collisions would be detected to the z-axis. That, however, requires an offset to point where the raycast begins.

Here is a snippet of the code where I do the raycasting (part of a function called from update):

     if (Physics.Raycast(transform.position + new Vector3(0, 0, 5), -Vector3.forward, out hit, Mathf.Infinity, -1))
     {
         if (hit.transform.tag == "AI")
         {
             print("Collision Detected On Z-Axis");
             hit.transform.GetComponent<AIBehavior>().SendMessage("takeDamage", dmgMod);
             Destroy(this.gameObject);
         }
     }

Should I check the x-axis with this, the collision detection never finds anything which makes me think that adding the vector3 to it is doing something, but I'm still at lost on how to make this work.

Any thoughts?

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

  • Sort: 
avatar image
0

Answer by FortisVenaliter · May 12, 2015 at 09:28 AM

Honestly, I would add a simple circle-collision check (the size of the projectile) to the first frame to see if it hits anyone other than the shooter. If the circle-collision fails, then switch to the ray. That should make sure that if it starts in an enemy it will still hit them.

Alternately, you could move the origin of the ray the opposite direction to move the origin back, such as:

 Vector3 rayFwd = new Vector3(1,0,0);
 Ray ray = new Ray(transform.position - (rayFwd * 5), rayFwd);

But that may have unintended consequences of hitting enemies BEHIND the gun, so that may not be ideal.

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 zckhyt · May 16, 2015 at 04:13 PM 0
Share

Interesting idea, I'll give it a shot to see if I like it any better than my current fix of simply raycasting from the player's position when it first starts up to the point where the object itself spawned. That seems to be working well enough, though.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

Basic LineRender Reflection 2 Answers

(Steam VR / Vive) Rigidbody moving in only one direction after collision 1 Answer

Instantiate after collision 2 Answers

Collision.contacts? 1 Answer

how detect the angle of collision on disc object ? 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