playMaker

Author Topic: Another old problem - Raycast distance  (Read 632 times)

curb47

  • Sr. Member
  • ****
  • Posts: 256
Another old problem - Raycast distance
« on: January 20, 2021, 01:32:59 AM »
Hi again,

I'm looking at old issues that I've brushed under the carpet for a while, so now's the time to clean them up...

I'm using Raycast to fire lasers, and with the spaceship in the centre of the screen the Raycast distance needs to be shorter when the ship is facing Up and Down. I really don't know how to tackle this, I thought there must be some Math that could take the rotation value of the ship and a Raycast length could be extrapolated from that value. Or something like that. I tried using Colliders but that looked rubbish.



This is kinda what I'm looking for, with the green Oval representing the boundary of the Raycast -



Thanks for your help, you guys rock.

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7616
    • jinxtergames
Re: Another old problem - Raycast distance
« Reply #1 on: January 20, 2021, 09:06:44 AM »
Hi.
I'm just thinking this out of my head :)

You probably need to get the angle, then check the angle and you need 4 parts to check.

if 0-90 check for that part, if 90-180 check for that part same for 180-240 and 240 - 360.

then to calculate :

first you need to know the distance from left and up (you can set this in edit mode)
lets say left is 10 and top is 8.
this means there is a difference of 2.

lets say angle wound be 10. then do 90-10(angle), if angle would be 100 do 180 - 100(angle)

the result of this (80 in this sample) you need to divide by 90.
this will result in a value between 0 and 1 (angle result).

now do 2 * result(angle result) = 'add to base'.
This value might need to be inverted depending on what angle part.
to invert simply do 2- add to base = add to base inverted

now you can do 8 (shortest distance) + add to base (inverted)

Another way you could try is to set some colliders around the camera and use a 2nd raycast that will check for those colliders, get the distance an use that on the laser raycast.

curb47

  • Sr. Member
  • ****
  • Posts: 256
Re: Another old problem - Raycast distance
« Reply #2 on: January 20, 2021, 09:16:12 AM »
Hey djaydino,

Thanks for the input on this problem. I like the idea of the maths route, rather than the collider route. I'll delve deeper into your idea and see what I come up with.

Thanks again.

J.