- Home /
Third Person Help
could anyone start me of with a simple script which would play an animation and make my character move forward when I press w? I don't want anyone to write a massive script which would take ages, I just want something simple. Or could anyone tell me of a good tutorial which would show me how to, ive tried the tut that unity provides but I cant get it to work.
Answer by asafsitner · Sep 23, 2011 at 05:28 PM
If you want something to happen when you press a key you just need to check if that key was pressed and then just do whatever it is you want to do. For example, to play an animation and move your character forward when you press the 'w' key, you would write something like this (c#):
using UnityEngine;
public class Move : MonoBehaviour
{
/*This will determine how fast the character will move, not how fast the animation plays,
* You'll have to make sure it matches your animation manually
* Either by changing the animation clip itself, or by adjusting the animation's speed property
* e.g: animation["MyAnimationName"].speed = MyValue;
*/
public float WalkSpeed = 5.0f;
// Update is called once per frame
void Update ()
{
/*check if we pressed the desired key (will continue to return 'true' while the key is pressed
* This is different than Input.GetKeyDown("w") which will only return true once, when the key was actually pressed
*/
if (Input.GetKey("w"))
{
/*Call the animation to play by name. This will crossfade between animations so you'll get smooth
* transition between animations and won't 'snap'
*/
animation.CrossFade("Walk");
/*This will move the character forward by the speed value. However, this will move the character
* 5 units every frame, which is far too fast. To tone things down we multiply it by Time.deltaTime,
* or the time since the last frame to create smoother and slower movement
*/
transform.Translate(Vector3.forward * WalkSpeed * Time.deltaTime);
}
else
{
//if we're not moving we'll want to crossfade back to the idle animation
animation.CrossFade("Idle");
}
}
}
Shouldn't really be this long, if you'll skip all the green text :)
your script works perfectly, but at the moment the animations only play once, whereas I want them to loop, any ideas?
You have to set the loop type manually per animation, upon importing. Change the 'wrap mode' to 'loop' ins$$anonymous$$d of 'default' or 'once'. You can change that in script too, if you access the ' animation["animationName"].wrap$$anonymous$$ode ' property.
Useful resources on that:
http://unity3d.com/support/documentation/Components/animeditor-AnimationCurves.html
http://unity3d.com/support/documentation/ScriptReference/Animation-wrap$$anonymous$$ode.html
This is great thanks so much, 1 more thing how could i get it, so when i press s it makes my character go backwards?
In that case you'll want to do things a little differently. You'll want to query the axis input rather than a single keystroke. That will give you more versatility and reusable code, as well as easily changeable control schematics. Axes can be defined in the Input $$anonymous$$anager (go to menu Edit -> Project Settings -> Input) where you can add and tweak them. You then refer to them in script by name.
An axis have a positive and negative keys (not necessarily keyboard keys or mouse buttons, it could be any kind of input really but I'll stick to keyboard for simplicity's sake) which, when pressed will return a value between 1 and -1 respectively. No input will return 0.
$$anonymous$$nowing this, it will be very simple to change the above script to achieve your desired result. First, you'll want to cache the user's input into a variable to make it easier to use later. Then you'll change the if query to check the variable ins$$anonymous$$d of direct input, and last you'll add that variable to the Translate method. The changes should look like this:
float verticalAxisInput = Input.GetAxis("Vertical");
if (verticalAxisInput != 0)
transform.Translate(Vector3.forward verticalAxisInput WalkSpeed * Time.deltaTime);
For additional reading: http://unity3d.com/support/documentation/ScriptReference/Input.html
Thankyou for helping me with my character's animations, they are just about done now, thanks again.
Answer by Meltdown · Sep 23, 2011 at 04:32 PM
Hi,
This will be exactly what you need to get started. http://unity3d.com/support/resources/tutorials/3d-platform-game.html
Answer by goosoodude · Sep 23, 2011 at 07:16 PM
Did you ever think of adding a third person controller?
This is, essentially, part of the controller. I'd rather write my own controller than use a premade one, as they never fit exactly to what I need.
Your answer
Follow this Question
Related Questions
Blender animation 1 Answer
adding animation dynamically by script 0 Answers
Loop Animation Help 1 Answer
Animation Scripting Help 1 Answer