• 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 Andrew138 · Oct 31, 2013 at 09:37 PM · transform.translatexonly

moving object on x axis without affecting z axis

I have an NPC object in my 2d sidescrolling game. While he is far away from the player, I want him to patrol nearby area, by changing it's x coordinate in a range, but while x coordinate is changing, z coordinate is changing too. Here is the code:

 #pragma strict
 var range : float;
 private var start_x : float;
 
 
 
 function Start () {
     start_x = transform.position.x;
     range = gameObject.GetComponent(NPC_set_attributes).npc_patrolling_range;
 }
 
 function Update () {
 
     transform.Translate(Vector3(gameObject.GetComponent(NPC_set_attributes).npc_move_speed*Time.deltaTime, 0.0, 0.0), Space.Self);
     
     if(transform.position.x >= start_x + range){
         Change_direction();
     } else if(transform.position.x <= start_x-range) {
         Change_direction();
     }
 }
 
 function Change_direction () {
     gameObject.GetComponent(NPC_set_attributes).npc_move_speed = -gameObject.GetComponent(NPC_set_attributes).npc_move_speed;
     gameObject.GetComponent(NPC_set_attributes).npc_facing_left = !gameObject.GetComponent(NPC_set_attributes).npc_facing_left;
 }


Don't know what to do. "Freeze Position Z" won't help, because I need to be able to move my object on z axis too.

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

Answer by aldonaletto · Nov 01, 2013 at 01:12 AM

Space.Self in Translate makes the object move in its local directions - if the object has any rotation, it will move in other axes too. Try Space.World instead: this will Translate only in the X axis with this code. By the way, you could improve performance and write a lot less if the script reference was cached in a variable at Start:

 #pragma strict

 var range : float;
 private var start_x : float;
 private var attribs: NPC_set_attributes; // reference to the NPC_set_attributes script
 
 function Start () {
     start_x = transform.position.x;
     attribs = GetComponent(NPC_set_attributes); // get the script
     range = attribs.npc_patrolling_range;
 }
 
 function Update () {
     transform.Translate(Vector3(attibs.npc_move_speed*Time.deltaTime, 0.0, 0.0), Space.World);
     if(transform.position.x >= start_x + range){
         Change_direction();
     } else if(transform.position.x <= start_x-range) {
         Change_direction();
     }
 }
 
 function Change_direction () {
     attribs.npc_move_speed = -attribs.npc_move_speed;
     attribs.npc_facing_left = !attribs.npc_facing_left;
 }
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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

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

Related Questions

Sun(light) rotation is not working 1 Answer

Add only ONE component at runtime? 2 Answers

Different movement speeds over network 1 Answer

How to keep cameras Vector3.y Position While Translating it. 1 Answer

Alternative to using transform.translate and transform.position for moving objects exact values? 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