these are my current Character Controls
//moving around
var speed : float = 15.0;
var rotateSpeed : float = 3.0;
//shooting
var bulletPrefab:Transform;
//dying
static var dead = false;
//getting hit
var tumbleSpeed = 800;
var decreaseTime = 0.01;
var decayTime = 0.01;
static var gotHit = false;
private var backup = [tumbleSpeed, decreaseTime, decayTime];
function LateUpdate()
{
if(dead)
{
transform.position = Vector3(-40,4,0);
gameObject.Find("Main Camera").transform.position = Vector3(-40,4,0);
dead = false;
}
if(gotHit)
{
if(tumbleSpeed < 1)
{
//we're not hit anymore ... reset and get back into the game
tumbleSpeed = backup[0];
decreaseTime = backup[1];
decayTime = backup[2];
gotHit = false;
}
else
{
//we're hit.. spin character around
transform.Rotate(0,tumbleSpeed*Time.deltaTime,0,Space.World);
tumbleSpeed = tumbleSpeed - decreaseTime;
decreaseTime += decayTime;
}
}
}
//function OnControllerColliderHit(hit : ControllerColliderHit)//onTriggerEnter | collider
function OnTriggerEnter( hit : Collider )
{
if(hit.gameObject.tag == "fallout")
{
dead = true;
//subtract life here
HealthControl.LIVES -= 1;
}
if(hit.gameObject.tag == "enemyProjectile")
{
gotHit = true;
HealthControl.HITS += 1;
Destroy(hit.gameObject);
}
}
function Update ()
{
var controller : CharacterController = GetComponent(CharacterController);
// Rotate around y - axis
transform.Rotate(0, Input.GetAxis ("Horizontal") * rotateSpeed, 0);
// Move forward / backward
var forward : Vector3 = transform.TransformDirection(Vector3.forward);
var curSpeed : float = speed * Input.GetAxis ("Vertical");
controller.SimpleMove(forward * curSpeed);
if(Input.GetButtonDown("Fire1"))
{
var bullet = Instantiate(bulletPrefab,
transform.Find("spawnPoint").transform.position,
transform.rotation);
bullet.tag = "playerProjectile";
bullet.rigidbody.AddForce(transform.forward * 6000);
}
}
@script RequireComponent(CharacterController)
I still have no clue what a ‘Fable’ camera is, but decided to make a basic player controller. The character moves with WASD or arrow keys, to turn the character 180 degrees, press Q. The mouse horizontal rotates the character. Check the screenshot for the hierarchy on the gameObjects, follow the instructions on setting up an empty gameObject that will be the camera’s position.
- Create a basic scene, attach the below script to the Player Character
how to set up the CameraHolder object used in the script :
-
set the camera in the desired position
-
create an empty gameObject, name it CameraHolder
-
make the empty CameraHolder a child of the camera
-
set the Transform andd Rotation of CameraHolder to 0 in the Inspector
-
Now drag CameraHolder out of the camera and make it a child of the Player Character
-
CameraHolder is now set in the correct position
Here is the Script :
#pragma strict
public var theCamera : Transform;
private var movementSpeed : float = 5.0;
private var rotationSpeed : float = 5.0;
private var currentRotation : float = 0.0;
private var myTransform : Transform;
private var cameraHolder : Transform;
function Start()
{
myTransform = this.transform;
cameraHolder = transform.Find( "CameraHolder" );
}
function Update()
{
// get inputs
var inputX : float = Input.GetAxis( "Horizontal" );
var inputY : float = Input.GetAxis( "Vertical" );
var inputR : float = Mathf.Clamp( Input.GetAxis( "Mouse X" ), -1.0, 1.0 );
// press 'Q' to turn 180 degrees
if ( Input.GetKeyDown(KeyCode.Q) )
{
currentRotation += 180.0;
}
// get current position and rotation, then do calculations
// position
var moveVectorX : Vector3 = myTransform.forward * inputY;
var moveVectorY : Vector3 = myTransform.right * inputX;
var moveVector : Vector3 = ( moveVectorX + moveVectorY ).normalized * movementSpeed * Time.deltaTime;
// rotation
currentRotation = ClampAngle( currentRotation + ( inputR * rotationSpeed ) );
var rotationAngle : Quaternion = Quaternion.Euler( 0.0, currentRotation, 0.0 );
// update Character position and rotation
myTransform.position = myTransform.position + moveVector;
myTransform.rotation = rotationAngle;
// update Camera position and rotation
theCamera.position = cameraHolder.position;
theCamera.rotation = cameraHolder.rotation;
}
function ClampAngle( theAngle : float ) : float
{
if ( theAngle < -360.0 )
{
theAngle += 360.0;
}
else if ( theAngle > 360.0 )
{
theAngle -= 360.0;
}
return theAngle;
}