• 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 Dreave · Apr 28, 2012 at 10:46 AM · animationsimplesprint

Simple Sprint?

How would I make my fps controller go from the speed of 10 to about 12 and play a sprint animation on my gun when shift is held down? I have looked around for an answer to this but nothing came up that worked, can anyone help me out please?

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
0
Best Answer

Answer by aldonaletto · Apr 28, 2012 at 06:11 PM

If you're using the First Person Controller prefab, you can find here a script to make it run and crouch. If you're using a custom movement script, however, you should modify the speed when the key is pressed - something like this:

var regSpeed: float = 10.0; var sprintSpeed: float = 12.0;

function Update(){ var speed = regSpeed; if (Input.GetKey("left shift") || Input.GetKey("right shift")){ speed = sprintSpeed; } ... character.Move(moveDir speed Time.deltaTime); } About the animations: you can have an sprint animation and crossfade to it when shift is pressed. Another alternative is to create a hard coded animation, but the result usually is too artificial.

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 fafase · Apr 28, 2012 at 11:40 AM

 if(Input.GetKey("s")&&Input.GetKey("up")){
   Sprint();
   animation.Play("Sprint");}
 else if(Input.GetKey("up"))
   Walk();

 function Sprint(){
   transform.position.x +=12;}
 function Walk(){
   transform.position.x+=10;

}

The first if checks first if sprint button is down, so that if not it jumps right on to the else if (you save some cycles this way) and then walk, if s is down then it checks if up is down(...)and then does the sprint function and the sprint animation.

The function are simplified, they could/should include all directions instead.

Comment
Add comment · Show 6 · 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 Dreave · Apr 28, 2012 at 12:55 PM 0
Share

Ive added this script to my gun but it does not do anything I haven't made the sprint animation yet but it does not speed up my character up can you please help?

avatar image fafase · Apr 28, 2012 at 05:10 PM 0
Share

The script acts on the object it is attached to so attach it to the player, and it will move. Then, the animation is on the gun but I reckon the gun is attached to the player as a child. Create an animation from the animation window, that will show all the children of the object you can animate. Then you are good to go.

avatar image Dreave · Apr 28, 2012 at 05:22 PM 0
Share

I have done all this but still nothing happens, can you explain to me what the very first line means?

avatar image Lo0NuhtiK · Apr 28, 2012 at 05:28 PM 0
Share

if(Input.Get$$anonymous$$ey("s")&&Input.Get$$anonymous$$ey("up")){

That line? ... if(This AND that) ... if both at the same time.

avatar image fafase · Apr 28, 2012 at 05:33 PM 0
Share

Well,

Top level: Player with all component you can see in the inspector

2nd level: the part of the player, in a general way that would be every part of a character, head, arms, trunk, legs, feet, each of them can have a script attached that act only on the specific object.

3rd level: Decomposed once more each of them so feet can have heel, foot, toes. and so on.

in your case:

your player has the gun attached as child and the script is attached to the player so it one level up from the gun, so you need to tell the script "go and look in the children of the player for an object called gun and make it do this." To do so use GetComponentInChildren http://unity3d.com/support/documentation/ScriptReference/Component.GetComponentInChildren.html

I guess your player object has a little arrow next to it in the hierarchy, that is the sign of childed components

Show more comments

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Third Person Help 4 Answers

Reloading Help 2 Answers

Animation Scripting Help 2 Answers

Stop animation after played 30 times? 0 Answers

Character controller problem? 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges