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 Alienguard · Dec 19, 2013 at 12:32 AM · raycastpositionraylaser beam

Why Does the Ray change my object position?

Hello. I am following the laser beam tutorial in the videos of the Learn section.

However, when I try to implement the lasers something strange happens, when I shoot my lasers the gun tip changes its position, the laser appears in the new position and slowly moves away. I tried to keep in the position I am setting it to, but it keeps changing its position and then moving away.

Here is the code I am using. I will be thankful for any kind of help.

 using UnityEngine;
 using System.Collections;
 
 public class OverWeapon : MonoBehaviour
 {
     LineRenderer line;
     //public Transform rifleGun;
     void Start()
     {
 
         line = gameObject.GetComponent<LineRenderer>();
         line.enabled = false;
     }
 
     void Update()
     {
         Vector3 forward = transform.TransformDirection(Vector3.forward) * 10;
         Debug.DrawRay(transform.position, -forward, Color.green);
         if(Input.GetKeyDown(KeyCode.Space))
         {
             StopCoroutine("shootBeam");
             StartCoroutine("shootBeam");
         }
     }
 
     IEnumerator shootBeam()
     {
         line.enabled = true;
 
         while(Input.GetKey(KeyCode.Space))
         {
             //Ray ray = new Ray(transform.position, transform.forward);//I HAVE DISCOVERED THAT THIS IS SOMEHOW CAUSING THE PROBLEM
             line.SetPosition(0,ray.origin);
             line.SetPosition(1,ray.GetPoint(100));
             yield return null;
         }
 
         line.enabled = false;
     }
 
 }
 
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 Kiloblargh · Dec 19, 2013 at 12:52 AM

I'm not sure if this is the problem, but you shouldn't use a while loop that way.

And your code has a lot of redundancy in it. Try this:

 Update() {
 line.enabled = Input.GetKey (KeyCode.Space) );
 if (line.enabled) {
 line.SetPosition (0, transform.position);
 line.SetPosition (1, transform.TransformPoint (Vector3(0,0,100)));
 }
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 Alienguard · Dec 19, 2013 at 01:10 AM 0
Share

Alright I will give it a try. However, I am trying to use a ray as I draw my line because I want my laser to eventually hit something and destroy it. Now, the reason I used that while loop is that I was trying to practice on coroutines. Please do excuse my horrible coding and thanks for your help. :)

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

18 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

Related Questions

How to set position of raycast higher? 1 Answer

Performance issue Raycast each frame 1 Answer

Changing position of a RayCast 1 Answer

Using Trigonometry for Collision Detection 0 Answers

how to tell what platform you're on. 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