22-11-2010, 04:34 PM
Ecco qui 2 script che permettono di creare una camera è un controllere stile WOW!
Script da applicare alla camera (WowCamera.js) :
Script per controllare il personaggio (WowController.js) :
Good Work! rofl
Script da applicare alla camera (WowCamera.js) :
Codice:
var target : Transform;
var targetHeight = 12.0;
var distance = 5.0;
var maxDistance = 20;
var minDistance = 2.5;
var xSpeed = 250.0;
var ySpeed = 120.0;
var yMinLimit = -20;
var yMaxLimit = 80;
var zoomRate = 20;
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();
@script AddComponentMenu("Camera-Control/WoW Camera")
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;
}
Codice:
private var jumpSpeed:float = 8.0;
private var gravity:float = 20.0;
private var runSpeed:float = 50.0;
private var walkSpeed:float = 15.0;
private var rotateSpeed:float = 150.0;
private var grounded:boolean = false;
private var moveDirection:Vector3 = Vector3.zero;
private var isWalking:boolean = false;
private var moveStatus:String = "idle";
private var jumping:boolean = false;
private var moveSpeed:float = 0.0;
function Update ()
{
// Only allow movement and jumps while grounded
if(grounded) {
moveDirection = new Vector3((Input.GetMouseButton(1) ? Input.GetAxis("Horizontal") : 0),0,Input.GetAxis("Vertical"));
// if moving forward and to the side at the same time, compensate for distance
// TODO: may be better way to do this?
if(Input.GetMouseButton(1) && Input.GetAxis("Horizontal") && Input.GetAxis("Vertical")) {
moveDirection *= .7;
}
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= isWalking ? walkSpeed : runSpeed;
moveStatus = "idle";
if(moveDirection != Vector3.zero)
moveStatus = isWalking ? "walking" : "running";
// Jump!
if(Input.GetButton("Jump"))
moveDirection.y = jumpSpeed;
}
// Allow turning at anytime. Keep the character facing in the same direction as the Camera if the right mouse button is down.
if(Input.GetMouseButton(1)) {
transform.rotation = Quaternion.Euler(0,Camera.main.transform.eulerAngles.y,0);
} else {
transform.Rotate(0,Input.GetAxis("Horizontal") * rotateSpeed * Time.deltaTime, 0);
}
if(Input.GetMouseButton(1) || Input.GetMouseButton(0))
Screen.lockCursor = true;
else
Screen.lockCursor = false;
// Toggle walking/running with the T key
if(Input.GetAxis("Run") == 1)
isWalking = !isWalking;
//Apply gravity
moveDirection.y -= gravity * Time.deltaTime;
//Move controller
var controller:CharacterController = GetComponent(CharacterController);
var flags = controller.Move(moveDirection * Time.deltaTime);
grounded = (flags & CollisionFlags.Below) != 0;
}
function GetSpeed () {
if (moveStatus == "idle")
moveSpeed = 0;
if (moveStatus == "walking")
moveSpeed = walkSpeed;
if (moveStatus == "running")
moveSpeed = runSpeed;
return moveSpeed;
}
function IsJumping () {
return jumping;
}
function GetWalkSpeed () {
return walkSpeed;
}
@script RequireComponent(CharacterController)
Good Work! rofl