• 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 Trouch · Dec 04, 2009 at 03:05 PM ·

Auto Walking + Speed increase

Hey guys i just need some easy newbie help here,

What part of the Standard First Person Controler ( FPSWALKER ) code do i need to edit to achieve the following.

i want the player to automatically walk in one direction on the X axis ( Left or Right ), and every 10 seconds the walk speed increases.

I would only know how do this in a simple 2D C# code which would be something like

Int Speed = 10 ;

{ Timer (10 seconds)} Speed = Speed + 5; }

Void { Player.Left = Speed; } "

If that makes sense,

Comment
Add comment
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

2 Replies

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

Answer by rocket5tim · Dec 04, 2009 at 09:36 PM

Here's one way to do it using a tweaked coroutine from the Penelope tutorial. Should be fairly easy to convert to C#. And there's probably a smarter way to restart the CheckTime().

edit Here's the code from my original response integrated with FPSWalker.js as requested. Note I think the reason you were having trouble is because I had accidentally put a lower-case "s" in start(). Start() needs to be upper-case d'oh!

@script RequireComponent(CharacterController)

var speed = 6.0; var jumpSpeed = 8.0; var gravity = 20.0; var delay = 10.0; // amount of time to wait before increasing speed

private var timeLeft : float; private var moveDirection = Vector3.zero; private var grounded : boolean = false;

function Start() { timeLeft = delay; CheckTime(); }

function FixedUpdate() { if (grounded) { // We are grounded, so recalculate movedirection directly from axes moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")); moveDirection = transform.TransformDirection(moveDirection); moveDirection *= speed;

     if (Input.GetButton ("Jump")) {
         moveDirection.y = jumpSpeed;
     }
 }

 // Apply gravity
 moveDirection.y -= gravity * Time.deltaTime;

 // Move the controller
 var controller : CharacterController = GetComponent(CharacterController);
 var flags = controller.Move(moveDirection * Time.deltaTime);
 grounded = (flags & CollisionFlags.CollidedBelow) != 0;

}

function CheckTime() { while (timeLeft > 0) { yield WaitForSeconds(1); timeLeft -= 1; Debug.Log(timeLeft); } speed += 5; // increase character controller speed timeLeft = delay; // reset timeLeft to 10 TimerExpired(); }

function TimerExpired() { CheckTime(); // restart the coroutine }

Almost forgot to answer your other question. To make your character walk forward automatically, replace this line in fpswalker.js:

moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));

with something like this:

moveDirection = Vector3(1, 0, 0); // moves character along the x axis
Comment
Add comment · Show 3 · 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 Trouch · Dec 05, 2009 at 02:04 AM 0
Share

Thanks heaps ill try that out

avatar image Trouch · Dec 05, 2009 at 02:19 AM 0
Share

Ok wow i feel like a noob, First off thanks again for helping me out because that code for it to automatically move does work.

but the timer code, I add that as a component but i dont see much of a change in speed, TimeLeft and Delay what do they do?

i would guess timeleft is when the timer resets again and delay would be..the delay

avatar image rocket5tim · Dec 05, 2009 at 03:21 PM 0
Share

Hmmm... If you added the above code to the FPSWalker.js script and still have this line in it: moveDirection *= speed; then it should be increasing that speed variable by 5 every 10 seconds. $$anonymous$$aybe you accidentally have 2 speed variables defined - $$anonymous$$e plus the one that was in the script already (tho that shouldn't be possible). Delay is just the amount of time in seconds that you want to wait before adding 5 to speed. You can check to see if these are behaving correctly by adding Debug.Log(speed); and Debug.Log(timeLeft) at various points in CheckTime() to see that it's working.

avatar image
0

Answer by Trouch · Dec 05, 2009 at 11:48 PM

var speed = 6.0; var jumpSpeed = 8.0; var gravity = 20.0;

private var moveDirection = Vector3.zero ; private var grounded : boolean = false;

function FixedUpdate() { if (grounded) { // We are grounded, so recalculate movedirection directly from axes moveDirection = Vector3(1, 0, 0); // moves character along the x axis

moveDirection = transform.TransformDirection(moveDirection); moveDirection *= speed; // moveDirection + 20;

 if (Input.GetButton ("Jump")) {
     moveDirection.y = jumpSpeed;
 }

}

// Apply gravity moveDirection.y -= gravity * Time.deltaTime;

// Move the controller var controller : CharacterController = GetComponent(CharacterController); var flags = controller.Move(moveDirection * Time.deltaTime); grounded = (flags & CollisionFlags.CollidedBelow) != 0; }

@script RequireComponent(CharacterController)

After reading and understand your code i still dont get it , where does it go in fpswalker ( and i think it does conflict with 'var speed 6;'

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 rocket5tim · Dec 09, 2009 at 09:53 PM 0
Share

Sorry, didn't see your response until just now. I updated my original response with FPSWalker.js as you requested. Have fun!

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How to import the object from server to unity 2 Answers

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

Material doesn't have a color property '_Color' 4 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