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_.collider.isTrigger && hits*.distance < nearest)*_

* {*
* // The character is grounded, and we store the ground angle (calculated from the normal)*
* grounded = true;*
_ nearest = hits*.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.collider.isTrigger && right_hits*.distance < nearest)*
* {
grounded = true;_

nearest = right_hits.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.collider.isTrigger && left_hits*.distance < nearest)*
* {
grounded = true;_

nearest = left_hits.distance;
_ }
}
}
i’ve wrote x axis but it doesn’t turn[23345-asd.jpg_
|23345]

*

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.