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 sigma-z-1980 · Oct 11, 2018 at 10:10 PM · raycastlinerendererlaser beam

Raycast wrong direction - really weird

So there are three types of objects in my game that emit a continuous laser beam:

  1. main ship,

  2. support ship

  3. planet cannon

Each gameobject instantiates a separate GameObject with a Laser script attached (same script) that implements RayCast and LineRenderer.

In Image1 there's only planet cannon. As you see, the generated raycast goes in the wrong direction. I don't know why this happens, but setting the parent of the laser didn't help. alt text

In Image2 I added both ship types, and when the main ship gets sufficiently close to the player, for some reason the planet cannon's raycast is correct.

I've been tryinf to fix it for many hours now, and nothing comes out. I guess this is something to do with the direction of the raycast (transform.forward), but I don't understand why it changes when the ship approach.

alt text

EDIT: so each gameobject (both ship types, planet cannons) has a script attached to it that instantiates a laserstream once the player is sufficiently close. It also sets each cannon as the parent of the stream and its owner:

         sbeam = Instantiate(satellitelbeam, satellites[nearest].transform.GetChild(0).transform.position, satellites[nearest].transform.GetChild(0).transform.rotation);
         sbeam.transform.SetParent(satellites[nearest].transform);
         sbeam.GetComponentInChildren<csLaser>().owner.Add(satellites[nearest].tag);


In the Laser.cs code attached to the Laser gameobject the update checks Raycast using

 if (Physics.Raycast(this.transform.position, this.transform.forward, out hit, MaxLength))

and the result is shown in Image 1: the LineRenderer is in the direction opposite to the player. The position for the LineRenderer is calculated in the following way:

     NewPos = this.transform.parent.transform.position + new Vector3(transform.forward.x * (NowLength - 1), transform.forward.y * (NowLength - 1), transform.forward.z * (NowLength - 1)); //Set LineRenderer Length throught NowLength's information.  

 _LineRenderer.SetPosition(0, shipowner.position); //Set this GameObject's Position
 _LineRenderer.SetPosition(1, NewPos); //Set Next SetPosition use NewPos information

In the last piece of code shipowner is defined as

 shipowner = transform.parent.parent;

which points to the cannon.

So I can't make heads and tails - why is the raycast looking the opposite direction? Why does it look in the right direction when the ship approaches?

raycast2.png (419.7 kB)
raycast1.png (396.1 kB)
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
1

Answer by FlightOfOne · Oct 12, 2018 at 01:28 AM

Some code would help along with the images, its hard to tell what's going on with out any code. I am assuming your raycast is not detecting the ship when it's for and raycastHit.point points to vecto3.zero and when the ship is near its getting a hit point.

Comment
Add comment · Show 4 · 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 sigma-z-1980 · Oct 12, 2018 at 10:09 AM 0
Share

I'll upload the code when I'm back home.

The raycast points in the opposite direction from the player when the ship is not around. When the ship is near it is correct. The direction I use is transform.forward.

As I understand, transform relates to the gameobject to which the script is attached.

I really can't make heads and tails from this

avatar image sigma-z-1980 · Oct 13, 2018 at 09:07 PM 0
Share

pls see the update

avatar image FlightOfOne sigma-z-1980 · Oct 14, 2018 at 02:30 PM 0
Share

So when there's no ship approaching what would you like the laser to do? Hide itself or point at a default direction?

  if (Physics.Raycast(this.transform.position, this.transform.forward, out hit, $$anonymous$$axLength))
     

Looking at above if there's no ship near by, meaning raycast doesn't detect a hit, "hit.point" will default to vector3.zero.

What you can do is:

   if(HitShip)
     {
     show laser
     }
     else
     {
     either hide the laser
     or it to point to a default position
     e.g. default=laser.transform.position + laser.transform.forward * 10;
     }
 

This should is pretty straight forward, unless there's something else going on. Also be sure that the linerenderer is set to world space.

avatar image sigma-z-1980 FlightOfOne · Oct 25, 2018 at 11:37 AM 0
Share

I'm sorry I didn't do a good job at explaining: there are two 'ship' in play here: player ship and enemy ship. Of course the laser from the planet cannon should be directed at the player ship when it is near. But ins$$anonymous$$d the laserbeam is pointing in the opposite direction. Then, an enemy ship approaches, and when it is near, the laser from the planet cannon changes the direction and points towards the player. This is so confusing I'm still unable to sort this out.

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

132 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

Related Questions

Line Renderer & Raycasting SOS !!! 1 Answer

RayCast Line Not Rotating with Gun? 0 Answers

Simple wire/cable 1 Answer

Reflecting linerenderer with raycast? 2 Answers

Bouncing Laser Beam 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