- Home /
Top down shooter camera follow cursor
Hi, I'm facing a problem that I can't seem to solve. I want the camera to follow the mouse cursor, but the player character to remain visible. So the camera can't go a certain distance away from the player. Can anybody clear some stuff up for me?
Answer by simco500 · Jan 20, 2013 at 02:53 AM
I think I know what you mean, I've had the same problem but with a little thinking I got it. I made a script for the camera where it calculates the middle between the Player and the position of the cursor. Then I only needed to make something that made the camera follow that position in the x and z direction and voila :). You can tweak it as much if you want. This script I made was perfect for my project, I hope it'll work with you too.
var Damping = 5.0;
var Player : Transform;
var Height : float = 4;
var Offset : float = 5;
private var Center : Vector3;
var ViewDistance : float = 5.0;
function Update ()
{
var mousePos = Input.mousePosition;
mousePos.z = ViewDistance;
var CursorPosition : Vector3 = Camera.main.ScreenToWorldPoint(mousePos); /
var PlayerPosition = Player.position;
Center = Vector3((PlayerPosition.x + CursorPosition.x) / 2, PlayerPosition.y, (PlayerPosition.z + CursorPosition.z) / 2);
transform.position = Vector3.Lerp(transform.position, Center + Vector3(0,Height,Offset), Time.deltaTime * Damping);
}
Answer by simco500 · Jan 20, 2013 at 02:53 AM
Hey, I've tought a little about your problem and here's what I did: calculate the middle between the player and the position of the mouse in the world. Let the camera smooth follow that middle. Actually quite simple but it took me quite a while to come up with.
var Damping = 12.0;
var Player : Transform;
var Height : float = 13.0;
var Offset : float = 0.0;
private var Center : Vector3;
var ViewDistance : float = 3.0;
function Update ()
{
var mousePos = Input.mousePosition;
mousePos.z = ViewDistance;
var CursorPosition : Vector3 = Camera.main.ScreenToWorldPoint(mousePos);
var PlayerPosition = Player.position;
Center = Vector3((PlayerPosition.x + CursorPosition.x) / 2, PlayerPosition.y, (PlayerPosition.z + CursorPosition.z) / 2);
transform.position = Vector3.Lerp(transform.position, Center + Vector3(0,Height,Offset), Time.deltaTime * Damping);
}
I've never seen a copy paste work so easly. I love you
(from 2020)
How is this code working for you? I'm getting about 26 errors, like
Assets\Player\Scripts\CameraFollow.cs(5,42): error CS1003: Syntax error, ',' expected
Assets\Player\Scripts\CameraFollow.cs(13,19): error CS1002: ; expected
Assets\Player\Scripts\CameraFollow.cs(13,19): error CS1022: Type or namespace definition, or end-of-file expected
Assets\Player\Scripts\CameraFollow.cs(13,19): error CS1022: Type or namespace definition, or end-of-file expected
Assets\Player\Scripts\CameraFollow.cs(13,21): error CS0116: A namespace cannot directly contain members such as fields or methods
Assets\Player\Scripts\CameraFollow.cs(13,27): error CS1022: Type or namespace definition, or end-of-file expected
Assets\Player\Scripts\CameraFollow.cs(19,25): error CS1002: ; expected
Assets\Player\Scripts\CameraFollow.cs(19,25): error CS1513: } expected
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraFollow : MonoBehaviour
var Damping = 12.0;
var Player : Transform;
var Height : float = 13.0;
var Offset : float = 0.0;
private var Center : Vector3;
var ViewDistance : float = 3.0;
function Update ()
{
var mousePos = Input.mousePosition;
mousePos.z = ViewDistance;
var CursorPosition : Vector3 = Camera.main.ScreenToWorldPoint(mousePos);
var PlayerPosition = Player.position;
Center = Vector3((PlayerPosition.x + CursorPosition.x) / 2, PlayerPosition.y, (PlayerPosition.z + CursorPosition.z) / 2);
transform.position = Vector3.Lerp(transform.position, Center + Vector3(0,Height,Offset), Time.deltaTime * Damping);
}