So I have a script that makes my camera move like the camera in Enter the Gungeon, The camera follows the player when the player moves. However, when I move the mouse to shift the camera, the camera moves based the origin instead of based off player.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraScript : MonoBehaviour
{
//Camera Movement
public Transform player;
Vector3 target, mousePos, shakeOffset;
Vector3 refVel = Vector3.zero;
float cameraDistance = 3.5f;
public float smoothTime = 0.3f, zStart;
//Camera Shake
float shakeMag, shakeTimeEnd;
Vector3 shakeVector;
bool shaking;
void Start()
{
target = player.position;
zStart = transform.position.z;
}
void Update()
{
mousePos = CaptureMousePos();
shakeOffset = UpdateShake();
target = UpdateTargetPos();
UpdateCameraPosition();
}
Vector3 CaptureMousePos()
{
Vector2 ret = Camera.main.ScreenToWorldPoint(Input.mousePosition);
ret *= 2;
ret -= Vector2.one;
float max = 0.9f;
if (Mathf.Abs(ret.x) > max || Mathf.Abs(ret.y) > max)
{
ret = ret.normalized;
}
return ret;
}
Vector3 UpdateTargetPos ()
{
Vector3 mouseOffset = mousePos * cameraDistance;
Vector3 ret = mouseOffset + player.position;
ret += shakeOffset;
ret.z = zStart;
return ret;
}
void UpdateCameraPosition ()
{
Vector3 tempPos;
tempPos = Vector3.SmoothDamp(transform.position, target, ref refVel, smoothTime);
transform.position = tempPos;
}
public void Shake (Vector3 direction, float magnitude, float length)
{
shaking = true;
shakeVector = direction;
shakeMag = magnitude;
shakeTimeEnd = Time.time + length;
}
Vector4 UpdateShake()
{
if (!shaking || Time.time > shakeTimeEnd)
{
shaking = false;
return Vector3.zero;
}
Vector3 tempOffset = shakeVector;
tempOffset *= shakeMag;
return tempOffset;
}
}