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 unixty · Sep 23, 2017 at 06:30 PM · raycast2.5dhitbox

2.5D Bullet hitbox detection for distant objects

Hello, I am not even sure how to ask it properly so I will just give you an example

15:28 https://www.youtube.com/watch?v=9krkyc0hAfg

As you can see, the robot is behind the dude, but his bullets still hit it. I want to achieve this same exact thing, 2.5D with full 3D environments and create bullets that hit anything no matter how close or far away from the camera they are. I have really no idea how to achieve this and if you could provide some code I would be very grateful.

Thank you

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
1

Answer by Kabaw · Sep 23, 2017 at 09:28 PM

Hi @unixty,

A good way to achieve what you want is using Raycast. If you Raycast from your bullet position and in the direction the camera is facing, you can detect if your bullet hit some enemy, regardless of how far away from the camera it is.

Below there is a example situation:

In this situation the Character has fired a bullet upwards and using a Raycast, a collision was detected between the bullet and the enemy.

alt text



alt text

Below there is a sample code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Bullet : MonoBehaviour
 {
     public int damage;
     public float rayCastDistance;
     public LayerMask targetsLayerMask;
 
     void Start ()
     {
         // Some initializantion code
     }
 
     void Update ()
     {
         RaycastHit hitInfo;
 
         //If the raycast hit something, the bullet must try to apply damage
         if (Physics.Raycast(transform.position, Camera.main.transform.forward, out hitInfo, rayCastDistance, targetsLayerMask))
         {
             HealthComponent hc = hitInfo.transform.GetComponent<HealthComponent>();
 
             //If the GameObject hit by the ray has a HealthComponent it must receive damage
             if (hc)
             {
                 //The bullet applies damage to the enemy
                 hc.TakeDamage(damage);
 
                 //Destroy the bullet after applying the damage
                 Destroy(gameObject);
             }
         }
 
         //Drawn the raycast in the Scene View
         Debug.DrawLine(transform.position, transform.position + (Vector3.forward * rayCastDistance), Color.red);
     }
 }


In the "distance" variable, you just have to put a high value, in order to ensure that the bullet will hit any thing that is faaaarrrrrrrr away from the camera. But remember that Raycast is performance expensive, so the shorter the distance of the ray the better the performance.

In the "targetsLayerMask" variable you select the layers with which the bullet should collide.

If you also want to check if the bullet is colliding with a enemy that is between the bullet and the camera, you just need to create another Raycast in the opposite direction of the one I've shown in the exemple image.


image03.png (163.6 kB)
image01.png (226.5 kB)
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 unixty · Sep 25, 2017 at 10:52 AM 0
Share

Thanks @$$anonymous$$abaw for your response, but I think I forgot to add one more thing. If the character is riding a bike, that means he is actually moving and I would need to apply some physics to the bullets as well. Also in the video, bullets ignore perspective, they are always "looking" at the camera, like they were created in the UI or something like that.

7:16 would be another perfect example

https://www.youtube.com/watch?v=g_-4cNSQZ$$anonymous$$k

The camera rotates but the bullets are always on the same plane (sorry if I am saying it wrong), they completely ignore pure physics or realism.

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

97 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

Related Questions

My Raycast Won't Fire 1 Answer

2.5D Shooting Rigidbodies vs RayCasts 0 Answers

Get closest point on collider on at a certain z depth 0 Answers

Finding a certain variable is failing 0 Answers

Do raycasts only collide with colliders or can I detect a hit through something else? 2 Answers


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