I have my player running around well and the camera works, but I don’t want my camera attached square on the player. I’m making an isometricish dungeon crawler and wanted the camera to more or less follow the player and for the player to face the mouse pointer, but for the camera to drift to wherever the mouse is currently.
In other words, the camera follows the mouse, but there is a ‘boundary’ around the player so if he starts moving it’ll tag along. This will probably conflict with my current camera, as it relies on the player facing where the camera is pointing on a Vector3.
My current movement file:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[DisallowMultipleComponent]
[RequireComponent(typeof(Animator))]
[RequireComponent(typeof(Rigidbody))]
[RequireComponent(typeof(CapsuleCollider))]
public class Movement : MonoBehaviour
{
Animator anim;
private Camera mainCamera;
bool isWalking = false;
const float WALK_SPEED = .25f;
void Start ()
{
mainCamera = FindObjectOfType<Camera>();
}
void Awake()
{
anim = GetComponent<Animator>();
}
void Update()
{
Turning();
Walking();
Move();
Jump();
//Player look
Ray cameraRay = mainCamera.ScreenPointToRay(Input.mousePosition);
Plane groundPlane = new Plane(Vector3.up, Vector3.zero);
float rayLength;
if (groundPlane.Raycast(cameraRay, out rayLength))
{
Vector3 pointToLook = cameraRay.GetPoint(rayLength);
Debug.DrawLine(cameraRay.origin, pointToLook, Color.blue);
transform.LookAt(new Vector3(pointToLook.x, transform.position.y, pointToLook.z));
}
}
void Turning()
{
anim.SetFloat("Turn", Input.GetAxis("Horizontal"));
}
void Walking()
{
if (Input.GetKeyDown(KeyCode.LeftShift)) {
isWalking = !isWalking;
anim.SetBool("Walk", isWalking);
}
}
void Move()
{
if (anim.GetBool("Walk"))
{
anim.SetFloat("MoveZ", Mathf.Clamp(Input.GetAxis("MoveZ"), -WALK_SPEED, WALK_SPEED));
anim.SetFloat("MoveX", Mathf.Clamp(Input.GetAxis("MoveX"), -WALK_SPEED, WALK_SPEED));
}
else
{
anim.SetFloat("MoveZ", Input.GetAxis("MoveZ"));
anim.SetFloat("MoveX", Input.GetAxis("MoveX"));
}
}
void Jump()
{
if (Input.GetKeyDown(KeyCode.Space))
anim.SetTrigger("Jump");
}
}
My current camera file:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SmoothCamera : MonoBehaviour
{
public Transform lookAt;
private bool smooth = true;
private float smoothSpeed = 0.125f;
private Vector3 offset = new Vector3(0, 10, -10);
private void LateUpdate()
{
Vector3 desiredPosition = lookAt.transform.position + offset;
if (smooth)
{
transform.position = Vector3.Lerp(transform.position, desiredPosition, smoothSpeed);
}
else {
transform.position = desiredPosition;
}
}
}
I’m still (obviously) pretty new so excuse me if this is a dumb question.