- Home /
Character Controls like the game "Fable"
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)
What is the question exactly? What is wrong with this script, what do you want it to do? (some of us have never played 'Fable' )
sorry about that, im not quite sure how to explain the controls for fable. it is a third person RPG and i just wish to have better 3rd person Character controls than the default ones on unity. i would just like much more control over how i move.
i'm sorry if this isn't very helpful :/
Nope, not helpful. What specifically is it about the default character controller that is lacking and that you want to improve?
i'd like to be able to jump run walk i want the character to FACE the direction im going and have to camera turn the direction he is facing. as well as have the ability to use two different type of weapons or powers. and have left click primary right click secondary. that my not be exactly like fable but i havent played it on pc in years. i just want more control with my character and not just A and D rotates left and W forward S backwards. if i press S i want him to face backwards and go that way, not just transform backwards. i am not sure how much help this is ...
That's another swing-and-a-miss ! This kind of vague question usually gets left unanswered, but I'm feeling helpful and have written an answer that may help =]
Answer by AlucardJay · Sep 17, 2012 at 03:34 AM
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;
}

Hello. Did this script work, or did you find the suggestion useful? Please post a comment or mark an answer, for future reference by other people searching this 'site.
This is the usual answer for questions like this, so I think this script is a bonus : http://answers.unity3d.com/questions/309842/dungeon-defenders-camera-and-control.html
@jay $$anonymous$$ay thanks, I have tried this but 1) i get the error at the bottom. 2) the capsule doesnt move with the camera
NullReferenceException UnityEngine.Transform.get_position () (at C:/BuildAgent/work/d9c061b1c154f5ae/Runtime/ExportGenerated/Editor/UnityEngineTransform.cs:19) NewChar.Update () (at Assets/$$anonymous$$yStuff/1$$anonymous$$yScripts/NewChar.js:46)
The script goes on the player. $$anonymous$$ake sure you have dropped the camera into theCamera in the inspector.
I have made a package you can import and check how I have set it up : http://www.alucardj.net16.net/unityanswers/FableChar.unitypackage
In Unity, browse to Assets > Import Package > Custom Package , then locate where you downloaded the package.
Change the Update to this :
function Update()
{
// get inputs
var inputX : float = Input.GetAxis( "Horizontal" );
var inputY : float = Input.GetAxis( "Vertical" );
// get current position, then do calculations
var moveVectorX : Vector3 = theCamera.forward * inputY;
var moveVectorY : Vector3 = theCamera.right * inputX;
var moveVector : Vector3 = ( moveVectorX + moveVectorY ).normalized * movementSpeed * Time.deltaTime;
// update Character position
myTransform.position = myTransform.position + Vector3( moveVector.x, 0.0, moveVector.z );
// and rotation
myTransform.LookAt( myTransform.position + Vector3( moveVector.x, 0.0, moveVector.z ) );
// Firing
if ( Input.GetButtonDown("Fire1") )
{
var bullet = Instantiate( bulletPrefab, transform.Find("spawnPoint").transform.position, transform.rotation );
bullet.tag = "playerProjectile";
bullet.rigidbody.AddForce( transform.forward * 6000 );
}
}
remove : @script RequireComponent(CharacterController)
in if (dead) , you now have a reference to the main camera, so change that line to :
theCamera.position = Vector3(-40,4,0);
But this camera part may be overriden by the commands driving the camera. And the tumbling part I am worried about the char breaking. If you store the rotation before tumbling, then when respawning just use that rotation and give it back to the char.
Oops, don't forget to add this :
public var theCamera : Transform;
public var movementSpeed : float = 8.0;
private var myTransform : Transform;
function Start()
{
myTransform = this.transform;
}
function ClampAngle( theAngle : float ) : float
{
if ( theAngle < -360.0 )
{
theAngle += 360.0;
}
else if ( theAngle > 360.0 )
{
theAngle -= 360.0;
}
return theAngle;
}
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Mech Movement/Camera Help 0 Answers
Toggling a basic graphical GUI overlay using the escape key. 3 Answers
iPhone joystick? 0 Answers
Jump Speed 1 Answer