x


How would I make my first person controller sprint, when I hold left shift?

I am new to codeing. So I was wondering if somebody could be as kind as to share their script with me.Or give me the link to a script. Preferabley Java script. Thanks in advance?

more ▼

asked Apr 22 '12 at 12:02 PM

Dan290496 gravatar image

Dan290496
15 1 3 3

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

Keep a temporary variable to act as your final speed, and a constant that is your normal speed, along with another variable to act as a multiplier. A simple example:

var NaturalSpeed = 10;
var tempSpeed : float;
var SpeedMultiplier = 1.2; // for 20% speed increase while sprinting

var moveDirection : Vector3; //this determines which direction we should be moving towards

function Update()
{
    moveDirection = Vector3.zero; //this rests our move direction to nothing so we wouldn't move unless movement keys were pressed
    tempSpeed = NaturalSpeed; //this rests our speed back to natural speed

    //this is just a simple movement check for demonstrating sprint; use your own methods of course
    moveDirection.x += Input.GetAxis("Horizontal");
    moveDirection.z += Input.GetAxis("Vertical");

    if(Input.GetKey(KeyCode.LeftShift)) //this checks to see if the player is holding left shift
    {
       tempSpeed *= SpeedMultiplier; //this increases our speed by the multiplier
    }

    transform.Translate(moveDirection.normalized * tempSpeed * Time.deltaTime); //this moves our player to the correct direction at the determined speed (sprinted or not), smoothed over time
}
more ▼

answered Apr 22 '12 at 12:17 PM

asafsitner gravatar image

asafsitner
2.4k 2 8 19

Thank you for sharing this.

Apr 22 '12 at 12:20 PM Dan290496

No need to post a new answer, a comment is enough. :) You might also want to mark the question as answered to take it off the 'questions' list (this can be done by accepting the answer (clicking the V mark under the vote marks))

Apr 22 '12 at 01:13 PM asafsitner

Hey asafsitner your scripts ignores any collision what stands in fps controller way,i mean when i add your script i can walk through any objects....PLEASE FIX THIS.

Jan 04 at 11:38 AM Claymore

Please help,im going to pdate this comment every minute,cause i NEED HELP.

Jan 04 at 11:44 AM Claymore

HELP!!!!!!!!!!!!!!!!!

Jan 04 at 12:41 PM Claymore
(comments are locked)
10|3000 characters needed characters left

Take a course on scripting maybe?

more ▼

answered Mar 02 at 10:23 PM

angstrom gravatar image

angstrom
1

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1361
x1169
x518
x27

asked: Apr 22 '12 at 12:02 PM

Seen: 1580 times

Last Updated: Mar 02 at 10:23 PM