Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 miwwes · Sep 26, 2017 at 03:04 AM · vector3transform.position

How to change transform.position.x to go backwards at specific instance?

Hi,

I'm having some trouble with moving my character. I'm trying to get it to go in the negative x direction when the character passes a certain x position. Any help?

public class Movement : MonoBehaviour {

 public float moveSpeed;
 // Use this for initialization
 void Start () {
     moveSpeed = 0.8f;
 }
 
 // Update is called once per frame
 void Update () {
     transform.position += new Vector3(.0001f, 0, 0);

     if ((transform.position.x) > 0.005 && transform.position.x < .01) {
         transform.position -= new Vector3(.0001f, 0, 0);
     }


 }

}

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

2 Replies

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

Answer by tmalhassan · Sep 26, 2017 at 03:46 AM

Hello @miwwes.

The problem with your code is that you're telling the object to move right and after that you're immediately telling it to go left. That's is why it seems stuck at 0.0049. You can fix this by manipulating the situation and changing the if statement.

Try changing your code to something like this:

 public float moveSpeed;

 private bool moveRight = true;

 void Start()
 {
     moveSpeed = 0.8f;
 }
 
 void Update()
 {
     if (moveRight == true)
     {
         transform.position += new Vector3(0.0001f, 0, 0);

         if ((transform.position.x) >= 0.01f)
         {
             moveRight = false;
         }
     }
     else
     {
         transform.position -= new Vector3(0.0001f, 0, 0);

         if ((transform.position.x) <= 0)
         {
             moveRight = true;
         }
     }
 }

Let me know how it goes and all the best :)

Comment
Add comment · Show 9 · 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 miwwes · Sep 26, 2017 at 02:47 PM 0
Share

$$anonymous$$y object got stuck at 0.4999 and that happened before as well on my code. Any thoughts?

avatar image tmalhassan miwwes · Sep 26, 2017 at 02:56 PM 0
Share

i just updated the code. try now...

avatar image miwwes · Sep 26, 2017 at 04:36 PM 0
Share

It worked the very first time I implemented it! However, after that, it got stuck again. Between 0.00499 and 0.005. Even if I were to change it to make it change at a later x position it would be get stuck. Thank you so much for helping me!

avatar image tmalhassan miwwes · Sep 26, 2017 at 05:01 PM 0
Share

Oh, so you want it to keep on moving back and forth?

avatar image miwwes tmalhassan · Sep 26, 2017 at 05:03 PM 0
Share

Yes, but with that code, it moved right, then moved left on the first run. After the first run, it went right and stopped. Regardless, It should be going right then left after every time i first run the program right?

Show more comments
avatar image
0

Answer by irfan-ayub · Sep 26, 2017 at 05:55 AM

If you want to access a particular axis in any transform component you can always reference it separately.

you can do it like this:

  public float moveSpeed;
  // Use this for initialization
  void Start () {
      moveSpeed = 0.8f;
  }
  
  // Update is called once per frame
  void Update () {
 
      transform.position.x += 0.0001f;
 
      if ((transform.position.x) > 0.005f && transform.position.x < 0.01f) {
          transform.position.x -= 0.0001f;
      }
  }


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 miwwes · Sep 26, 2017 at 02:46 PM 0
Share

I tried this and got an error telling me I could not modify transform.position's return value alt text

screen-shot-2017-09-26-at-104633-am.png (148.7 kB)

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

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

Related Questions

Why is my y-axis bugging? 1 Answer

How do I change one value of a vector? 2 Answers

Nav Mesh and transform.position failure! 0 Answers

Move camera when mouse is near the edges of the screen 1 Answer

Trouble converting transform.position to C# 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