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

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

if(transform.Rotate) Doesn't Work? 4 Answers

[Javascript] Check X and Z coordinates for an instance of a certain object 0 Answers

Only one animation playing NON-STOP!!? 2 Answers

How do I create a tool similiar to the object movement gizmo in unity 2 Answers

How do I get the edge of an object from transform.position? 2 Answers


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