- Home /
Raycast Rotate Help
i try to rewrite this code from sample assets because i want to raycast from the right and left side but the raycast doesn't rotate with the player
// Ground Check:
// Create a ray that points down from the centre of the character.
Ray ray = new Ray(transform.position, -transform.up);
Ray right_ray = new Ray(new Vector3(transform.localPosition.x+0.5f,transform.localPosition.y,transform.localPosition.z), -transform.up);
Ray left_ray = new Ray(new Vector3(transform.localPosition.x-0.5f,transform.localPosition.y,transform.localPosition.z), -transform.up);
// Raycast slightly further than the capsule (as determined by jumpRayLength)
RaycastHit[] hits = Physics.RaycastAll(ray, capsule.height * jumpRayLength );
RaycastHit[] right_hits = Physics.RaycastAll(right_ray, capsule.height * jumpRayLength );
RaycastHit[] left_hits = Physics.RaycastAll(left_ray, capsule.height * jumpRayLength );
float nearest = Mathf.Infinity;
if (grounded || rigidbody.velocity.y < 0.1f)
{
// Default value if nothing is detected:
grounded = false;
// Check every collider hit by the ray
for (int i = 0; i < hits.Length; i++)
{
// Check it's not a trigger
if (!hits[i].collider.isTrigger && hits[i].distance < nearest)
{
// The character is grounded, and we store the ground angle (calculated from the normal)
grounded = true;
nearest = hits[i].distance;
//Debug.DrawRay(transform.position, groundAngle * transform.forward, Color.green);
}
}
//Check every collider hit by the right
for (int i = 0; i < right_hits.Length; i++)
{
// Check it's not a trigger
if (!right_hits[i].collider.isTrigger && right_hits[i].distance < nearest)
{
grounded = true;
nearest = right_hits[i].distance;
}
}
//Check every collider hit by the left
for (int i = 0; i < left_hits.Length; i++)
{
// Check it's not a trigger
if (!left_hits[i].collider.isTrigger && left_hits[i].distance < nearest)
{
grounded = true;
nearest = left_hits[i].distance;
}
}
}
i've wrote x axis but it doesn't turn
Answer by robertbu · Mar 09, 2014 at 03:07 PM
I don't believe you want to use transform.localPosition as part of this calculation. Raycasting is a world positing thing, so I suggest you convert your point from local to world space using Transform.TransformPoint(). Something like:
Ray right_ray = new Ray(transform.TransformPoint(new Vector3(0.5f,0,0), -transform.up);
Ray left_ray = new Ray(transform.TransformPoint(new Vector3(-0.5f,0,0), -transform.up);
Also note that your use of '-transform.up' here will cast the ray out the bottom of the cube no matter what the rotation. If you are using this for ground sensing, consider changing that to Vector3.down.
error CS0119: Expression denotes a type', where a
variable', value' or
method group' was expected
Edited the answer to fix the problems. As I move back and forth between C# and Javascript, I sometimes forget the 'new' and the 'f' in C#.
Your answer
![](https://koobas.hobune.stream/wayback/20220613135807im_/https://answers.unity.com/themes/thub/images/avi.jpg)