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

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

what's wrong with this script? 2 Answers

Perform Action on frame/time of animation 1 Answer

adding animation dynamically by script 0 Answers

Animation Script Help 0 Answers

How to keep an animation attached to gameobject 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