Camera Help

Hello everyone,

I am working on a camera script (similar to a WoW camera) where the player is allowed to hold down either mouse button and adjust the camera angle by dragging it around. Also, the scrollwheel should zoom in and out from the player.

Here are the problems I am having:

  1. The scrollwheel zoom only works when the mouse is held down, and is messed up when used without a mouse button down.
  2. The camera follows the player when he jumps only when the mouse button is held down, and otherwise ignores the players y position (even when going up a hill).
  3. every half a second or so, the scene ‘twitches’ (only if walking) Not sure if this is script related, or because im using a not-so-new computer.
  4. Any other recommendations for my code? What are some general guidelines for making ‘clean’ and fast working code (I fear the computer doesn’t like mine that much…)?

Thank you sooooooooo much!!!

Here is part of my code (the parts i think are important)

function LateUpdate () {
	var targetcenter = target.position + centeroffset;
	var targethead = target.position + headoffset;
	targetheight = targetcenter.y + height;
	
	var angles = transform.eulerAngles;
	xangles=angles.y;
	yangles=angles.x;
	
	MoveCam (Vector3(targetcenter.x, targetheight, targetcenter.z));
	
	//mousescroll
	distance -= (Input.GetAxis("Mouse ScrollWheel") * Time.deltaTime) * zoomrate * Mathf.Abs (distance);
	distance = Mathf.Clamp(distance, mindistance, maxdistance);
}

and the main function of interest:

function MoveCam (targetcenter : Vector3) {
		
	if (Input.GetMouseButton(0) || Input.GetMouseButton(1)){
		xangles += Input.GetAxis("Mouse X") * xSpeed * 0.05;
		yangles -= Input.GetAxis("Mouse Y") * ySpeed * 0.05;
		var rotation1 :Quaternion = Quaternion.Euler(yangles,xangles,0);
	
		position1= target.position - (rotation1* Vector3.forward * distance + Vector3(0,-targetheight,0));
		transform.rotation = rotation1;
		transform.position = position1;
		lockposition= position1;
		lockrotation = rotation1;
	}
	else {
		var position = transform.position;
		var offset = position - targetcenter;
		var newtargetposition = target.position - (lockrotation*Vector3.forward*distance + Vector3(0,-targetheight,0));
		//confused about the optimal script for this variable "newtargetposition"
		
		var newposition : Vector3;
	
		newposition.x = newtargetposition.x;
		newposition.y = lockposition.y;
		newposition.z = newtargetposition.z;
	
		transform.position = newposition;
	}
}

bump

When you say the scroll wheel is “messed up” without the button down, do you mean nothing happens or something strange happens?

something strange happens. Basically without the button down, the scroll only works by moving along the xz plane. No scrolling along the y axis happens – in fact no moving of the camera happens at ALL along the y axis (even when going up hills) without the mouse button down.

In the “else” part of the statement, you seem to be setting the value of newtargetposition.y, but the Y value you actually use is from lockposition.y - if lockposition.y doesn’t get updated then presumably the camera’s height will not change.

hey I switched the “newposition.y = lockposition.y” with just “newposition.y = newtargetposition.y” and there were some improvements! thanks! But there still are a couple kinks if you wouldn’t mind addressing…

Now the mouse scroll works pretty well, the old bug is fixed.

But I would like some advice on fixing the camera movement when the player jumps into the air, or is going up a hill. When jumping, the camera goes too high and the result makes it look unrealistic. Also, the same kinda thing happens when the player goes up a hill. The camera goes way too high, and the player is not even shown on camera anymore. Is there a way to make the camera stationary when jumping, but follow the player smoothly when going up a hill? Thanks again

There’s a great little WoW-like camera movement set of scripts in this thread. Credit to matrix211v1 and Paintbrush for some great scripting!

I tried it out, and it works extremely well for me. It might not be exactly what you want, but at least it may be a good starting point for you. There are two different sets, one by matrix211v1

Hope this helps.