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 raycosantana · Jan 13, 2013 at 01:28 AM · raycastlaserdebug.drawline

Get raycast direction from weapon

I want to use raycast to create a laser system (also for the bullets) for the weapons in my game, but right now Im just trying to draw a line to see whats going on in there. But how can I get the direction where the weapon is pointing?

 using UnityEngine;
 using System.Collections;
 
 [AddComponentMenu("Rayco's scripts/Laser")]
 public class laser : MonoBehaviour {
     public GameObject weapon;
     public Vector3 RotationOffset;
     public Vector3 PositionOffset;
     Vector3 rayo;
     Vector3 direccion;
     void Update() {
         direccion = weapon.transform.forward;
         rayo = weapon.transform.position;
         transform.parent = weapon.transform;
         Physics.Raycast(rayo, direccion, 10);
          Debug.DrawLine(rayo, direccion, Color.red);
     }
     void start() {
         transform.localEulerAngles = RotationOffset;
         transform.localPosition = PositionOffset;
     }
 }
Comment
Add comment · Show 4
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 Wolfram · Jan 13, 2013 at 01:48 AM 0
Share

I don't understand your question. If you modeled your weapon so that the muzzle points towards +Z, the code you provided should already cast the ray in the correct direction. So what exactly is it you need?

Also, what are you trying to accomplish with "transform.parent = weapon.transform;"? This should be called once, not in Update().

One problem I see with your code is that reparenting does not modify the world coordinates of your object, ins$$anonymous$$d it modifies the local transformations accordingly, which will effectively override the settings you are trying to do in Start().

Another bug in your script is case sensitivity. "start()" will never be called. The function needs to be called "Start()".

avatar image raycosantana · Jan 13, 2013 at 10:45 AM 0
Share

Well it does not, it cast from the weapon to the ground (always to the same spot). I think the question its pretty obvious.

avatar image Wolfram · Jan 13, 2013 at 03:12 PM 0
Share

Did you check any of the other things I mentioned? All of the remarks were important. $$anonymous$$ost importantly, weapon.transform.forward will point along +Z of your "weapon" node. At the top of your editor there are two buttons, one says "Pivot" or "Center", the other "Global" or "Local". Set them to "Pivot" and "Local". Now select the object you assigned to "weapon". Does the muzzle now point along the blue arrow?

avatar image raycosantana · Jan 13, 2013 at 05:13 PM 0
Share

It was already in Pivot and Local and it does not point to the blue arrow, but I already knew that, thats why I added offset fields so I can adjust it later. Anyways I changed all the code. Now the line moves with the character but its waaay off the weapon. I added the code to a new answer, as soon as it gets reviewed you can check it out. Thanks for your time.

1 Reply

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

Answer by robertbu · Jan 13, 2013 at 05:04 AM

As Wolfram mentioned, there are a number of issues with your code. So below is a bit of starter code. Attache the script to your gun (or a child object of your gun), drag a light onto the public lightLazer variable, and the script will place a light on objects the Raycast hits in the scene. I setup my light with a range of 0.1, intensity of 1.5 and the color red. This code cast the ray from the origin of the object it is attached to. You will need to translate the ray up to the gun sight level (or perhaps easier, attach the script to an empty game object with its origin at the level of the sight). Also if your gun has a collider, you will need to either 1) remove it, 2) use a more complex raycast, or 3) move the origin of the raycast outside the gun. Good luck with your project.

 using UnityEngine;
 using System.Collections;
 
 public class Lazer : MonoBehaviour {
     
 public Light lightLazer;
 private Ray ray;
 
     void Start ()
     {
         lightLazer.enabled = false;
     }
     
     void Update ()
     {
         RaycastHit hit;
         ray.direction = transform.forward;
         ray.origin = transform.position;
         if (Physics.Raycast (ray, out hit))
         {
             Vector3 v3Pos = ray.GetPoint (hit.distance * 0.995f);
             lightLazer.enabled = true;
             lightLazer.gameObject.transform.position = v3Pos;
         }
         else
         {
             lightLazer.enabled = false;
         }
     }
 }
Comment
Add comment · Show 6 · 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 raycosantana · Jan 13, 2013 at 11:17 AM 1
Share

Thanks for your code but I rather fix my code, If I use your code Im not learning anything.

avatar image robertbu · Jan 13, 2013 at 03:21 PM 0
Share

I too try and not use code I do not understand. But I'm forever dropping in sample code and modifying it with it as a way to understand an approach if not the specifics for a problem. And comparing my code to a working sample has allowed to solve any number of problems.

avatar image raycosantana · Jan 13, 2013 at 05:04 PM 0
Share

Ok lets try again:

 using UnityEngine;
 using System.Collections;
 
 [AddComponent$$anonymous$$enu("Rayco's scripts/Laser")]
 public class laser : $$anonymous$$onoBehaviour {
     public GameObject weapon;
     public Vector3 RotationOffset;
     public Vector3 PositionOffset;
     Vector3 rayo;
     private Ray ray;
     void Start () {
         
         rayo = weapon.transform.position;
         transform.parent = weapon.transform;
         transform.localEulerAngles = RotationOffset;
         transform.localPosition = PositionOffset;
         
     }
     void Update() {
         ray.origin = rayo;
         ray.direction = transform.forward;
         RaycastHit hit;
         Physics.Raycast(ray, out hit, 100);
          Debug.DrawLine(rayo, hit.point, Color.red);
     }
     
 }

This seems to work but the line its waaaay off the weapon (its draw under the map and pointing in a different direction). Changing the offset values doesnt seem to do anything.

avatar image Lovrenc raycosantana · Jan 13, 2013 at 05:05 PM 1
Share

Use Comment or even EDIT in this case. Questions and their elaborations are to be in guestion post.

Answers are, well for answers.

avatar image Wolfram raycosantana · Jan 13, 2013 at 05:28 PM 0
Share

Well, if it's pointing in the wrong direction, you need to adjust your RotationOffset - but in which way is impossible to tell without knowing your scene setup, and in which world space direction your muzzle is actually pointing.

Concerning the position, note that you initialize ray.origin with weapon.transform.position, which is the world space position of your weapon's pivot. You are currently not applying PositionOffset to that position. for starters, try ray.origin = transform.position; ins$$anonymous$$d, and also replace rayo in the Debug-command with ray.origin.

avatar image raycosantana raycosantana · Jan 13, 2013 at 05:46 PM 0
Share

Ok chaged ray.origin now it works! Thanks U!

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

11 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

Related Questions

RayCast, origin not updating fast enough 2 Answers

Raycast Will Only Hit Target If Jumping Over Object Firing Raycast 1 Answer

Lasersight flickering 0 Answers

Can't get a laser working properly. 2 Answers

Is there a better way to find where to start my raycast? 2 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