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:
- The scrollwheel zoom only works when the mouse is held down, and is messed up when used without a mouse button down.
- 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).
- 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.
- 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;
}
}