- Home /
Smooth follow on WoW camera.. help?
NON-coder Alert!!
OK so I want my camera to move nicely behind my charater smoothly like with the built in SmoothFollow.js script.
Problem is I also want to be able to use the mouse to look about and steer (On mouse down.. NOT using MouseLook)
So I have this WoW camera script (credited as by matrix211v1)
This is almost perfect, except I wish it had that nice smooth movement that SmoothFollow gives (no I cant use both... tried...)
Can someone tell me which bit of SmoothFollow is doing this nice moving thing, cos some of it lokks very simerlar to a few lines in the WoW camera script...
For your delectation Script 1. SmoothFollow
// The target we are following
var target : Transform;
// The distance in the x-z plane to the target
var distance = 10.0;
// the height we want the camera to be above the target
var height = 5.0;
// How much we
var heightDamping = 2.0;
var rotationDamping = 3.0;
// Place the script in the Camera-Control group in the component menu
@script AddComponentMenu("Camera-Control/Smooth Follow")
function LateUpdate () {
// Early out if we don't have a target
if (!target)
return;
// Calculate the current rotation angles
wantedRotationAngle = target.eulerAngles.y;
wantedHeight = target.position.y + height;
currentRotationAngle = transform.eulerAngles.y;
currentHeight = transform.position.y;
// Damp the rotation around the y-axis
currentRotationAngle = Mathf.LerpAngle (currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime);
// Damp the height
currentHeight = Mathf.Lerp (currentHeight, wantedHeight, heightDamping * Time.deltaTime);
// Convert the angle into a rotation
currentRotation = Quaternion.Euler (0, currentRotationAngle, 0);
// Set the position of the camera on the x-z plane to:
// distance meters behind the target
transform.position = target.position;
transform.position -= currentRotation * Vector3.forward * distance;
// Set the height of the camera
transform.position.y = currentHeight;
// Always look at the target
transform.LookAt (target);
}
And now Script2. My Wow Camera.js (Im not sure if all of this is nessasary, I tried to strip away some stuff that a partner added...
//WowCamera.js
//When the camera hits something, it "instantly" gets behind the player.
//Credits to this script: "matrix211v1"
var target : Transform;
//not sure what JD is doing here… leave it... for now...
private var dataBase;
var targetHeight = 3.0;
var distance = 7.0;
var maxDistance = 20;
var minDistance = 2.5;
var xSpeed = 250.0;
var ySpeed = 120.0;
var yMinLimit = -20;
var yMaxLimit = 80;
var zoomRate = 10;
var rotationDampening = 3.0;
var theta2 : float = 0.5;
private var x = 0.0;
private var y = 0.0;
private var fwd = new Vector3();
private var rightVector = new Vector3();
private var upVector = new Vector3();
private var movingVector = new Vector3();
private var collisionVector = new Vector3();
private var isColliding : boolean = false;
private var a1 = new Vector3();
private var b1 = new Vector3();
private var c1 = new Vector3();
private var d1 = new Vector3();
private var e1 = new Vector3();
private var f1 = new Vector3();
private var h1 = new Vector3();
private var i1 = new Vector3();
function Start () {
var angles = transform.eulerAngles;
x = angles.y;
y = angles.x;
/* Make the rigid body not change rotation
if (rigidbody)
rigidbody.freezeRotation = true;
*/
}
function LateUpdate () {
if(!target)
return;
// If either mouse buttons are down, let them govern camera position
if (Input.GetMouseButton(0) || Input.GetMouseButton(1))
{
x += Input.GetAxis("Mouse X") * xSpeed * 0.02;
y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;
// otherwise, ease behind the target if any of the directional keys are pressed
} else if(Input.GetAxis("Vertical") || Input.GetAxis("Horizontal")) {
var targetRotationAngle = target.eulerAngles.y;
var currentRotationAngle = transform.eulerAngles.y;
x = Mathf.LerpAngle(currentRotationAngle, targetRotationAngle, rotationDampening * Time.deltaTime);
}
distance -= (Input.GetAxis("Mouse ScrollWheel") * Time.deltaTime) * zoomRate * Mathf.Abs(distance);
distance = Mathf.Clamp(distance, minDistance, maxDistance);
y = ClampAngle(y, yMinLimit, yMaxLimit);
var rotation:Quaternion = Quaternion.Euler(y, x, 0);
var position = target.position - (rotation * Vector3.forward * distance + Vector3(0,-targetHeight,0));
// Check to see if we have a collision
collisionVector = AdjustLineOfSight(transform.position, position);
// Check Line Of Sight
if (collisionVector != Vector3.zero)
{
Debug.Log("Check Line Of Sight");
a1 = transform.position;
b1 = position;
c1 = AdjustLineOfSight(transform.position, position);
d1 = c1 - a1;
e1 = d1.normalized * -1;
f1 = d1 + e1 * 1;
g1 = f1 + a1;
position = g1;
// check distance player to camera
h1 = position - a1;
if (h1.magnitude < 10)
{
position = a1 - fwd * 4;
//position.y = targetPlayer.y;
theta2 = theta2 + .25;
}
// set new camera distance
h1 = position - a1;
distance = h1.magnitude;
}
//check collision
if (Physics.CheckSphere (position, .5) )
{
a1 = transform.position;
newPosition = a1 - fwd * 4;
//newPosition.y = targetPlayer.y;
theta2 = theta2 + .25;
// set new camera distance
h1 = position - a1;
distance = h1.magnitude;
}
position = Vector3.Slerp(transform.position, position, Time.deltaTime * 100);
transform.rotation = rotation;
transform.position = position;
}
static function ClampAngle (angle : float, min : float, max : float) {
if (angle < -360)
angle += 360;
if (angle > 360)
angle -= 360;
return Mathf.Clamp (angle, min, max);
}
function AdjustLineOfSight (vecA: Vector3, vecB: Vector3)
{
var hit: RaycastHit;
if (Physics.Linecast (vecA, vecB, hit))
{
// Debug.Log("I hit something");
// return hit.point;
}
return Vector3.zero;
}
REALLY hope someone can help!!
Cheers... MArk
Best of luck dude...... might someone will have enough time to read the whole code and figure out your problem with Proper Solution.........Cheers....
Look at the date, it's not relevant anymore. Someone just revived it, because he wasn't able to use the code.
Answer by UTeam · Mar 26, 2013 at 02:02 PM
I have a quick question.
How do i translate this line into c#:
var wantedRotationAngle = target.eulerAngles.y;
;)
Answer by Ziron999 · Feb 12, 2014 at 11:33 AM
var wantedRotationAngle = target.eulerAngles.y;
=
float wantedRotationAngle = target.eulerAngles.y;
transform.position.y = currentHeight;
=
transform.position = new Vector3(transform.position.x, currentHeight, transform.position.z);
Your answer
Follow this Question
Related Questions
Change hieght on smooth follow OnTriggerEnter 0 Answers
How to make animated animal's head smoothly look at player instead of instantly snapping? 1 Answer
Mouse orbit + Smooth follow script question 0 Answers
How to make animated animal's head smoothly look at player instead of instantly snapping? 1 Answer
Setting Target of SmoothFollow Script After Initiating Prefab 1 Answer