Clamp vector values to a minimum circular radius around player?
I have been messing around with this for a couple days now and feel like there is probably a simple solution that is just not occurring to me.
Top-down shooter; I am using a main camera raycast to point a turret at the mouse cursor. However, I am trying to determine how best to limit the rotation of the turret along X and Z so that it will not point too far down when the mouse position is close to the player.
Ideally, when the mouse cursor is within the minimum radius, I would like the turret to continue rotating around the Y-axis to "face" the mouse cursor, but for rotation along X and Z to stop at a minimum angle until the cursor is moved outside the minimum radius.
Am I correct in assuming that the turret direction vector needs to have its minimum values clamped to X and Z coordinates on a circle with radius R, with an origin of player X and player Z? If so, I am at a loss how to implement that logic.
If anyone has any idea how to go about this I would like to hear it.
public class CrosshairRaycast : MonoBehaviour
{
public GameObject Player;
[SerializeField]
private Transform turret;
public float turretTurnSpeed = 5f;
Quaternion turretQuat = Quaternion.identity;
void Start()
{
}
void FixedUpdate()
{
//New ray from the camera origin to the crosshair position
Ray rayOrigin = Camera.main.ScreenPointToRay(Input.mousePosition);
//New raycast hit
RaycastHit hitInfo;
//If the raycast hits anything, store hit info "hitinfo" from cursor and run logic. Otherwise do nothing
if (Physics.Raycast(rayOrigin, out hitInfo))
{
//If the raycast is hitting a collider, otherwise do nothing
if (hitInfo.collider != null)
{
//New vector "directionTarget" to determine the proper turret direction. Destination vector "hitinfo.point" minus source.
Vector3 directionTarget = hitInfo.point - turret.position;
turretQuat = Quaternion.LookRotation(directionTarget, Vector3.up);
turret.rotation = Quaternion.Slerp(turret.rotation, turretQuat, (Time.deltaTime * turretTurnSpeed));
Debug.DrawRay(turret.position, directionTarget, Color.red);
}
}
}
}
Answer by kylecat6330 · Feb 05, 2020 at 07:19 AM
I'm not sure I completely understand the question, but it sounds like you are just asking where/how to clamp the x and z rotations of the turret, so it doesn't look too far down.
In which case you would first need to figure out what angles you want it at. Place some Debug.Logs to give you the directionTraget's x and z values.
Debug.Log("X: " + directionTarget.x);
Debug.Log("Z: " + directionTarget.z);
Once you know what you want your minimums and maximums to be you can clamp them and put them into directionTarget.
Vector3 directionTarget = hitInfo.point - turret.position;
directionTarget.x = Mathf.Clamp(directionTarget.x, xMin, xMax);
directionTarget.z = Mathf.Clamp(directionTarget.z, zMin, zMax);
I hope that helps.
Your answer
Follow this Question
Related Questions
assigning several positions and call array of raycasts 0 Answers
My Ray is going the wrong way 1 Answer
Raycast under crosshair 0 Answers