My Learning
Pathways
Guided learning journeys
Embark on a guided experience where you unlock free assets, prepare to get Unity Certified, and earn shareable badges to demonstrate your learning to future employers.
Unity Essentials
Pathway
Foundational
+600 XP
2 Weeks
Designed for anyone new to Unity, this guided learning journey is your first step toward gaining the background, context, and skills you need to confidently create in the Unity Editor and bring your vision to life. Experience hands-on learning as you discover what’s possible with Unity and unlock free assets to support you in creating your best projects. Completing this Pathway will equip you with the foundation you need to further your learning and specialize in your area of interest.  
Junior Programmer
Pathway
Beginner
+3000 XP
12 Weeks
Welcome to Junior Programmer! Designed for anyone interested in learning to code or obtaining an entry-level Unity role, this pathway assumes a basic knowledge of Unity and has no math prerequisites. Junior Programmer prepares you to get Unity Certified so that you can demonstrate your job-readiness to employers.
Browse
TOPICS
Editor EssentialsScriptingGraphics & Visual Effects2DMobile & TouchXRPhysicsUser InterfaceFor EducatorsAI & NavigationMore
Pathway
Mission
Course
Project
Tutorial
Guided learning experiences
Gain the Unity skills you need to advance your goals and bring your vision to life.
Unity Essentials
Pathway
Foundational
+600 XP
2 Weeks
Designed for anyone new to Unity, this guided learning journey is your first step toward gaining the background, context, and skills you need to confidently create in the Unity Editor and bring your vision to life. Experience hands-on learning as you discover what’s possible with Unity and unlock free assets to support you in creating your best projects. Completing this Pathway will equip you with the foundation you need to further your learning and specialize in your area of interest.  
Junior Programmer
Pathway
Beginner
+3000 XP
12 Weeks
Welcome to Junior Programmer! Designed for anyone interested in learning to code or obtaining an entry-level Unity role, this pathway assumes a basic knowledge of Unity and has no math prerequisites. Junior Programmer prepares you to get Unity Certified so that you can demonstrate your job-readiness to employers.
Live
TOPICS
Browse All Topics
ALL
TUTORIALS
PROJECTS
COURSES
MISSIONS
PATHWAYS
See all 0 Results
FAQ
Create a Unity ID
Sign in
Mike Breidegam
Apr 10, 2020
Anyone able to get their animals to move at random speeds? I spent a lot of time and finally got them to sort of change speeds while running across the screen but not appear and run a random constant speeds.

Here is my code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AnimalMoveFwd : MonoBehaviour
{

public int [] speed;

// Start is called before the first frame update
void Start()
{

}

// Update is called once per frame
void Update()
{
int speedIndex = Random.Range(0, speed.Length);
transform.Translate(Vector3.forward * Time.deltaTime * speed[speedIndex]);


}
}

So the speed array I add multiple numbers for each animal. So I may set speed value to 3 and say have values for each field by 5, 10 and 15. What seems to happen is the animal toggles between speeds whilst running. A random speed value may generate (I believe), but doesn't state constant. For example it may start at 10 and then speed up to 15 and then slow down to 5 instead of staying at 10.

I think I'm closer than I originally was, when I tried to just use a Random.Range without any array.

Any help is appreciated. Online searches are giving more complex coding examples.

Thanks

Comments
CC
Chuck Chuckerson
a year ago
yes its local vs global variable. If you declare before the Start block, then it will be global. We will learn more about this in the course.
0
Mike Breidegam
a year ago
Composer
Chuck Chuckersonbefore the Start block, type int speed;
Thank you so much. I think I get it now. You don't have to set a value for the Variable, you just create it. Then the variable value can be set in the Start or Update method depending on how you want to use it (without using the int or float in front). Now that I see it I get it. This was driving me mad.
0
CC
Chuck Chuckerson
a year ago
using System.Collections; using System.Collections.Generic; using UnityEngine; public class AnimalMoveFwd : MonoBehaviour { public int speed;
// Start is called before the first frame update void Start() { speed = Random.Range(3, 15); } // Update is called once per frame void Update() {
transform.Translate(Vector3.forward * Time.deltaTime * speed); } }
1
CC
Chuck Chuckerson
a year ago
Mike BreidegamI get message in Visual Studio for the translate method "The name 'speed' does not exist in the current context.
before the Start block, type int speed;
0
Mike Breidegam
a year ago
Composer
Chuck Chuckersonthen in the start block we want speed = Random.Range(3, 15);
I get message in Visual Studio for the translate method "The name 'speed' does not exist in the current context.
0
Load More
Copyright © 2021 Unity Technologies
Legal
Privacy Policy
Cookies
Cookies Settings
Language:
English
简体中文
한국어
日本語
Русский
Español
Français
Deutsch
Português