- Home /
Changing position of a RayCast
Got a simple Raycast with this script:
var hit: RaycastHit;
var rayRange = 10;
var rayDirection = transform.TransformDirection(Vector3.forward);
if(Physics.Raycast(transform.position, rayDirection, hit, rayRange))
{
//So on, so on...
}
Need to somehow change the starting origin of the Raycast... For example, at the moment it is casting from the bottom of my character, I just want to lift it up so its casting from around about the center of my character. Sure its a SUPER SIMPLE fix, just not to sure and quite new to Unity. Thank you in advance!
Answer by whydoidoit · Jul 11, 2012 at 09:42 AM
Obviously it depends on how tall your character is and which way up it is facing - presuming it is always standing on the ground then you can add to the transform.position:
if(Physics.Raycast(transform.position + Vector3.up * 0.75,
That would place the start of the cast 0.75 units upwards
You could eventually use the local up vector, so that this takes the player rotation:
if(Physics.Raycast(transform.position + transform.up * 0.75,
No problem - actually on reflection you can also use transform.up ins$$anonymous$$d Vector3.up as well (this takes into consideration the rotation of your character)
Your answer
Follow this Question
Related Questions
Move player with mouse help 0 Answers
camera.ScreenPointToRay always has same origin... 1 Answer
Ray.origin point moving from stationary object? 1 Answer
How to set position of raycast higher? 1 Answer
Performance issue Raycast each frame 1 Answer