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 greatwhiteshark17283 · Jun 08, 2013 at 07:47 PM · c#variablespeedchange

How to adjust the speed of an object?

I got this script that makes it so then a game object travels through certain transforms. I didn't make this and I'm not familiar with C#, so I don't know how to make a variable that adjusts the speed. So, can anybody let me know how I can change the speed with a variable? Here is the script:

 public class randomMovement : MonoBehaviour {
     //Here I am defining a new array called "targets", with an array length of 3.
     //This means that there will be 3 transforms in my array.
     //I can assign these in the inspector window.
     public Transform[] targets = new Transform[3];
     //Here I am defining an int variable which i can use to change the current "targets" transform to use.
     public int curTarget = 0;
  
     void Update() {
        //Check if we are within 1 unit of our target (reduces risk of error - object could miss target which is what causes yours to circle the target)
        if((transform.position - targets[curTarget].transform.position).magnitude < 1f) {
          //When we are within one unit of target, switch to the nex target(or the next transform in the array).
          curTarget++;
          //When our current target reaches the length of the array, we will loop it back to 0 so that it repeats.
          if(curTarget > 2) {
           curTarget = 0;
          }
        }
        //Move forwards
        transform.Translate(Vector3.forward * 3.0f * Time.deltaTime);
        //Always look at the current target
        transform.LookAt(targets[curTarget].position);
     }
 }
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by bigbat · Jun 08, 2013 at 08:09 PM

just add a public var for movement speed (i.e. add this line of code at line 6 of your code : )

 public float MoveSpeed = 3.0f;

and change line 20 to this :

 transform.Translate(Vector3.forward * MoveSpeed * Time.deltaTime);
Comment
Add comment · Show 1 · 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 greatwhiteshark17283 · Jun 08, 2013 at 08:13 PM 0
Share

Thank you so much! The new script works fine!

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

15 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

How do I change my speed based on certain events? 0 Answers

Change output rotation of this Lookat script? 2 Answers

How to Set the Game Speed Based on the Player's Current Score? 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