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 /
avatar image
0
Question by Genzuo · Dec 08, 2019 at 11:22 AM · scripting problemraycastraycasting

Ray origin moving with player movement and mouse movement

I'm using a ray cast for a gun's laser sight with a line renderer for the visuals and I have this problem where the ray's origin changes depending on the player's movement and mouse movement. I had the same problem earlier in my project, but sometimes unity seems to act strange for seemingly no reason which fixes with just a restart of unity. Anyway, this time I can't seem to get it to return to how it was. Here is the code for updating the ray.

 private void Update () {
     Ray laserRay = new Ray(gunBarrel.position, gunBarrel.forward);
     RaycastHit hitInfo;
         
     if (laser == null) {
         createLine();
     }
             
     laser.SetPosition(0, laserRay.origin + new Vector3(0, laserOffset, 0));
     if (Physics.Raycast(laserRay, out hitInfo, laserDistance) && !hitInfo.collider.gameObject.CompareTag("Projectiles")) {
     laser.SetPosition(1, hitInfo.point);
     } else {
         laser.SetPosition(1, laserRay.origin + laserRay.direction * laserDistance);
     }
 }

I just can't figure out what's causing this because there really isn't that much to the code. Also I'm pretty new to this so sorry if I missed something obvious.

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

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

Answer by lgarczyn · Dec 09, 2019 at 02:20 AM

As @Fariborzzn pointed out, you are not using the same origin for the raycast and for the LineRenderer

Apart from that, using LateUpdate or the script order window could help you, as your character controller or animator might update your position after your code has set the positions/rotation of the camera or player.

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 Genzuo · Dec 09, 2019 at 10:54 AM 0
Share

Late update seemed to fix it for me. The line does flicker every now and then but it's a much better problem than what I had, thanks. Also yeah, the ray is just co$$anonymous$$g from the barrel so I could use it for both the laser sight and the actual bullets, but I ended up just going with hitscan as I couldn't seem to get the bullets to work properly. Anyway, thank you again.

avatar image
1

Answer by Fariborzzn · Dec 08, 2019 at 05:08 PM

Hey Genzuo i look in to your code.. you are casting a ray every frame in your update function

Ray laserRay = new Ray(gunBarrel.position, gunBarrel.forward);

if your orgin is not static its because of gunBarrel.position is dynamic (gunbarrel position is changing over time and it should be! because player need to walk and shoot in diffrent direction base on input ) if your problem is something else it would be great if you post a picture of it i will be happy if i can help

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 Genzuo · Dec 09, 2019 at 11:01 AM 0
Share

Sorry I thought this would have happened, it's a little hard to explain the problem. Basically if I moved left the laser origin would move off to the right, 'detaching' from the gun. Then when I moved my mouse the laser origin would lag behind where it was meant to be. As @ceandros suggested though, late update fixed it.

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

247 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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 Ray cast for click to move 2 Answers

script problem even though i have no errors 2 Answers

scripting problem 0 Answers

Can you figure out raycast origin position from RacyastHit? 2 Answers

I need help with dragging objects 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