• 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
Question by GTS925 · Jan 07, 2011 at 08:36 AM · transformobjectmoveleftright

How do you make an object go left and right?

T$$anonymous$$s is my script, it only goes left. How can I make it go left, then right?

void Update() {

     t$$anonymous$$s.transform.position += new Vector3(-1.0f, 0.0f, 0.0f) * Speed;


     if (t$$anonymous$$s.AIShoot == true)
     {
         Instantiate(Ballistic, t$$anonymous$$s.transform.position, new Quaternion());
         t$$anonymous$$s.AIShoot = false;
         AudioSource.PlayClipAtPoint(Sound, new Vector3(12.50f, 30.0f, 12.50f), 10.0f);
     }

     else
     {
     }

}

Comment

People who like this

0 Show 0
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

Answer by Graham-Dunnett · Jan 07, 2011 at 12:02 PM

Your object is moving left because you have a vector (-1,0,0) in your script. To make the object move right you'd use a vector (1.0f, 0.0f, 0.0f).

To make the object move left and then right, you need to decide when to make the switch. That could either be when the x value of the position is less than a certain value:

if (t$$anonymous$$s.transform.position.x < 10.0f) {
    // move right
} else {
    // move left
}

or based on time, in w$$anonymous$$ch case you'd use Time.time.

However, typically in a game you want your objects to be moving and changing direction all the time. With t$$anonymous$$s more complex requirement you are really better off using a simple state mac$$anonymous$$ne to control your object. The state could be a simple variable that takes on one of 3 values (0, 1 or 2) w$$anonymous$$ch mean the object is not moving, moving left, or moving right:

var state = 0;

void Update() { if (state == 2) { // move right } else if (state == 1) { // move left } else { // state is zero // don't move } }

T$$anonymous$$s code just controls how the object moves. You can now have other code that sets the value of state, to control how the object moves. You could for example have an OnGUI function that has three buttons you can press to control w$$anonymous$$ch direction your object moves in. In your real game, however, you'd compute the state based on lots of t$$anonymous$$ngs including time, where the object is, where an AI system wants the object to go to, or what keys, mouse or touch the player makes.

Note for the more advanced programmer, you'd probably want to use enums, as per http://stackoverflow.com/questions/287903/enums-in-javascript to represent the state of the object.

Comment
Kuba
shaqpesigan1

People who like this

2 Show 0 · 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

how to check if the target is to the left or the right? 3 Answers

Opening a door at proximity 2 Answers

How to Make an Endless Runner Move from track to track smoothly 0 Answers

Controlling if object is moving right or left 0 Answers

using Transform on a Object will not move it 100% exactly... 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