- Home /
Raycast wrong direction - really weird
So there are three types of objects in my game that emit a continuous laser beam:
main ship,
support ship
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.
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.
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?
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.
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
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.
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
Follow this Question
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