- Home /
Animation = Trasnslate Speed
Hello everyone. I'm a Unity n00b and I'm trying to perform a task that sounds really simple but I can't for the life of me figure out how to make it work. I'm making a 2D game where my player, a horse, just stays in one position of the screen and I make the environment move towards the left so it looks like it's scrolling. I applied the following script to a platform which works just fine:
var HorseSpeed : float = 10.0;
function Start () {
}
function Update () {
transform.Translate(-HorseSpeed * Time.deltaTime, 0, 0);
}
But I want to change the float of HorseSpeed depending on the animation that it's doing (e.g. walk, run, etc.). So I thought to make an array and made this script:
var HorseSpeed : float[];
function Start () {
//varied speeds are in ThirdPersonCharacterControl script
HorseSpeed[0] = walkSpeed;
HorseSpeed[1] = trotSpeed;
HorseSpeed[2] = runSpeed;
}
function Update () {
transform.Translate(-HorseSpeed * Time.deltaTime, 0, 0);
}
Then I get error messages saying that my speeds are unknown identifiers and that Operator '-' cannot be used with an expression of type 'float[]'. So I went to adjust the code and came up with this:
var HorseSpeed : float[];
var walkSpeed;
var trotSpeed;
var runSpeed;
function Start () {
HorseSpeed[0] = walkSpeed;
HorseSpeed[1] = trotSpeed;
HorseSpeed[2] = runSpeed;
}
function Update () {
transform.Translate(HorseSpeed * Time.deltaTime, 0, 0);
}
And I get this error message: No appropriate version of 'UnityEngine.Transform.Translate' for the argument list '(System.Array, int, int)' was found.
What am I doing wrong?
Answer by valyard · Nov 09, 2012 at 11:35 PM
HorseSpeed is an array. You need to select an element in it like this:
transform.Translate(HorseSpeed[0] * Time.deltaTime, 0, 0);
If you want to select speed depending on animation type you would write something like this
var speed = 0;
if (animation.IsPlaying("walk")) {
speed = HorseSpeed[0];
} else if (animation.IsPlaying("trot")) {
speed = HorseSpeed[1];
} else if (animation.IsPlaying("run")) {
speed = HorseSpeed[2];
}
if (speed != 0) transform.Translate(speed * Time.deltaTime, 0, 0);
UPDATE:
If you want to use animation object of some other object, in this case your player, add a property
var PlayerAnimation:Animation;
You'll see this property in unity inspector. Drag player gameobject there and its Animation will be linked with this property. After that change animation to PlayerAnimation in the script.
Well I went and inserted the code you recommended. It looks like this now:
var HorseSpeed : float[];
var speed = 0;
function Start () {
}
function Update () {
if (animation.IsPlaying("walk")) {
speed = HorseSpeed[0];
}
else if (animation.IsPlaying("trot")) {
speed = HorseSpeed[1];
}
else if (animation.IsPlaying("run")) {
speed = HorseSpeed[2];
}
if (speed != 0) transform.Translate(speed * Time.deltaTime, 0, 0);
transform.Translate(HorseSpeed[] * Time.deltaTime, 0, 0);
}
But now I'm getting error messages that's being generated from line 38 saying insert ; here, unexpected token 0, etc. Am I still on the wrong track?
what is on line 38? why did you add transform.Translate(HorseSpeed[] Time.deltaTime, 0, 0); which is syntaxically wronf because you don't have a number between []*?
Wow, I did forget to add the number! I should clarify that this script is for a cube that the player is on top of. After putting the number in HorseSpeed[], the script's trying to find the animations for the cube itself but not the player where all the animations are (the Third Person Controller).
Should I add GetComponent(ThirdPersonController) at function Start () or would this not change a thing?
Sorry for taking up your time.
This line is not needed at all: transform.Translate(HorseSpeed[] * Time.deltaTime, 0, 0); The previous line does the job.
Answer by MadVash2005 · Nov 10, 2012 at 12:33 AM
Well I went and inserted the code you recommended. It looks like this now:
var HorseSpeed : float[];
var speed = 0;
function Start () {
}
function Update () {
if (animation.IsPlaying("walk")) {
speed = HorseSpeed[0];
}
else if (animation.IsPlaying("trot")) {
speed = HorseSpeed[1];
}
else if (animation.IsPlaying("run")) {
speed = HorseSpeed[2];
}
if (speed != 0) transform.Translate(speed * Time.deltaTime, 0, 0);
transform.Translate(HorseSpeed[] * Time.deltaTime, 0, 0);
}
But now I'm getting error messages that's being generated from line 38 saying insert ; here, unexpected token 0, etc. Am I still on the wrong track?
Your answer
![](https://koobas.hobune.stream/wayback/20220613083624im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Setting Velocity of Rigidbody to Mouse Speed 1 Answer
Problems with 2d arrays , += incrementing values 0 Answers
2d array.What is the difference between these two? 1 Answer
Problem with rotated colliders 0 Answers
Simple Topdown Movement Problem 1 Answer