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 15, 2013 at 12:38 AM · raycastupdatelaser

RayCast, origin not updating fast enough

I made a laser system for the guns in my game, it works fine but when I move the character it seems the origin of the RayCast its not updating fast enough and its not moving with the gun but a bit slower, here its my code also I included some pictures of the problem (in the second picture I meant to write "Laser not moving with the gun"): alt text

I have been testing a number of solutions including adding the script directly to the gun so I narrowed it down to RayCast Origin.

Im really worried about this, if I cant make this be accurate the bullets are gonna be really inaccurate, also it looks very bad, laser sigths are suppose to be really reliable and accurate.

 using UnityEngine;
 using System.Collections;
 
 [AddComponentMenu("Rayco's scripts/Laser")]
 public class laser : MonoBehaviour {
     public GameObject weapon;
     public int LaserLength;
     public Vector3 RotationOffset;
     public Vector3 PositionOffset;
     public Material LaserMaterial;
     public Color LaserColor;
     public Color LaserEndColor;
     public float LaserStartWidth;
     public float LaserEndWidth;
     Ray ray;
     void Start () {
         
         LineRenderer lineRenderer = gameObject.AddComponent<LineRenderer>();
         transform.position = weapon.transform.position;
         transform.parent = weapon.transform;
         transform.localEulerAngles = RotationOffset;
         transform.localPosition = PositionOffset;
         //LineRenderer options
         lineRenderer.material = LaserMaterial;
         lineRenderer.SetColors(LaserColor,LaserEndColor);
         lineRenderer.SetVertexCount(2);
         lineRenderer.SetWidth(LaserStartWidth,LaserEndWidth);
         lineRenderer.useWorldSpace = true;
         
         
     }
     void Update() {
         //LineRenderer
         LineRenderer lineRenderer = GetComponent<LineRenderer>();
         //Raycast stuff
         ray.origin = transform.position;
         ray.direction = transform.forward;
         RaycastHit hit;
         lineRenderer.SetPosition(0,ray.origin);
         if (Physics.Raycast (ray, out hit, LaserLength)){
         //Debug.DrawLine draws a line on game view, I have it for testing pourposes, you may delete any reference to it
         Debug.DrawLine(ray.origin, hit.point, Color.red);
         lineRenderer.SetPosition(1,hit.point);    
         }
         else{
         Debug.DrawLine(ray.origin,ray.GetPoint(LaserLength), Color.red);
         lineRenderer.SetPosition (1,ray.GetPoint(LaserLength));
         }
     }
     
 }


lasersss.jpg (83.5 kB)
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 DaveA · Jan 15, 2013 at 12:53 AM 0
Share

Not that it might solve it, but you could move that GetComponent to Start so you don't have to call that every Update

avatar image raycosantana · Jan 15, 2013 at 01:06 AM 0
Share

You cant do that. If you do that the things in the update dont know what "lineRenderer" is. Also if you have AddComponent and GetComponent in the same function the script doesnt even compile.

avatar image DaveA · Jan 15, 2013 at 01:51 AM 0
Share

Are you saying that if you moved lineRenderer outside these functions (make it global to this script), that it won't work, that you can't remove the GetComponent in Update? That's odd.

avatar image raycosantana · Jan 15, 2013 at 03:39 AM 0
Share

Actually, I think You cant make it global, I tried and gave me error everytime, you can have "Public LineRenderer" and add a gameobject with LineRenderer there though.

2 Replies

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

Answer by Bunny83 · Jan 15, 2013 at 01:19 AM

Since a LineRenderer is a component attached to a gameobject, why not just using local space coordinates?

 LineRenderer m_LineRenderer;
 
 void Start ()
 {
     // ...
     m_LineRenderer = GetComponent<LineRenderer>();
     // ...
     m_LineRenderer.SetVertexCount(2);
     m_LineRenderer.useWorldSpace = false;  // use local space
     
     m_LineRenderer.SetPosition(0,Vector3.zero);
 }
 
 void Update()
 {
     // ...
     if (Physics.Raycast (ray, out hit, LaserLength))
     {
         m_LineRenderer.SetPosition(1,new Vector3(0,0,hit.distance));
     }
 }

hit.distance works because your LineRenderer objects forward direction points in the same direction as your ray.

As alternative you can use InverseTransformPoint like this:

     if (Physics.Raycast (ray, out hit, LaserLength))
     {
         var P = m_LineRenderer.transform.InverseTransformPoint(hit.point);
         m_LineRenderer.SetPosition(1, P);
     }
Comment
Add comment · Show 2 · 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 15, 2013 at 01:41 AM 0
Share

Ok the first solution fixed it, Thanks!

avatar image raycosantana · Jan 15, 2013 at 03:43 AM 0
Share

oops I found a different problem now. Now the laser doesnt stop on RayCast hit.

void Update() { //LineRenderer LineRenderer lineRenderer = GetComponent(); //Raycast stuff ray.direction = transform.forward; RaycastHit hit; if (Physics.Raycast (ray, out hit, LaserLength)){ lineRenderer.SetPosition (1,new Vector3 (0,0,hit.distance)); } else{ lineRenderer.SetPosition (1,new Vector3 (0,0,LaserLength)); } }

}

avatar image
1

Answer by Chronos-L · Jan 15, 2013 at 01:50 AM

I have the same problem before, it is with a 2D marker tracking a 3D scene object. I came out with 2 different solutions for my problem.

How about using LateUpdate() instead of Update() in your laser script? It is possible that the Update() from your laser script is run before the Update() of your movement script. Hence there is a slight inaccuracy everytime you move and yet, everything looks fine when you are not moving.

Another possible solution is to redraw the laser in your movement script:

 public class laser : MonoBehaviour {
    ...
 
    void Start() {
       ...
    }
 
    void Draw() {
       //LineRenderer
       LineRenderer lineRenderer = GetComponent<LineRenderer>();
       //Raycast stuff
       ray.origin = transform.position;
       ray.direction = transform.forward;
       ...
    }
 }

Then in your movement script:

 public class movement : MonoBehavior {
    public Laser laser;
    ...
    ...
    void movementfunction() {
       ...
       //Call the Draw() at the end of your movement
       laser.Draw();
    }
 }

By following any of these 2 method, the laser will always be drawn after the movement is calculated at each frame of your game. Hopes that this will help you.

Comment
Add comment · Show 2 · 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 15, 2013 at 01:58 AM 0
Share

I tried LateUpdate before posting here, didnt work. But anyways my problem its fixed, Bunny83's solution worked for me. Thanks for trying to help me.

avatar image shohan4556 · Nov 27, 2016 at 05:23 PM 0
Share

Thanks man LateUpdate is works for me

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

12 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

Related Questions

Get raycast direction from weapon 1 Answer

Creating a Ray Gun 1 Answer

3D Space Shooter Firing Projectiles 0 Answers

How should i add raycast to multiple game objects??? 0 Answers

Can someone explain to me why this raycast doesn't fire straight? 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