TopDown Shooter style camera.

Hey guys. I’m having some trouble in my top down shooter game. My character is on the screen and the gun rotates correctly, and now im looking for a camera script that does not position itself. I want it to stay at its current position, but follow the player without rotating or anything…

Im also looking for a better gun rotation script. Mine is working fine but is going too slow and is quite annoying, here it is:

function Start() {
Screen.showCursor = false;
}

function Update () {
	var position = Input.mousePosition;
    newposition = Vector3(position.x,position.y,camera.main.transform.position.y-      transform.position.y);
    var lastposition = camera.main.ScreenToWorldPoint(newposition);
    transform.LookAt(lastposition);
}

If Help is given, Name will be in credits. When the game is complete i plan to place it on http://www.kongregate.com

Currently investigating, one moment… :slight_smile:

Thanks!

I think you could use the FPS camera script and remove the mouse rotate component. You can then readjust the camera so its over the players head looking down instead of showing through the characters eyes.

You could also create a camera and attach it to the player.

First suggestion did not work.
Attaching a camera to the player would make the camera rotate along with the player using the mouse, and i want only the player to rotate…

I just want the camera to stay right where it is, but stay hovering over the character.

You can set the camera to follow your characters X and Z movements only and keep the Y constant, then it will never rotate (Assuming this is a top-down) game

suto code

transform.position.x = character.transform.position.x
transform.position.z = character.transform.position.z

It is a top down game. How would i do this? Some pseudo code would be helpful :slight_smile:

By follow the player, without rotating or anything, you mean it just hovers above it and looks at it but doesn’t rotate when the object it is following rotates?

If so, can you use a script from the Penelope tutorial.

I believe it would be the FollowTransform.js script.

You basically need to make your camera position relative to the position of your player’s transform.


As far as the rotation being slow, what do you mean? It should always look at the mouse, and move as fast as you move the mouse.

Is it slowly rotating to the position of the mouse?

//////////////////////////////////////////////////////////////
// FollowTransform.js
// Penelope iPhone Tutorial
//
// FollowTransform will follow any assigned Transform and 
// optionally face the forward vector to match for the Transform
// where this script is attached.
//////////////////////////////////////////////////////////////

var targetTransform : Transform;		// Transform to follow
var faceForward : boolean = false;		// Match forward vector?
private var thisTransform : Transform;

function Start()
{
	// Cache component lookup at startup instead of doing this every frame
	thisTransform = transform;
}

function Update () 
{
	thisTransform.position = targetTransform.position;
	
	if ( faceForward )
		thisTransform.forward = targetTransform.forward;
}

I will check out the penelope tutorial. thanks!

Here is the code for the gun. Just place it on a cube and press play. you will see what i mean.

function Update () {
var position = Input.mousePosition;
newposition = Vector3(position.x,position.y,camera.main.transform.position.y- transform.position.y);
var lastposition = camera.main.ScreenToWorldPoint(newposition);
transform.LookAt(lastposition);
}

Sawfish, This script moves the camera to the position of the target, it doesnt look at it

EDIT: I fixed that. Just added

thisTransform.position.x = targetTransform.position.x;
thisTransform.position.z = targetTransform.position.z;

Thats somebody to add to the credits :slight_smile:

anyways, now im looking for a script that will rotate my player correctly facing the cursor. Gosh,i need to work on my programming in unity…

Did you try this Dusk?

transform.position.x = character.transform.position.x
transform.position.z = character.transform.position.z

i haven’t written any javascript/unityscript in a while and haven’t tested this but it should work.

var height : float; //use this to set in the inspector how high you want the camera to hover over the player.
var target : Transform; //drag the player object to this in the inspector

function Start () {
	transform.eulerAngles = Vector3(90, 0, 0); //sets the camera to point straight down
}
function LateUpdate () {
	transform.position = target.position + (Vector3.up * height);
}

if you’re moving your character in FixedUpdate than you’ll probably want to change LateUpdate to FixedUpdate.

Yeah, Check the edit :slight_smile:

Thanks teatime, but i already got the camera script. Anyone got a script that will make an object look at the mouse, like a top down shooter game?

Try attaching the player to an empty game object along with the camera. The hierarchy would look a little like this.

Game object
|_
| … |
Cam … Player

Evac City tutorial by David Lancaster

Like i said, I already got a camera script. Anyone got a script that will make an object look at the mouse, like a top down shooter game?

Thats not Evac city in any way, shape, or form.

But thanks so much! It had the mouselook script in there and now im happy. Another name to be in the credits!
:smile:

MrDusklig are you a modeler?

In your typical top down shooter, the character has to be able to rotate independently of his movement, so you could look in one direction and move in another.

For me the solution was pretty simple, I used a setup where my character model is child object of my player object (the object with the character controller and FPSInputController), then I’m using the default SmoothFollow camera targeting the parent, and the model child object has a script called “LookAtMouse” that makes it rotate to face the mouse. That way the Camera does not rotate when the model rotates, because the camera is looking at the parent (which isn’t actually rotating). Then I make my bullets, etc. emit from the forward vector of the model object.