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 Deathleaper · Mar 22, 2017 at 10:12 AM · linerenderergunfire

Line renderer not working

So I was trying to make a very simple bullet which literally just casts a line from the tip of the gun forward the same amount as a raycast. I'm thinking my issue may have something to do with the fact that the accessed line renderer isn't on the same object as the script but I did try this before and it seemed to work. I would put the line renderer on the same object but it's already in use by another script on the object. I'll put the code I think is relevant here:

RaycastHit hit; Ray isGrounded = new ray (transform.position, cam.forward); if (Physics.RayCast (isGrounded, out hit, reach)){ line.SetPosition(1, pistol.transform.position); line.SetPosition(2, hit.point); }}

I understand the line should only be drawn when there is a raycast hit, but I have also set up the print function so I know when it should be called. I'm really lost with this ass it worked fine with practically identical code on the other script so I figure it must be something to do with the line renderer not actually being attached to the player's object. 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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by TheAnimatedKiddo · Sep 30, 2020 at 10:06 AM

Hey @Deathleaper! Try this code and when the shoot function, call the line renderer. I modified this code from Brackyes, just so you know.

using UnityEngine; using System.Collections; using UnityEngine.UI; using JetBrains.Annotations; public class OSGun : MonoBehaviour { public float damage = 10f; public float range = 100f; public int maxAmmo = 10; private int currentAmmo; public float reloadTime = 1f; private bool isReloading = false; public Camera fpsCam; public ParticleSystem muzzleFlash; public Transform shotText; public Animator animator; public AudioSource audioData; void Start() { currentAmmo = maxAmmo; audioData = GetComponent<AudioSource>(); } void OnEnable() { isReloading = false; animator.SetBool("Reloading", false); animator.SetBool("Shooting", false); shotText.GetComponent<Text>().text = currentAmmo + "/" + maxAmmo.ToString(); } void Update() { if (isReloading) { return; } if (currentAmmo <= 0) { StartCoroutine(Reload()); return; } if (Input.GetButtonDown("Fire1")) { animator.SetBool("Shooting", true); Shoot(); shotText.GetComponent<Text>().text = currentAmmo + "/" + maxAmmo.ToString(); } if (Input.GetButtonUp("Fire1")) { animator.SetBool("Shooting", false); } if (Input.GetKey("r")) { if (currentAmmo == maxAmmo) { return; } else { StartCoroutine(Reload()); return; } } } IEnumerator Reload () { isReloading = true; Debug.Log("Reload..."); animator.SetBool("Reloading", true); yield return new WaitForSeconds(reloadTime - .25f); animator.SetBool("Reloading", false); yield return new WaitForSeconds(.25f); currentAmmo = maxAmmo; shotText.GetComponent<Text>().text = maxAmmo + "/" + maxAmmo.ToString(); isReloading = false; } void Shoot() { muzzleFlash.Play(); currentAmmo--; audioData.Play(0); RaycastHit hit; if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range)) { Debug.Log(hit.transform.name); Target target = hit.transform.GetComponent<Target>(); if (target != null) { target.TakeDamage(damage); } } else if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range)) { Debug.Log(hit.transform.name); GlassTarget target = hit.transform.GetComponent<GlassTarget>(); if (target != null) { target.TakeDamage(damage); } } } public void SetAmmoOnEquip() { shotText.GetComponent<Text>().text = currentAmmo + "/" + maxAmmo.ToString(); } public void SetAmmoOnUnequip() { shotText.GetComponent<Text>().text = "--"; } }

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

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

Using LineRenderer, how can I draw a single path passing through more than two points? 1 Answer

How do you calculate the points for a predictive trajectory line? 0 Answers

Drawn line twists and becomes invisible 0 Answers

Line Renderer 2d Follow the edge 0 Answers

Line Renderer Object to Object and Lock On Object 0 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