using UnityEngine;
public class CameraMovement : MonoBehaviour
{
// Reference to the player GameObject to follow
public Transform player;
// Offset between the camera and the player
private Vector3 offset;
// Called once when the script is enabled
void Start()
{
// Calculate the initial offset between the camera and the player
offset = transform.position - player.position;
}
// Called every frame after all Update functions have been called
void LateUpdate()
{
// Update the camera position to directly follow the player
transform.position = player.position + offset;
}
}
public Transform player;
),
using UnityEngine;
public class CameraMovement : MonoBehaviour
{
// Reference to the player GameObject to follow
public Transform player;
// Speed at which the camera follows the player
public float followSpeed = 4f;
// Offset between the camera and the player
private Vector3 offset;
// Called once when the script is enabled
void Start()
{
// Calculate the initial offset between the camera and the player
offset = transform.position - player.position;
}
// Called every frame after all Update functions have been called
void LateUpdate()
{
// Calculate the target position for the camera to move towards
Vector3 targetPosition = player.position + offset;
// Smoothly move the camera towards the target position using Lerp
transform.position = Vector3.Lerp(transform.position, targetPosition, followSpeed * Time.deltaTime);
}
}
using UnityEngine;
public class CrosshairPlacement : MonoBehaviour
{
void Update()
{
// Cast a ray from the camera through the mouse position
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
// If the ray hits something, place the crosshair at the hit point
transform.position = hit.point;
}
}
}
In this script, a ray is cast from the camera through the mouse cursor.
To fix the problem with the Crosshair moving towards the camera when you stop moving the mouse, you can disable the Crosshair's Sphere Collider component (the ray was hitting that collider).
We replace: transform.position = hit.point;
with transform.position = new Vector3(hit.point.x, hit.point.y + 1.3f, hit.point.z);
so the crosshair hovers above the ground.
We want the crosshair to hover above the ground, not above other colliders (e.g., Player's collider). To do that, we will add a new Layer named "Ground", and we will set it so the ray will be able to hit only the colliders in that layer.
using UnityEngine;
public class CrosshairPlacement : MonoBehaviour
{
// Layer mask for ground objects
public LayerMask groundLayerMask;
void Start()
{
//Set Cursor to not be visible
//Cursor.visible = false;
}
void Update()
{
// Create a ray that starts from the main camera and passes through the mouse position on the screen
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
// RaycastHit provides details about where the ray hits an object
RaycastHit hit;
// Cast a ray into the scene and check for collisions
if (Physics.Raycast(ray, out hit, Mathf.Infinity, groundLayerMask))
{
// If the ray hits something, place the crosshair at the hit point
transform.position = new Vector3(hit.point.x, hit.point.y+1.3f, hit.point.z);
}
}
}
public LayerMask groundLayerMask;
).using UnityEngine;
public class CameraMovement : MonoBehaviour
{
// References to the Player and Crosshair
public Transform player;
public Transform crosshair;
public float followSpeed = 4f;
private Vector3 offset;
void Start()
{
offset = transform.position - player.position;
}
void LateUpdate()
{
// Calculate the target position for the camera to move towards
Vector3 targetPosition = this.PositionBetween(player.position, crosshair.position, 0.4f) + offset;
transform.position = Vector3.Lerp(transform.position, targetPosition, followSpeed * Time.deltaTime);
}
// Calculate the position between two given positions (between player and crosshair)
public Vector3 PositionBetween(Vector3 startPosition, Vector3 endPosition, float fraction)
{
return Vector3.Lerp(startPosition, endPosition, fraction);
}
}
Final state:
The floor is larger and I added a few 3D objects (a cube and a sphere).