- Home /
Clamping Camera to player using Raycast
Hello everyone,
I am quite new to Unity and currently trying to work with my top down camera clamping to a player in a 3d environment. My question would be how would I try to clamp my camera while using Raycast. I figured out on how to use the Raycast to move my camera around but have no idea how to convert into vectors in order to use the Mathf.Clamp function. Is there any guidance to this or alternative method that i am not aware of? Any direction or help would be appreciated.
Code that i am currently working on
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraTarget : MonoBehaviour
{
[SerializeField] Camera cam;
// Update is called once per frame
void Update()
{
//this.transform.position = targetPos;
Ray ray = cam.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out RaycastHit raycastHit))
{
transform.position = raycastHit.point;
}
}
}
The goal is to clamp the camera to the player position so that the camera can't wander off a certain distance from the player itself.
Can you give more information about what you mean with "Clamping the camera to the player position"? Do you want to have the camera following the players position? Have the camera be static but only move when the players gets to the edge of the screen? Have a certain radius where it should move? Im also not very sure what you are using the raycast for. So if you have any more information about the situation you are trying to achieve, that be nice.
@metalted having a certain radius where it can move from the player that controls the character. basically a top down shooter game but in 3d space.
https://www.youtube.com/watch?v=LFe017d-S58&ab_channel=TheZBillDyl
an example can be seen for the first 10 sec of the video if it makes it clear of what im trying to do.
Couldn't you follow that tutorial on Youtube if that is what you want? If the game is a top down 3d shooter, the camera would basically be the same as the 2D one. Only difference will be that the raycast hit's height value could be different, because of the 3d objects, but you could set the raycast hit height to a constant value or use a seperate raycast layer for you camera script so the raycast doesnt actually hit the world, but like a plane underneath your world. Would that be a solution?
Unsure how to translate Raycast into vectors if I intend to use the Mathf function if I follow the tutorial since it takes vectors. Hence if there's a method to do so or another method besides using Mathf.Clamp? Any help would be appreciated.
Answer by metalted · Dec 17, 2021 at 04:11 PM
Checking out the tutorial, this script has a similar function but for a 3D space:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TopDownTutorialCamera : MonoBehaviour
{
public Camera cam;
public Transform player;
public float threshold;
public float cameraHeight = 10f;
void Update()
{
Ray ray = cam.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
Vector3 mousePos = hit.point;
Vector3 targetPos = (player.position + mousePos) / 2f;
targetPos.x = Mathf.Clamp(targetPos.x, -threshold + player.position.x, threshold + player.position.x);
targetPos.y = cameraHeight;
targetPos.z = Mathf.Clamp(targetPos.z, -threshold + player.position.z, threshold + player.position.z);
transform.position = targetPos;
}
}
}
I tested this script with a simple player cube and a world plane. The mousePos vector is not really neccessary but it keeps the tutorial code the same.
Tutorial: https://www.youtube.com/watch?v=LFe017d-S58&ab_channel=TheZBillDyl
Oh thanks for helping out!, i'll check this script now
It works! Thank you so much for helping out!
Your answer
Follow this Question
Related Questions
Animation root node is displaced? 0 Answers
How to create a stationary weapon FPS controller ? 2 Answers
UV mapped model with material tiling? 0 Answers
Finding enemies from a list script 1 Answer
Prefabs can't be attached together with a hinge joint (A joint can't connect the body to itself) 1 Answer