Offsetting a direction?

by olawrdhalpme   Last Updated September 10, 2019 15:13 PM - source

I'm trying to figure out how I can best offset a direction when raycasting.

So lets say I have an object and I want to cast a ray from my mouse position to said object, that's easily done with (for example):

// "obj" being the transform of the object I'm raycasting to.
Vector2 mPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector2 dir = (Vector2)obj.position - mPosition;
Debug.DrawRay(mPosition, dir,;

But what if I want the direction to always be slightly offset to the right of the object from the mouse position? And I mean not just to the right of the object itself, but to the right from where the mouse is.

I hope I'm making any sense. Thanks in advance!

Edit: Adding some pictures to try and make it a little more clear: (Pretend the line is the ray and the circle is the object)

I want to offset the ray so that when my mouse is under the circle, it goes to the right of it, like this: enter image description here

However, when I move my mouse to the left of the object, the ray should instead go like this: enter image description here

Meaning the ray will always cast to the right of the object from where the mouse is.

The reason I'm doing this is because I'm trying to raycast to the corners of objects, but also see what's past them: enter image description here

I need to use offset rays to just barely miss the corners and allow them to continue on and see if there's a wall behind them like on the picture above. I'm offsetting my rays in the picture above, but the way I'm doing it isn't the best and I'm trying to find the proper way of doing it.

Answers 1

You can rotate it around the Y axis a bit. E.g. 10 degrees:

direction = Quaternion.Euler(0, 10, 0) * direction;
September 10, 2019 14:24 PM

Related Questions

Raycast Masks not working Unity3D

Updated April 07, 2015 18:05 PM

Raycasting on Z in 2D

Updated April 28, 2015 21:05 PM