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 PrimeDerektive · Aug 06, 2010 at 03:58 PM · charactercontrollerspeedfpswalker

How to add diving/rolling to FPSWalker script? (Not animation, just speed)

I dunno why I can't figure this out, but its really frustrating me. I am trying to add a feature to the FPSWalker script so that if the player taps the spacebar key while moving, he will "dive" or "roll" in that direction, momentarily increasing his speed in that direction for a brief moment.

How would I go about doing this? I tried doing something similar to the stock jump code in the FPSWalker script, and tried starting a timer sort of like the bullet reset from the machine gun/ rocket launcher from the rocket launcher/machinegun script from the FPS tut, and applying the speed over that time, but I can't get it to do anything.

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

Answer by Heratitan · Aug 06, 2010 at 05:05 PM

You could try doing it almost completely the same way as jumping by changing the axis on which you jump to the z axis, so that you jump forward. You could then increase the speed of the dive so that it is faster than the walking speed. You would also have to adjust the gravity so that you stopped after a few feet.

For Example:

var speed = 6.0; var diveSpeed = 40.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 = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")); moveDirection = transform.TransformDirection(moveDirection); moveDirection *= speed;

     if (Input.GetButton ("Jump")) {
         moveDirection.z = diveSpeed;
     }
 }

 // 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;

}

This is the FPS Walker from the FPS Tutorial I think, except with the dive. Hope That helps.

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 PrimeDerektive · Aug 06, 2010 at 06:30 PM 0
Share

This is what I tried, but unlike jumping on the y axis (because gravity stops the speed increase), jumping on the z axis will just jump indefinitely, like a run mode. What do you mean when you say "adjust the gravity so you stopped after a few feet"? How would I go about doing that?

avatar image
1

Answer by PrimeDerektive · Aug 06, 2010 at 06:58 PM

Not sure if its poor form to answer your own question, but I think I figured this out.

I wanted to be able to dive in any direction, so I created a coroutine called DiveCheck(), that checks for a press of the jump button, and if pressed, changes walkSpeed to diveSpeed, waits for .1 second, and changes speed back to its original value (so its only a quick burst). Then I do another WaitForSeconds(0.5), so you can't spam the dive key.

Heres my code (i took out any vertical jumping code, because my game does not allow jumping):

var speed = 6.0; var diveSpeed = 20.0;

private var moveDirection = Vector3.zero;

function Start(){ DiveCheck(); }

function FixedUpdate() {

 //Calculate movedirection directly from axes
 moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
 moveDirection = transform.TransformDirection(moveDirection);
 moveDirection *= speed;

 // Move the controller
 var controller : CharacterController = GetComponent(CharacterController);

controller.Move(moveDirection * Time.deltaTime); }

function DiveCheck(){ while(true){

     var initialSpeed = speed;

     if(Input.GetButtonUp("Jump")){
         speed = diveSpeed;
         yield WaitForSeconds(0.1);
         speed = initialSpeed;
         yield WaitForSeconds(0.5);
     }
     yield;  
 }

}

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 PrimeDerektive · Aug 06, 2010 at 06:59 PM 0
Share

Is it just me, or is figuring stuff out on your own in Unity one of the most gratifying things in the world? :)

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

No one has followed this question yet.

Related Questions

Run speed in world-based character controller 0 Answers

A problem with character speed 1 Answer

Change speed of character using gameobject.active at runtime? 1 Answer

Adjust turning speed for 3rd Person Character? 1 Answer

Altering Character Motor max speeds from script 0 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