Changing Raycast origin upwards (offset)

Hello, I have an annoying problem and I seached everywhre but couldn’t find the solution.

I need t ochange the origin point of a raycast, I defined localOffset for it and I did exactly like all solutions that are supposed to work, but it is just not working:

All it does is change the direction of the ray, the origin however still stays at the origin of the game object (player character in this case, which is far too low).
So now the ray goes forward and up a bit.

I want to cast from about middle height (0.5).

var localOffset = transform.position + transform.up * 0.5;
var hitCollision : RaycastHit;
var CollisionCheckRange : int = 1.1;

if (Physics.Raycast (localOffset, transform.forward, hitCollision, CollisionCheckRange))

Indeed a great idea. I just make it a child of the player object. Now I just have to find out how to access the variables it puts out in my main movement script, a whole new can of worms opened so to speak :) But I have to learn it anways for my elevator script, so thanks for the suggestion! I will flag your answer as a solution once I have it all working correctly, it may take a few days but I have this question page bookmarked.

Wow, I cannot believe you acknowledged that you were told 30 minutes ago, and I just told you on your other question 5 minutes ago. You won't be posting another comment as an answer again, will you... no? good. * Watch : http://video.unity3d.com/video/7720450/tutorials-using-unity-answers * Read : http://answers.unity3d.com/page/newuser.html

This is also what I'm doing - I have guns on a ship and each has an Empty at the correct position that I use for starting various position-based checks at the right location. I made sure each faces "forward" in the direction I expect too in order to minimize the amount of walking up and down the object tree as needed. In short, Fattie is spot on.

to access variables, check out the tutorials on unityGEMS.com it's very easy and it is the core of unity programming!

1 Answer

1

Hello,

You should look up how Vectors work and the difference between global and local space. The origin of a Raycast is in global space, your localOffset is also in global space, therefore you have to convert your localOffset to global space, when you use it as global space. To do this, I suggest you use the Transform function called TransformPoint It transforms a point from local space to global space.
Therefore:

if (Physics.Raycast(transform.TransformPoint(localOffset), transform.forward/*... etc*/)) {

Hope this helps, Benproductions1

Thanks for the suggestion, unfortunately it does not help, it still just changes the direction of the ray but the origin still remains at the game object's origin (i.e. the bottom). Another solution which is infact like my code looks at the moment is presented here: http://answers.unity3d.com/questions/224511/how-can-i-offset-a-raycast-along-the-transforms-lo.html But that user had the same problem as me so he had to resort to a workaround.

@Cherno It does work, your just not using it right