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 /
  • Help Room /
avatar image
0
Question by RichardM · Nov 10, 2016 at 05:40 PM · c#linerenderergameobjectstransforms

linerenderer start position not always correct

I have a spaceship with 2 guns and I alternate between them. Some times the "laser" lines are drawn perfectly others they come from any point around the gun end. I have been working on this for almost a week and just can't figure out why. Here is the relevant code:

 using UnityEngine;
 using System.Collections;
 
 public class RaycastShoot : MonoBehaviour {
     public int gunDamage = 1;
     public float fireRate = 0.12f;
     public float hitForce = 100f;
     public Transform gunEnd1;
     public Transform gunEnd2;
     public Camera fpsCam;
     private WaitForSeconds shotDuration = new WaitForSeconds(0.07f);
     private LineRenderer laserLine1;
     private LineRenderer laserLine2;
     private float nextFire;
     private int lastShot = 1;
 
     void Start () {
         laserLine1 = GetComponent<LineRenderer>();
         laserLine2 = GetComponent<LineRenderer>();
     }
 
     void Update () {
         if (Input.GetButton ("Fire1") && Time.time > nextFire) {
             nextFire = Time.time + fireRate;
             Vector3 rayOrigin = fpsCam.ViewportToWorldPoint (new Vector3 (0.5f, 0.5f, 0.0f));
             RaycastHit hit;
 
             if (lastShot == 1) {
                 laserLine1.SetPosition (0, gunEnd1.position);
             } else {
                 laserLine2.SetPosition (0, gunEnd2.position);
             }
             StartCoroutine (ShotEffect ());
 
             if (Physics.Raycast (rayOrigin, fpsCam.transform.forward, out hit, 1000)) {
                 Debug.Log ("Checking for hit");
                 laserLine1.SetPosition (1, hit.point);
                 Enemy health = hit.collider.GetComponent<Enemy> ();
                 if (health != null) {
                     health.Damage (gunDamage);
                 }//end if health
             } else { //end of raycast
                 Debug.Log ("nothing to hit");
                 laserLine1.SetPosition (1, rayOrigin + (fpsCam.transform.forward * 1000));
             }//end of raycast else
         }//end of fire1
     }//end of update
 
     private IEnumerator ShotEffect()    {
         if (lastShot == 1) {
             laserLine1.enabled = true;
             lastShot = 2;
         } else {
             laserLine2.enabled = true;
             lastShot = 1;
         }
         yield return shotDuration;
         laserLine1.enabled = false;
         laserLine2.enabled = false;
     }//end of IEnumerator
 
 }

Most of the code comes from an Unity tutorial. I have removed all the dynamic GameObject (GO) assignments and placed the actual object on the variable in the inspector.

On the ship there is a GO called weapons. Under that there are 2 capsule GOs called Gun (1) and Gun (2) under each gun there is Transforms called gunEnd(#). alt text

The gunEnd transforms are attached to the script like so: alt text

What I don't understand is why the start point is moving while the end point remains correct. The raycast is also working correctly even if the lines are not.

Any help would be greatly appreciated.

Richard M.

weapons.jpg (6.1 kB)
weapons2.jpg (17.9 kB)
Comment
Add comment · Show 5
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 Glurth · Nov 10, 2016 at 05:48 PM 0
Share

have not reviewed the whole things, but I suspect this line will cause you problems:

 Vector3 rayOrigin = fpsCam.ViewportToWorldPoint (new Vector3 (0.5f, 0.5f, 0.0f));

the z coordinate in the vector parameter defines the distance from the camera. If you use distance 0, you will always get the camera's world position. Try using fpsCam.nearClipPlane ins$$anonymous$$d of 0.0f.

Or if it's actually the cemera position that you want: you can just use fpsCam.position

avatar image RichardM · Nov 10, 2016 at 08:27 PM 0
Share

@Glurth As far as I understand it the rayOrigin is just for the raycast to see if there is any object within 1000 unitys of the "middle of the screen"; not the linerenderer.

Or am I missing something?

Richard $$anonymous$$.

avatar image Glurth RichardM · Nov 10, 2016 at 09:28 PM 0
Share

For the raycast: I was just saying you could simplify it (since you are using only the center of the screen), and eli$$anonymous$$ate the ViewportToWorldPoint like this:

 if(Physics.Raycast (fpsCam.transform.position, fpsCam.transform.forward, out hit, 1000))

But note, this line indicates that rayOrigin DOES in fact affect the line-renderer:

 laserLine1.SetPosition (1, rayOrigin + (fpsCam.transform.forward * 1000));

Still though, not sure if this is the actual cause of your problem or not- just waned to throw it out there.

Edit: another thought- make sure the line renderer is specifying WORLD coordinates, not local.

avatar image hexagonius · Nov 10, 2016 at 11:18 PM 0
Share

Wanted to throw my opinion in too.
You seem to be using two different intervals, one for firing and one for the shot animation, your beams.
It's just that I think the coroutine get out of sync with your shooting probably toggling lastshot every too often keeping old positions while firing off again.
Just a side note, but it seems it's two lasers behaving the same. It might be more readable putting the laser code into another script and just keep the fire left, fire right, fire left... while they handle themselves how they look. cleaner code you know

avatar image ElijahShadbolt · Nov 11, 2016 at 02:07 AM 0
Share

On lines 18 and 19, I think you set both laserLines to the same LineRenderer Component. Shouldn't they be different components? $$anonymous$$aybe you should make the laserLines public and assign a different LineRenderer to each.

0 Replies

· Add your reply
  • Sort: 

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

261 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 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

Need help getting randomly moving particles to head to the nearest of 4 coordinates. 1 Answer

IndexOutOfRangeException: Index was outside the bounds of the array. (wrapper stelemref) System.Object.virt_stelemref_class(intptr,object) 1 Answer

Drawing lines folling user input using line renderer. 0 Answers

Referencing uninstantiated GameObject within a class? 0 Answers

How to swap any two gameobjects in a field area? 0 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