Cannot figure out how to measure the distance of a raycast being sent out to the side of an object
Basically I have a camera in 3d space that the user controls for a 3d house tour. I have a raycast that prevents the user from going through walls that works good using raycast as long as the wall is in front of the user and they are moving forward. My problem is I want to replicate that when the user moves the camera from side to side so the camera will stop when it gets too close to a wall. I figured out how to send the raycast to the left and right of the camera but I cant get the raycast distance using that method. If anyone knows a way to get the distance or a way in general to get the raycast to prevent the user from going through a wall I would really appreciate it.
This is the raycast I want to make work
if (horizontalTranslation.isActivated())
{
float translateX = Input.GetAxis(mouseHorizontalAxisName) * horizontalTranslation.sensitivity;
transform.Translate(translateX, 0, 0);
var lft = transform.TransformDirection(Vector3.left);
var rgt = transform.TransformDirection(Vector3.right);
//left raycast
if (Physics.Raycast(transform.position, lft, 4))
{
originalRayPos = transform.position;
originalRayRot = transform.rotation;
Debug.LogWarning("left raycast set");
}
if (Physics.Raycast(transform.position, lft, 1))
{
transform.position = originalRayPos;
transform.rotation = originalRayRot;
Debug.LogWarning("left raycast fire");
}
//right raycast
if (Physics.Raycast(transform.position, rgt, 4))
{
originalRayPos = transform.position;
originalRayRot = transform.rotation;
Debug.LogWarning("right raycast set");
}
if (Physics.Raycast(transform.position, rgt, 1))
{
transform.position = originalRayPos;
transform.rotation = originalRayRot;
Debug.LogWarning("right raycast fire");
}
}
This is what I did for the raycast for when the camera moves forward and it works perfect I'm just adding it here for reference because this is what I want to accomplish with the other code
if (scroll.isActivated())
{
float translateZ = Input.GetAxis(scrollAxisName) * scroll.sensitivity;
transform.Translate(0, 0, translateZ);
Ray ray = GetComponent<Camera>().ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
// the object identified by hit.transform was clicked
// do whatever you want
if ((hit.distance < 5) && (hit.distance > 1))
{
originalRayPos = transform.position;
originalRayRot = transform.rotation;
}
if(hit.distance < 1)
{
transform.position = originalRayPos;
transform.rotation = originalRayRot;
}
}
}
}
I was able to figure out how to get the distance of the hit but this method isn't working correctly to stop the user from going through the wall if they move from the side. Here is what I have now. It is pretty buggy and doesn't work too great. the camera gets stopped when it shouldn't be stopped. If anyone has an alternative or a method they could point me towards that would be awesome.
if (horizontalTranslation.isActivated())
{
float translateX = Input.GetAxis(mouseHorizontalAxisName) * horizontalTranslation.sensitivity;
transform.Translate(translateX, 0, 0);
RaycastHit hitLeft;
RaycastHit hitRight;
var lft = transform.TransformDirection(Vector3.left);
var rgt = transform.TransformDirection(Vector3.right);
if (Physics.Raycast(transform.position, lft, out hitLeft))
{
Debug.LogWarning(hitLeft.distance);
if((hitLeft.distance < 4) && (hitLeft.distance > 1))
{
originalRayPos = transform.position;
originalRayRot = transform.rotation;
}
if (hitLeft.distance < 1)
{
transform.position = originalRayPos;
transform.rotation = originalRayRot;
}
}
if (Physics.Raycast(transform.position, rgt, out hitRight))
{
Debug.LogWarning(hitRight.distance);
if ((hitRight.distance < 4) && (hitRight.distance > 1))
{
originalRayPos = transform.position;
originalRayRot = transform.rotation;
}
if (hitRight.distance < 1)
{
transform.position = originalRayPos;
transform.rotation = originalRayRot;
}
}
}
Answer by streeetwalker · Apr 02, 2020 at 05:57 PM
Hi @andrewu331, I'm trying to figure out why you can't get this RayCast hit distance when you cast rays from the left or right side of the camera. hit.distance is the distance you need, just like the RayCast you did using ScreenToPointRay, so where was the problem? What method were you using?
OK, here is a simple solution that does both directions in one shot. I've tested this on paper, and it works given the assumptions:
// assumes player moves right or left parallel to world x
// get the direction and magnitude of the move
// use those to determine if player can move
// uses a padding value to prevent movement too close
// to an obstacle
float translateX = Input.GetAxis(mouseHorizontalAxisName) * horizontalTranslation.sensitivity;
float magnitudeX = Math.Abs( translateX ); // how far?
if( magnitudeX > 0 ) { // if moving
float direction = Mathf.Sign( translateX ); // left or right?
float padding = 1f; // give me some room!
Vector3 rayCastDir = Vector3.right * direction; // ray direction
if ( Physics.Raycast( transform.position, rayCastDir, out hit ) ) {
float distance = hit.distance - padding; // apply padding
if( distance > 0 ) { // there is room to move
if( distance < magnitudeX ) { // translateX moves too far
// move close as possible
translateX = distance * direction;
}
} else translateX = 0; // no room to move
}
transform.position = transform.position + new Vector3( translateX, 0, 0 );
}
I figured out how to get the distance I ended up posting a reply with the new code under my original post. The problem now is the raycast is hitting stuff that isn't there so I think I don't have it set up right.
@@andrewu331, OK, I looked at your new code, but coulple questions first: 1. are you always moving left or right parallel with the world x axis, or the player can be at an angle and you want to move aligned with the players local x axis? 2. is the camera tied to the player?
@streeetwalker The player can be at any angle and I want to move aligned with the players local x axis and the player is actually moving the camera itself. Sorry for the late response.
Your answer
![](https://koobas.hobune.stream/wayback/20220612230301im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Why is my Physics.Raycast not returning true? 0 Answers
Cone 'Line of Sight' will not trigger on collision 0 Answers
Ray cast to object to transform, instead all objects with script transform. 0 Answers
Raycast from camera only working from certain angles 1 Answer
cant get raycast to work with tags 1 Answer