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 MadVash2005 · Nov 09, 2012 at 11:03 PM · 2darrayplatformscroll

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?

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

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 MadVash2005 · Nov 10, 2012 at 12:05 AM 0
Share

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?

avatar image valyard · Nov 10, 2012 at 12:11 AM 0
Share

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 []*?

avatar image MadVash2005 · Nov 10, 2012 at 12:24 AM 0
Share

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.

avatar image valyard · Nov 10, 2012 at 12:39 AM 0
Share

This line is not needed at all: transform.Translate(HorseSpeed[] * Time.deltaTime, 0, 0); The previous line does the job.

avatar image valyard · Nov 10, 2012 at 12:43 AM 0
Share

Updated the answer.

Show more comments
avatar image
0

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?

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

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

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


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