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 /
  • Help Room /
avatar image
0
Question by SQUARE-BADGER · Aug 20, 2016 at 07:46 AM · scripting problemraycastbullets

i need help with raycast

Hi, i started to make a twin stick shooter and made some bullet prefabs to work with but i discovered that the hit detection is stupid, i found that it could be posible to make bullets with raycast but i am dum at this please help

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Jessespike · Aug 20, 2016 at 08:19 AM

Give it an attempt. Ask for help if you get stuck, as in post your code and describe the problem.

  • https://docs.unity3d.com/ScriptReference/Physics.Raycast.html

  • https://unity3d.com/learn/tutorials/topics/physics/raycasting

  • http://answers.unity3d.com/search.html?f=&type=question&redirect=search%2Fsearch&sort=relevance&q=raycast

  • https://www.google.com/#q=unity+raycast+tutorial

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 SQUARE-BADGER · Aug 20, 2016 at 08:44 AM 0
Share
 using UnityEngine;
 using System.Collections;
 
 public class Gun : $$anonymous$$onoBehaviour {
 
 
     // Use this for initialization
     void Start () {
 
     
     }
     
     // Update is called once per frame
     void Update () {
         
     }
 
     void FixedUpdate()
     {
         RaycastHit hit;
 
         if (Physics.Raycast(transform.position, -Vector3.forward, out hit))
             print("Found an object - distance: " + hit.distance);
 
     }
 
 
     public void Shoot()
     {
         
     }
 }
 

i have made a script that activates the void Shoot, and i want when its activated check if the raycast hit the enemy and decrease its life

avatar image Jessespike · Aug 21, 2016 at 02:52 AM 0
Share

@SQAURE-BADGER

When the raycast hits something, check if it's an enemy. Enemies have a EnemyHealth script attached to them. EnemyHealth stores a int to represent health. If health is 0 or below, then it dies.

Gun.cs

 using UnityEngine;
 using System.Collections;
 
 public class Gun : $$anonymous$$onoBehaviour {
 
     void Update() {
         if (Input.Get$$anonymous$$ouseButtonDown(0))
         {
             Shoot();
         }
     }
 
     public void Shoot() {
         RaycastHit hit;
 
         if (Physics.Raycast(transform.position, transform.forward, out hit))
         {
             print("Found an object - distance: " + hit.distance);
 
             // if hit is an enemy, then reduce it's health
             EnemyHealth enemy = hit.transform.GetComponent<EnemyHealth>();
             if (enemy != null)
             {
                 enemy.ReduceHealth(10);
             }
         }
 
     }
 
 }

here's the EnemyHealth script

 using UnityEngine;
 using System.Collections;
 
 public class EnemyHealth : $$anonymous$$onoBehaviour {
 
     public int health = 100;
 
     void Update() {
         if (health <= 0)
         {
             Destroy(gameObject);
         }
     }
 
     public void ReduceHealth(int amt) {
         health = health - amt;
     }
         
 }
 

avatar image
0

Answer by SQUARE-BADGER · Aug 21, 2016 at 09:35 PM

wow thanks

Comment
Add comment · 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

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

93 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

Related Questions

Raycast not taking full length 0 Answers

Scripting Errors 1 Answer

Raycast not detected on instantiated object 0 Answers

I'm trying to use a ray to grab objects but, my script isn't working? 0 Answers

Raycast Enemy AI shooting script 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