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
-2
Question by MadJohny · Jul 27, 2013 at 06:42 PM · c#raycastraycastall

Bullets sometimes don't work

Hi, I've followed a tutorial so I could get bullets on my game, since I actually wanted them to travel overTime and not an instant raycast, I didn't wanted gravity tho.

As I said in another question, my bullets are bigger than his, so I am using 3 raycasts instead of just one

 using UnityEngine;
 using System.Collections;
 
 public class Projectile : MonoBehaviour {
 
     public float speed = 100f;
     public float lifeTime = 7f;
     public GameObject decalHitWall;
     
     [HideInInspector]
     public Vector3 moveDirection;
     [HideInInspector]
     public float shortestSoFar;
     
     [HideInInspector]
     public Vector3 instantiatePoint;
     [HideInInspector]
     public Quaternion instantiateRotation;
     [HideInInspector]
     public bool foundHit = false;
     
     public bool hasHitted = false;
     public bool damagePlayer = false;
     public int damage = 20;
     
     public float widht = 0f;
     
     void Awake () {
         moveDirection = transform.forward * speed;
         shortestSoFar = Mathf.Infinity;
         foundHit = false;
         
     }
     
     void Update () {
         if (!hasHitted){
             transform.rotation = Quaternion.LookRotation(moveDirection);
             RaycastHit[] hitsCenter;
             hitsCenter = Physics.RaycastAll(transform.position, transform.forward, speed * Time.deltaTime);
             foreach (var hit in hitsCenter){
                 float tempDistance = Vector3.Distance(transform.position, hit.point);
                 if(tempDistance < shortestSoFar){
                     instantiatePoint = hit.point;
                     instantiateRotation = Quaternion.LookRotation (hit.normal);
                     shortestSoFar = Vector3.Distance(transform.position, hit.point);
                     foundHit = true;
                     if(hit.transform.tag == "Enemy" && !damagePlayer){
                         hit.transform.parent.GetComponent<EnemyScript>().health -= damage;
                         shortestSoFar = Vector3.Distance(transform.position,hit.point);
                         GameObject.Find("Player").GetComponentInChildren<Player>().energy += 3;                    
                         Destroy (transform.root.gameObject);
                         GameObject spawnParticles = (GameObject)Instantiate (decalHitWall, instantiatePoint, instantiateRotation);
                     }
                     if(hit.transform.tag == "Player" && damagePlayer){
                         hit.transform.GetComponentInChildren<Player>().health -= damage;
                         shortestSoFar = Vector3.Distance(transform.position,hit.point);
                         Destroy (transform.root.gameObject);
                     }
                     if(hit.transform.tag == "LevelPart"){
                         shortestSoFar = Vector3.Distance(transform.position,hit.point);                    
                         Destroy (transform.root.gameObject);
                         GameObject spawnParticles = (GameObject)Instantiate (decalHitWall, instantiatePoint, instantiateRotation);
                     }
                     
                     if(hit.transform.tag == "Spectator" && !damagePlayer){
                         hit.transform.parent.GetComponent<SpectatorScript>().health -= damage;
                         shortestSoFar = Vector3.Distance(transform.position,hit.point);                
                         Destroy (transform.root.gameObject);
                         GameObject spawnParticles = (GameObject)Instantiate (decalHitWall, instantiatePoint, instantiateRotation);
                     }                
                 }
             }
             
             RaycastHit[] hitsLeft;
             hitsLeft = Physics.RaycastAll(new Vector3 (transform.position.x - widht, transform.position.y, transform.position.z), transform.forward, speed * Time.deltaTime);
             foreach (var hit in hitsLeft){
                 float tempDistance = Vector3.Distance(transform.position, hit.point);
                 if(tempDistance < shortestSoFar){
                     instantiatePoint = hit.point;
                     instantiateRotation = Quaternion.LookRotation (hit.normal);
                     shortestSoFar = Vector3.Distance(transform.position, hit.point);
                     foundHit = true;
                     if(hit.transform.tag == "Enemy" && !damagePlayer){
                         hit.transform.parent.GetComponent<EnemyScript>().health -= damage;
                         shortestSoFar = Vector3.Distance(transform.position,hit.point);
                         GameObject.Find("Player").GetComponentInChildren<Player>().energy += 3;                    
                         Destroy (transform.root.gameObject);
                         GameObject spawnParticles = (GameObject)Instantiate (decalHitWall, instantiatePoint, instantiateRotation);
                     }
                     if(hit.transform.tag == "Player" && damagePlayer){
                         hit.transform.GetComponentInChildren<Player>().health -= damage;
                         shortestSoFar = Vector3.Distance(transform.position,hit.point);
                         Destroy (transform.root.gameObject);
                     }
                     if(hit.transform.tag == "LevelPart"){
                         shortestSoFar = Vector3.Distance(transform.position,hit.point);                    
                         Destroy (transform.root.gameObject);
                         GameObject spawnParticles = (GameObject)Instantiate (decalHitWall, instantiatePoint, instantiateRotation);
                     }
                     
                     if(hit.transform.tag == "Spectator" && !damagePlayer){
                         hit.transform.parent.GetComponent<SpectatorScript>().health -= damage;
                         shortestSoFar = Vector3.Distance(transform.position,hit.point);                
                         Destroy (transform.root.gameObject);
                         GameObject spawnParticles = (GameObject)Instantiate (decalHitWall, instantiatePoint, instantiateRotation);
                     }    
                 }
             }
             
             RaycastHit[] hitsRight;
             hitsRight = Physics.RaycastAll(new Vector3 (transform.position.x + widht, transform.position.y, transform.position.z), transform.forward, speed * Time.deltaTime);
             foreach (var hit in hitsRight){
                 float tempDistance = Vector3.Distance(transform.position, hit.point);
                 if(tempDistance < shortestSoFar){
                     instantiatePoint = hit.point;
                     instantiateRotation = Quaternion.LookRotation (hit.normal);
                     shortestSoFar = Vector3.Distance(transform.position, hit.point);
                     foundHit = true;
                     if(hit.transform.tag == "Enemy" && !damagePlayer){
                         hit.transform.parent.GetComponent<EnemyScript>().health -= damage;
                         shortestSoFar = Vector3.Distance(transform.position,hit.point);
                         GameObject.Find("Player").GetComponentInChildren<Player>().energy += 3;                    
                         Destroy (transform.root.gameObject);
                         GameObject spawnParticles = (GameObject)Instantiate (decalHitWall, instantiatePoint, instantiateRotation);
                     }
                     if(hit.transform.tag == "Player" && damagePlayer){
                         hit.transform.GetComponentInChildren<Player>().health -= damage;
                         shortestSoFar = Vector3.Distance(transform.position,hit.point);
                         Destroy (transform.root.gameObject);
                     }
                     if(hit.transform.tag == "LevelPart"){
                         shortestSoFar = Vector3.Distance(transform.position,hit.point);                    
                         Destroy (transform.root.gameObject);
                         GameObject spawnParticles = (GameObject)Instantiate (decalHitWall, instantiatePoint, instantiateRotation);
                     }
                     
                     if(hit.transform.tag == "Spectator" && !damagePlayer){
                         hit.transform.parent.GetComponent<SpectatorScript>().health -= damage;
                         shortestSoFar = Vector3.Distance(transform.position,hit.point);                
                         Destroy (transform.root.gameObject);
                         GameObject spawnParticles = (GameObject)Instantiate (decalHitWall, instantiatePoint, instantiateRotation);
                     }    
                 }
             }
         }
         
         transform.position += moveDirection * Time.deltaTime;
         
         lifeTime -= Time.deltaTime;
         
         if(lifeTime <= 0f){
             Destroy(gameObject);
         }
     }
 } 

Also, I made the code in c# where eteeki made it in js.

The bullets work fine most times but sometimes they just go throught enemies, I don't know what is wrong with the script, can soomeone help me?

Thaks in advance.

Edit: here's a webplayer version: https://dl.dropboxusercontent.com/u/151460612/webplayer/webplayer.html Ok this problem is actually almost never happenning, which is good, but at the same time it can possibily happen, which is bad, and why am I getting so many downvotes give me a reason please

Comment
Add comment · Show 6
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 Jonathon82931 · Jul 27, 2013 at 06:55 PM 0
Share

why do you use [HideInInspector]rather then using private ins$$anonymous$$d of public? dose HideInInspector make it behave as a public but not be seen in the inspector?

avatar image Jamora · Jul 27, 2013 at 07:09 PM 2
Share

It does exactly that, Jonathon. It helps prevent cluttered inspectors when you take the shortcut of not program$$anonymous$$g your properties/getters/setters...

avatar image $$anonymous$$ · Jul 27, 2013 at 07:31 PM 1
Share

$$anonymous$$ake your own script, and you will at least understand your mistake

avatar image Jonathon82931 · Jul 27, 2013 at 07:33 PM 0
Share

thanks I was wondering that seems useful

avatar image MadJohny · Jul 27, 2013 at 07:59 PM 0
Share

Le Vailus so you downvoted me and just say to make my own script and don't even explain what is wrong?

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by RyanZimmerman87 · Jul 27, 2013 at 10:34 PM

First thing I would try is increasing the size of the collider you are hitting or even better slowing down the speed of the bullets. If it is only hitting some of the time chances are that your bullets are moving so fast that they may pass through the collider before Unity even detects them in a cycle.

I didn't read your question or code throughly enough but that would be my first guess. If that doesn't work I will try to edit my answer to go through other possibilities.

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 MadJohny · Jul 27, 2013 at 10:44 PM 0
Share

the bullet was already pretty slow compared to real bullets, because it's supposed to be a slow projectile, also, the collider that I'm trying to hit it's the enemies, sometimes it goes throught the smallest ones, but it sometimes can go right throught the biggest ones, which are actually pretty big, I'll upload my question with a webplayer, unfortunatly, the link may get broken, because dropboxs cancels links that are getting irregular traffic

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

18 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

Related Questions

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

Raycast - arrow stopped by walls (layer) but not by units (layer) 1 Answer

2 raycasts on the fpwalker/camera 1 Answer

how do i make my objects not spawn on already spawned objects 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