Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by -GenericPath- · Aug 31, 2013 at 02:24 PM · javascriptmovementfpsfps-controller

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)

Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image getyour411 · Sep 01, 2013 at 12:05 AM 0
Share

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.

avatar image -GenericPath- · Sep 01, 2013 at 12:50 AM 0
Share

(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.

avatar image meat5000 ♦ · Sep 01, 2013 at 08:22 AM 0
Share

var inputX; var inputY;

Declare you variables fully. casting is including what type they are.

avatar image -GenericPath- · Sep 01, 2013 at 08:45 AM 0
Share

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)

avatar image -GenericPath- · Sep 02, 2013 at 06:43 AM 0
Share

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

4 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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");


Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image -GenericPath- · Sep 01, 2013 at 04:06 AM 0
Share

(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...

avatar image
1

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");


Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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){

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image -GenericPath- · Sep 01, 2013 at 04:12 AM 0
Share

that doesn't really have anything to do with my question and I already know how to do that..

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges