- Home /
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?
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.
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.
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?
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.
I have done all this but still nothing happens, can you explain to me what the very first line means?
if(Input.Get$$anonymous$$ey("s")&&Input.Get$$anonymous$$ey("up")){
That line? ... if(This AND that) ... if both at the same time.
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
Your answer
Follow this Question
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