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 F1refly · Mar 03, 2013 at 07:19 PM · rotationlinerenderer

Line Renderer Rotation Issue

I've been pulling my hair out over this all day and I've finally given up and decided to come looking for help.

So anyway, the problem at hand: I have a line renderer that I am using as a laser (GameObject with Transform, Line Renderer and Script attached); it's essentially a copy of the one from AngryBots.

However, I am applying a rotation to its transform in order to make the laser point in different directions (along the transform's z-axis). This works fine when instantiated from the weapon but when I try to instantiate it from another projectile, it doesn't work.

For some reason the line renderer refuses to draw the laser correctly (even though the spawn script is the same), the transform rotates as expected and the z-axis has the right facing but the laser does not point along it.

This is how I am spawning the laser:

 function SpawnProjectile() {
     for (var i = 0; i < numberOfProjectiles; i++) {
         var clone : GameObject = Instantiate(projectile, projectileSpawn.position, projectileSpawn.rotation);
         
         if (projectile.tag == "Laser" || projectile.tag == "Conical") {
             clone.transform.parent = transform;
         }
         
         if (releaseAfterTime && releaseRate > 0) {
             yield WaitForSeconds(releaseRate);
         }
     }
 }

This function is in both the weapon and the projectile scripts that I am instantiating the laser from.

I am then applying the rotation in the script attached to the laser object, like so:

 function Start () {
     var randomShotVariance = Quaternion.Euler(Random.Range(minXVariance, maxXVariance), Random.Range(minYVariance, maxYVariance), 0);
     transform.rotation *= randomShotVariance;
 }

As mentioned, this seems to work perfectly fine when the laser object is instantiated from the weapon, but has the aforementioned problem when spawned from the projectile and I cannot figure out why.

Just for context, this is how the laser is being drawn:

 function Update () {
     NormalRaycast();
         
     if (hitInfo.transform) {
         lRenderer.SetPosition(1, (hitInfo.distance * Vector3.forward));
     }
     else {
         lRenderer.SetPosition(1, (maxDistance * Vector3.forward));        
     }
 }
 
 function NormalRaycast() {    
     Physics.Raycast(transform.position, transform.forward, hitInfo, maxDistance);
 }

Element 0 of the line renderer is naturally the origin point of the laser and Element 1 can be seen above.

I must stress that this works perfectly fine when on the weapon, but not when on the projectile.

Thanks in advance!

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 robertbu · Mar 04, 2013 at 02:34 AM 0
Share

On a quick read, I suspect line 5 and 8 of the last script. You are using Vector3.forward ins$$anonymous$$d of transform.forward. And you you are calculating a position projected from the origin. I suspect the first should be:

  lRenderer.SetPosition(1, (hitInfo.point - transform.position));

and the second line should be:

  lRenderer.SetPosition(1, (transform.forward * maxDistance));   

 
avatar image F1refly · Mar 04, 2013 at 10:08 PM 0
Share

Yeah, I did think about that, but it doesn't seem to make any difference, presumably because the line renderer is set to use local axis anyway. After messing around with it some more today however, I found that if I change the mesh of the projectile that is spawning the laser/line renderer, it works fine. Do you happen to know why this is? It's leaving me rather confused.

avatar image robertbu · Mar 04, 2013 at 10:31 PM 0
Share

Ahhh, the world coordinate thing. Without "Use World Space" checked, all the coordinates are relative to the the position of the game object. So for your example above, you need to set the original position to Vector3.zero, rather than the origin point of the laser. And I've edited the code above for the second point.

Not sure why changing the mesh made thing work unless you changed the position to the origin (i.e. where relative and absolute coordinates are the same).

avatar image F1refly · Mar 05, 2013 at 03:07 PM 0
Share

While that didn't solve the problem, you did get me thinking about world space. Since the line renderer seemed to be having the issue with local space, I converted my code to world space (thanks to what you said). I don't know why it didn't like local space with certain meshes, but I guess that might be a problem with Unity itself. Anyway, thanks a lot for the help, I'll post what I did as the answer.

avatar image bknetwork · Nov 07, 2015 at 07:02 AM 0
Share

I'm having the same issue over here. :D

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by F1refly · Mar 05, 2013 at 03:28 PM

I converted it all to world space in the end, and it works with whatever mesh I am using - strange, but at least it works. Thanks to robertbu though for the assistance!

Here's what I changed it to:

 function Update () {
     lRenderer.SetPosition(0, transform.parent.position);    
 
     NormalRaycast();
         
     if (hitInfo.transform) {
         lRenderer.SetPosition(1, transform.parent.position + (hitInfo.distance * transform.forward));
     else {
         lRenderer.SetPosition(1, transform.parent.position + (transform.forward * maxDistance));        
     }
 }
Comment
Add comment · 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

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

Linerenderer view rotation in local space 1 Answer

Rotating a LineRenderer 0 Answers

Fixing LineRender 3d rotation 0 Answers

Setting rotation of player equal to rotation of a Line from Line Renderer,Get the rotation of Line Renderer and equal it to the rotation of the player 0 Answers

Line renderer rotation and alignment 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