Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 May 22, 2020 at 02:50 AM by Symbie.
avatar image
0
Question by Symbie · May 21, 2020 at 01:48 AM · raycastbulletraycasthittrail

Raycast visible bullets

I am fairly new to unity and trying to create a simple fps game. at the moment i have a raycast to detect if i hit an enemy, this is working fine, however you cannot see a bullet or a trail. (i know that raycasts are not visible) how could i make it so you can see a bullet or a bullet trail? would the best way to do this be to create an object that follows the path of the raycast when i shoot? if so, how do i do this, and if not what is the best way? I think these are the relevant scrips that i have: The PlayerShoot script, which contains the raycast:

 using System.Collections;
 using UnityEngine;
 
 public class PlayerShoot : MonoBehaviour
 {
 
     public PlayerWeapon weapon;
 
     [SerializeField]
     private Camera cam;
 
     [SerializeField]
     private LayerMask mask;
 
     void Start()
     {
         if (cam == null)
         {
             Debug.LogError("PlayerShoot: No camera referenced!");
             this.enabled = false;
         }
     }
 
     void Update()
     {
         if (Input.GetButtonDown("Fire1"))
         {
             Shoot();
         }
     }
      void Shoot()
     {
         RaycastHit _hit;
         if (Physics.Raycast(cam.transform.position, cam.transform.forward, out _hit, weapon.range, mask))
         {
             //We hit something
             if (_hit.collider.tag == "ENEMY_TAG")
             {
                 Debug.Log("Hit for " + weapon.damage);
 
                 EnemyHealth enemyHealth = _hit.transform.gameObject.GetComponent<EnemyHealth>();
                 enemyHealth.currentHealth -= weapon.damage;
 
             }
 
         }
     }
 
 
 }

The PlayerWeapon script which gets the range and damage:

 using UnityEngine;
 
 [System.Serializable]
 public class PlayerWeapon
 {
 
     public int damage = 10;
     public float range = 100f;
  
 
 }
 

And finally the EnemyHealth script which sets how much health the enemy has:

 using UnityEngine;
 
 public class EnemyHealth : MonoBehaviour
 {
 
     public int maxHealth = 100;
     public int currentHealth;
 
     // Start is called before the first frame update
     void Start()
     {
         currentHealth = maxHealth;
 
     }
 
     void Update()
     {
         if(this.currentHealth <= 0)
         {
             Destroy(this.gameObject);
         }
     }
 
 }
 






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

  • Sort: 
avatar image
1

Answer by sheepmccree · May 21, 2020 at 05:30 AM

You could use the LineRenderer component. It's fairly easy to use and could do what you want. Set the first position to the source, and the second position to the end of the raycast.

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 Symbie · May 21, 2020 at 11:56 PM 0
Share

@sheepmccree i looked into this but all of the videos and threads i looked at i couldn't get to fit in with my script. how should i do this?

avatar image
0

Answer by sheepmccree · May 22, 2020 at 05:30 AM

I'll write this in a separate comment, so you can see it better.

The way I would do this (of course, there could be other, better solutions, but this is what I came up with) is the following: First, create an empty Gameobject with a LineRenderer component attached to it, and make it a prefab.

Settings I used Then, assign a script to it that would take care of setting the positions, and fading / dying after a time, and whatever else you would want to do. This is the script I tested with:

 public class Trail : MonoBehaviour
 {
     public Vector3 dest;
     
     
     // Start is called before the first frame update
     private void Start()
     {
         LineRenderer lr = GetComponent<LineRenderer>();
         
         lr.SetPosition(0, transform.position);
         lr.SetPosition(1, dest);
 
         StartCoroutine(Die());
     }
 
 
     private IEnumerator Die()
     {
         yield return new WaitForSeconds(2);
         Destroy(gameObject);
     }
 }


Then, in your PlayerShoot script, in the Shoot method, you would instantiate it and set its destination:

 Trail trail = Instantiate(bTrail, cam.transform.position, Quaternion.identity, transform).GetComponent();
 trail.dest = _hit.point;

If the raycast successfully hit something, and

 Trail trail = Instantiate(bTrail, cam.transform.position, Quaternion.identity, transform).GetComponent<Trail>();
  trail.dest = cam.transform.forward * weapon.range;

If it didn't hit. bTrail would be the bullet trailing prefab I talked about in the beginninng. I hope this will suffice as a quick starter for you! If there's something else, I'll be happy to help.

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

Follow this Question

Answers Answers and Comments

178 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 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

LineRender in Raycast 1 Answer

How can I can I cast a ray from a gameobject? 1 Answer

Raycast bullet collision problem 1 Answer

instantiate decal at raycastHit 1 Answer

Raycast not hitting instantiated object when timescale is 0 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