Raycast Vector3 Coordinates

Hi,

I’ve got a Raycast set up from my character that creates a sound when it hits a collider in front of it:

function Update (){
var hit : RaycastHit;
if (Physics.Raycast (transform.position, transform.forward, hit, 5)){
if (hit.collider.gameObject.tag == "josh01"  characterHit == false) {
audio.PlayOneShot (josh01Sound);

What I want to do, is for the Raycast to do the same, but when the collider is at a 45 degree angle to the left of the character.

I’ve been looking at everything I can find about Raycasts and Vector3, but I’ve not been able to get my head round Vector 3.

I’m sure I’d be able to grasp it once I’ve seen an example I could understand.

Is there anyone who could please help by telling me how I’d change the above script (which currently collides with objects to the front) so that it includes Vector3 coordinates to collide with objects at a 45 degree angle to the left.

If so, I, and anyone else as confused as I am, would be extremely grateful.

Thanks.

hint :
replace transform.forward
with -transform.right
which means left.

OK. Got that. Thank you.

I now understand how to collide forwards, and at a 90 degree angle to the right and to the left.

But how do I do the same thing, but at a 45 degree angle?

According to the documentation, the 2nd paramter gets
a Vector3.
static function Raycast (origin : Vector3, direction : Vector3, distance : float = Mathf.Infinity, layerMask : int = kDefaultRaycastLayers) : bool

You can tweak it simply, instead of puting -transform.right u can put something like Vector3(0,-transform.right / 2 ,0)
or just -transform.right / 2

I’m not sure it’ll work because I never tried it, refer to it as a Psuedo code,

have a nice day.

I’ve changed the line to

so current code is

function Update (){
var hit : RaycastHit;
if (Physics.Raycast (transform.position,  Vector3(0,-transform.right /2 ,0) hit, 5 )){
  if (hit.collider.gameObject.tag == "josh01"  characterHit == false) {
   audio.PlayOneShot (josh01Sound);

But I’m now getting a code error of:

NullreferenceException; Object reference not set to an instance of an object

if (Physics.Raycast (transform.position, -transform.right/2, hit, 5 )){

how about this?
this will probably not produce the result u were looking for but it should compile (i hope ;))

It was already said in my reply, thanks anyways.

Thanks for that. That computes.

I should be able to play about with things from here.

Thanks a lot. Much obliged.

Glad to be of a help.

/ 2 won’t get you any different result to what you have now. The direction is still the same its magnitude is just different.

if you want 45 deg angles, use right / -right and add forward / -forward to it. that way it will be 45 degrees. Towards which direction depends on the combination of ± forward ± right, you can cover all 4 possibilities

I’ve tried all the following combinations. They either work at 90 degrees not 45 degrees, or have a code error.

I feel I’m close, I’ve also tried a few other alternatives to the below lines, but these are the ones I’ve saved and know are incorrect.

My head’s spinning!

I just need that extra spark of enlightenment but I’m not quite there yet.

if (Physics.Raycast (transform.position, -transform.right/2, hit, 5 )){
if (Physics.Raycast (transform.position, transform.right/-forward, hit, 5 )){
if (Physics.Raycast (transform.position, transform.right/ forward, hit, 5 )){
if (Physics.Raycast (transform.position, transform.-right/ forward, hit, 5 )){
if (Physics.Raycast (transform.position, transform.right, +forward, hit, 5 )){

What is it I’ve not grasped?

45 degrees to the Left of forward is (Vector3.forward - Vector3.right)

so: Physics.Raycast(transform.position, Vector3.forward - Vector3.right, hit, 5 ))

Gets you a single raycast at a 45 degree angle to the left.

Thank you very, very much.

That’s exactly what I needed.

Works a treat.

Thanks.

сan u explain the logic behind Vector3.forward - Vector3.right?

It’s Vector mathematics.

I’m going to do some ASCII Top down:

                   ^
Vector Forward:    |
                   |

Vector Right:    --->


Negative Vector Right:    <---


Vector Forward - Vector Right:
               
           <---^
               |
               |

OR

  X
    \
      \

Which from Top Down is 45 degrees to the left.
(the fact that it’s 45 degrees is because both the Adjacent and Opposite sides are of equal length).

I still don’t understand.
this :

Vector Forward - Vector Right:

<—^
|
|

Forward - Right is …
^—>
|
|

I still can’t understand the logic behind this.
Thanks anyway, if u can explain any further it’d be better.

No that’s Vector.Forward + Vector.Right

You are adding Right to Forward.

Remember: Value1 - Value2 = Value + (-Value2)

so Invert Vector.Right to subtract.

a Clearer explanation may help with values:
It’s 2D to assist with the diagram, with forward being up on a graph.

Forward = (0, 1)
Right = (1, 0)

Forward - Right = Forward + (-Right)
= (0 + (-1), 1 + 0)
= (-1, 1)

I can’t recall my trig very well off the top of my head, but I hope we can agree that a vector whose absolute X and absolute Y is the same is also 45 degrees or 135 or 225 or 315 (45 degrees off an axis).

Now I did understand that, I do agree it’s:

<–^
|
|
|

But, Why is that 45 degrees? it looks like 90 degrees to the left…

I also didnt understand what did u mean by saying absolute… Where’s absolute here?

absolute I mean absolute value or |x|.

It is also called Magnitude I believe. It just means all negative values are converted to positive.

i.e. 3,3 or -4,4 or -23, -23 are all vectors whose absolute X and absolute Y are the same.

And as far as it being 45 degrees you have to remember the starting point

 A<---
     ^
     |
     |
     B

goes from B to A and so the resultant Vector from forward - right is:


 A
  \
    \
     B

And if you perform the trig for:
    ^
X   |
 \  |
   \

You will find that angle to be 45 (the arrow being forward).