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
0
Question by JoshuaHodkinson · Apr 02, 2014 at 09:33 AM · c#raycastlinerenderer

Setting Line Render position directly forward from facing of game object

So I have this script here that handles the firing of a weapon using raycasts and I'm trying to have it use a Line Renderer directly forward from the facing of the gun object. Initially I used hitInfo.point as the 2nd point for the line renderer but as there is no collision with the sky the line will not render there. After reading through other answers I determined that transform.TransformDirection(Vector3.forward) should project the line forward but when I run my game the line is always rendered to a fixed point despite which direction my player is facing ( and hence the gun's facing direction). If anyone could point out my mistake here, it would be much appreciated.

     public class RaycastMachineGun : MonoBehaviour {
     
     float mCooldown = -0.1f;
     float mFireRate = 0.1f;
     
     int mGunAmmo = 0;
     int mGunMaxAmmo = 500;
 
     int mGunDamage = 10;
     
     LineRenderer mGun;
     float mGunCountDown = 0.125f;
     float mCurrentCountDown = 0.0f;
     
     void Start () {
         mGun = GetComponent<LineRenderer>();
         mGun.enabled = false;
         mGun.SetVertexCount (2);
         mGun.SetWidth (0.01f, 0.01f);
         mGun.SetColors (Color.yellow, Color.yellow);
         mGunAmmo = mGunMaxAmmo;
     }
     
     void Update () {
         if(Input.GetButton("Fire1") && Time.time > mCooldown && mGunAmmo > 0)
         {
             mGunAmmo--;
             RaycastHit hitInfo;
             Vector3 direction = transform.TransformDirection(Vector3.forward);
 
             if (Physics.Raycast(transform.position, direction, out hitInfo))
             {
                 if(hitInfo.transform.tag != "Player")
                 {
                     Health enemyHealth = hitInfo.transform.GetComponent<Health>();
                     if (enemyHealth!= null)
                     {
                         enemyHealth.mDamage(mGunDamage);
                     }
                 }
                 mGun.enabled = true;
                 mGun.SetPosition(0, transform.position);
                 mGun.SetPosition(1, direction);
             }
             mCooldown = Time.time + mFireRate;
         }
         
         mCurrentCountDown += Time.deltaTime;
         if (mCurrentCountDown >= mGunCountDown)
         {
             mGun.enabled = false;
             mCurrentCountDown = 0.0f;
         }
     }
 
     void OnGUI(){
         GUI.Label (new Rect (5, Screen.height - 45, 100, 100), "Ammo: " + mGunAmmo.ToString () + " / " + mGunMaxAmmo.ToString ());
     }
 }

Also as this is my first question on here, I'm not entirely sure I formatted everything properly, if someone could also point me to where to do that, thanks in advance.

Comment
Add comment · Show 2
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 kestik · Apr 02, 2014 at 10:26 AM 0
Share

Have you tried 'transform.forward'? docs

avatar image JoshuaHodkinson · Apr 03, 2014 at 08:55 AM 0
Share

I believe that transform.forward would have the same effect as transform.TransformDirection(Vector3.forward)? I had already tried and it continued to not work unfortunately.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by JoshuaHodkinson · Apr 03, 2014 at 09:07 AM

I have since managed to fix my own mistake using the following code

 public class RaycastMachineGun : MonoBehaviour {
     
     float mCooldown = -0.1f;
     float mFireRate = 0.1f;
     
     int mGunAmmo = 0;
     int mGunMaxAmmo = 500;
 
     float mGunDamage = 10.0f;
     
     LineRenderer mGun;
     float mGunCountDown = 0.125f;
     float mCurrentCountDown = 0.0f;
     
     void Start () {
         //Set values for line renderer as feedback
         mGun = GetComponent<LineRenderer>();
         mGun.enabled = false;
         mGun.SetVertexCount (2);
         mGun.SetWidth (0.01f, 0.01f);
         mGun.SetColors (Color.yellow, Color.yellow);
 
         //Intially set the amount of ammo to max
         mGunAmmo = mGunMaxAmmo;
     }
     
     void Update () {
         //Check if player fires
         if(Input.GetButton("Fire1") && Time.time > mCooldown && mGunAmmo > 0)
         {
             //Subtract from ammo
             mGunAmmo--;
 
             RaycastHit hitInfo;
             Vector3 direction = transform.TransformDirection(0,0,1);
 
             //Find the object that is hit
             if (Physics.Raycast(transform.position, direction, out hitInfo))
             {
                 if(hitInfo.transform.tag != "Player")
                 {
                     //Render the line as player feedback
                     mGun.enabled = true;
                     mGun.SetPosition(0, transform.position);
                     mGun.SetPosition(1, hitInfo.point);
 
                     Health enemyHealth = hitInfo.transform.GetComponent<Health>();
                     if (enemyHealth!= null)
                     {
                         //Apply damage to enemies
                         enemyHealth.mDamage(mGunDamage);
 
                         //Create Particles if hit enemy
                     }
                     else
                     {
                         //Create Particles if not hit enemy
                     }
                 }
             }
             else 
             {
                 //Render the line as feedback
                 mGun.enabled = true;
                 mGun.SetPosition(0,transform.position);
                 mGun.SetPosition(1, transform.TransformDirection(Vector3.forward)*1000);
             }
             //Apply firerate cooldown
             mCooldown = Time.time + mFireRate;
         }
         //Turn off line renderer 
         mCurrentCountDown += Time.deltaTime;
         if (mCurrentCountDown >= mGunCountDown)
         {
             mGun.enabled = false;
             mCurrentCountDown = 0.0f;
         }
     }
 }
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

20 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

Related Questions

Multiple Cars not working 1 Answer

Bouncing Raycast "Laser" Not Working? 1 Answer

The referenced script on this Behavior ( Game Object 'Camera') is missing. What shall I do? 1 Answer

Distribute terrain in zones 3 Answers

Linerenderer doesn't match my ray. 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