- Home /
FPS Controller problems
Read my latest comment for info..
This is the code I have the main section of problem is in the function Update
//Garth's FPS Controller
private var currentSpeed : float;
var walkSpeed : float = 5;
var runSpeed : float = 9;
var gravity : float = 20;
private var sprinting : boolean;
var airControl : boolean = false;
private var playerControl : boolean = false;
var jumpTimer : int;
var limitDiagonalSpeed = true;
private var moveDirection = Vector3.zero;
var jumpSpeed : float = 8;
private var inputX;
private var inputY;
private var inputModifyFactor;
private var myTransform : Transform;
private var falling : boolean = false;
private var fallStartLevel : float;
var fallingDamageThreshold : float = 10.0;
var grounded : boolean = false;
private var controller;
var sprintTimer : float = 0;
var playerName = "New Player";
var jumpLimit : int = 30;
var jumpHold : float = 30;
var jumping : boolean;
function Start () {
controller = GetComponent(CharacterController);
sprinting = false;
currentSpeed = walkSpeed;
myTransform = transform;
sprintTimer = Mathf.Clamp(10,0,10);
jumpTimer = jumpLimit;
jumping = false;
}
function FixedUpdate () {
inputY = Input.GetAxis("Vertical");
inputX = Input.GetAxis("Horizontal");
inputModifyFactor = (inputX != 0.0 && inputY != 0.0 && limitDiagonalSpeed)? .7071 : 1.0;
grounded = (controller.Move(moveDirection * Time.deltaTime) & CollisionFlags.Below) != 0;
if (!Input.GetButton("Jump")) {
jumpTimer++;
jumping = false;
}
else if (jumpTimer >= jumpLimit) {
jumping = true;
moveDirection.y = jumpSpeed;
jumpTimer = 0;
}
if (falling) {
falling = false;
if (myTransform.position.y < fallStartLevel - fallingDamageThreshold)
FallingDamageAlert (fallStartLevel - myTransform.position.y);
}
if (airControl && playerControl) {
moveDirection.x = inputX * currentSpeed * inputModifyFactor;
moveDirection.z = inputY * currentSpeed * inputModifyFactor;
moveDirection = myTransform.TransformDirection(moveDirection);
}
if (!falling) {
falling = true;
fallStartLevel = myTransform.position.y;
}
moveDirection.y -= gravity * Time.deltaTime;
if (sprinting == true) {
sprintTimer -= Time.deltaTime;
}
if (sprinting == false) {
sprintTimer += Time.deltaTime;
}
if (Input.GetButton ("Jump")) {
jumpHold -= 1 * Time.deltaTime;
}
if (jumpHold <= 0 && !jumping) {
jumpHold = 30;
jumpTimer = 30;
}
if (sprintTimer >=10) {
sprintTimer = 10;
}
if (sprintTimer <= 0) {
sprintTimer = 0; //Fix sprint timer..? add a cooldown or something like that
}
}
function Update () {
if (Input.GetAxis ("Vertical") > 0) { //Forward
transform.Translate(Vector3(0,inputY,currentSpeed) * Time.deltaTime);
}
if (Input.GetAxis ("Vertical") < 0) { //Backward
transform.Translate (Vector3(0,-inputY,currentSpeed) * Time.deltaTime);
}
if (Input.GetAxis ("Horizontal") > 0) { //Right
transform.Translate (Vector3 (0,0,0) * Time.deltaTime);
}
if (Input.GetAxis ("Horizontal") < 0) { //Left
transform.Translate (Vector3 (0,0,0) * Time.deltaTime);
}
if (Input.GetButton ("Run") && Input.GetButton ("Vertical") && sprintTimer >= 1 && grounded == true){
currentSpeed = runSpeed;
sprinting = true;
}
else {
currentSpeed = walkSpeed;
sprinting = false;
}
if (Input.GetButton ("Jump")) {
jumpHold -= 1 * Time.deltaTime;
}
if (jumpHold <= 0 && !jumping) {
jumpHold = 30;
jumpTimer = 30;
}
if (sprintTimer <= 0) {
currentSpeed = walkSpeed;
sprinting = false;
sprintTimer = 0;
}
//Printing Section
//print ("Speed = " + currentSpeed);
//print ("Players Name = " + playerName);
//print ("Sprinting = " + sprinting);
//print ("Jumping = " + jumping);
}
function FallingDamageAlert (fallDistance : float) {
print ("Ouch! Fell " + fallDistance + " units!");
//Add sounds effects! + Health System
}
@script RequireComponent(CharacterController)
Any suggestions on what I could change to make the script better would be appreciated aswell
(Code updated to match question in the latest comment i made)
Seems like its related to inputX and inputY - in your global section you define them without a type, in FixedUpdate they are defined as Axis but that's only within the context of FixedUpdate.
(this is what i changed first off)if I change them in global section to be defined as the same as in fixed update it just comes up with this error
Assets/Scripts/FPS Controller.js(79,53): BCE0022: Cannot convert 'float' to 'UnityEngine.Vector3'.
Assets/Scripts/FPS Controller.js(83,52): BCE0022: Cannot convert 'float' to 'UnityEngine.Vector3'.
That's part of the reason I didn't have it defined.. Is there perhaps a way I can define it as a vector3 and still have the part in the fixed update still work? If anyone has a fix for this it would be greatly appreciated.
var inputX; var inputY;
Declare you variables fully. casting is including what type they are.
so should i place the same = to the global variable that i have in my fixed update? and then perhaps remove the = in fixed update? (cant test right now, away from laptop for a bit)
transform.position seems to just change my position so i changed it to transform.translate (vector3(0,inputY,currentSpeed) * Time.deltaTime); Only thing is that I havnt got any idea of how to get the positive/negate keys to work, nor how to get horizontal to work :P but atleast I have solved my main issue... Never$$anonymous$$d i caused another error saying that i cant use the inputX and inputY because it will only call axis in start or awake function.. i have no idea now and if anyone can just make one that works with my script i would be extremely grateful.. Ah the joys of learing, everything i have made has come up with atleast 4 errors..
Have now changed it so it is Input.GetAxis ("Verticals") > 0 so i can get positive/negative inputs. what do i add to the translations to get them to translate how i want them to? i have a working forward motion but cant figure out how to do anything else
Answer by -GenericPath- · Sep 03, 2013 at 06:27 AM
I have finally fixed all of the errors in my script! this was the fix for the movement:
function Update () {
if (Input.GetAxis ("Vertical") > 0) { //Forward
transform.Translate(Vector3.forward * currentSpeed * Time.deltaTime);
}
if (Input.GetAxis ("Vertical") < 0) { //Backward
transform.Translate (Vector3.back * currentSpeed * Time.deltaTime);
}
if (Input.GetAxis ("Horizontal") > 0) { //Right
transform.Translate (Vector3.right * currentSpeed * Time.deltaTime);
}
if (Input.GetAxis ("Horizontal") < 0) { //Left
transform.Translate (Vector3.left * currentSpeed * Time.deltaTime);
}
other fixes i used where not defining variables multiple times and alot of changing what my actual script was... (changing transform.position to transform.translate)
Answer by TrickyHandz · Sep 01, 2013 at 02:57 AM
You are defining variables of the same name twice. This is very bad as it leads to confusion for the processor, rather than changing your global variable, remove the declaration in FixedUpdate() and just have the assignment. So, your FixedUpdate() should start like this:
function FixedUpdate () {
inputX = Input.GetAxis("Horizontal");
inputY = Input.GetAxis("Vertical");
(this is now the error i am getting)
changing it to that just causes another error
InvalidCastException: Cannot cast from source type to destination type. FPS Controller.Update () (at Assets/Scripts/FPS Controller.js:79)
&
InvalidCastException: Cannot cast from source type to destination type. FPS Controller.Update () (at Assets/Scripts/FPS Controller.js:83)
as far as I can work out I've got something set-up wrong in my update with the transform positions, that was the inital question I had and it seems to be the main cause of problems...
Answer by TrickyHandz · Sep 01, 2013 at 02:57 AM
You are defining variables of the same name twice. This is very bad as it leads to confusion for the processor, rather than changing your global variable, remove the declaration in FixedUpdate() and just have the assignment. So, your FixedUpdate() should start like this:
function FixedUpdate () {
inputX = Input.GetAxis("Horizontal");
inputY = Input.GetAxis("Vertical");
Answer by citizen_rafiq · Sep 01, 2013 at 03:38 AM
if you want to use a new button go to InputManager ->Axes , create it and Name it Run. so Chang the name of button Run to Fire1 or create it.
if (Input.GetButton ("Fire1") && Input.GetButton ("Vertical") && sprintTimer >= 1){
that doesn't really have anything to do with my question and I already know how to do that..
Your answer
Follow this Question
Related Questions
Block Fps move 1 Answer
Set Max Rotation On Weapon Sway 0 Answers
What's wrong with this JavaScript door script? 2 Answers
How to turn two scripts on/off with a button 1 Answer
Rotate character to the moving direction problems? 2 Answers