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 JulySellanes · Mar 06, 2018 at 01:00 AM · positionvector3ifz axisx

How to detect if transform.position is not vector3 + 100 on x position

Hello, what I want to do is that when the position of the player is equal to the previous position only that in the x I advance 10

This is my script but it doesnt work:

 public GameObject[] posibleIslands;
 public GameObject IslandPrefab;

 public float Range1;
 public float Range2;
 public float Range3;
 public float Range4;
 public float Range5;
 public float Range6;

 private float valueX;
 private float valueY;
 private float valueZ;

 public Vector3 myPosition;
 public Vector3 myOldPosition;
 public Vector3 pos;

 void Update ()
 {
     pos = new Vector3(valueX, valueY, valueZ);

     valueX = Random.Range (Range1, Range2);
     valueY = Random.Range (Range3, Range4);
     valueZ = Random.Range (Range5, Range6);

     myPosition = transform.position;

     if (myPosition == myOldPosition + new Vector3(10, 0, 0) || myPosition == myOldPosition - new Vector3(10, 0, 0) || myPosition == myOldPosition + new Vector3(0, 0, 10) || myPosition == myOldPosition - new Vector3(0, 0, 10))
     {
         for (int i = 0; i < Random.Range (1, 5); i++)
         {
             GenerateIsland ();
             i = 0;
         }

         myOldPosition = myPosition;
     }
 }

 public void GenerateIsland()
 {
     int posibleIslandsInt = Random.Range (0, posibleIslands.Length);

     if (posibleIslandsInt == 0)
     {
         IslandPrefab = posibleIslands [0];
     }

     if (posibleIslandsInt == 1)
     {
         IslandPrefab = posibleIslands [1];
     }

     if (posibleIslandsInt == 2)
     {
         IslandPrefab = posibleIslands [2];
     }

     Instantiate(IslandPrefab, pos, Quaternion.identity);
 }
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 ransomink · Mar 06, 2018 at 11:24 AM

 pos = new Vector3( valueX, valueY, valueZ );

First, you set the pos variable based on your valueX, valueY, and valueZ but they are private (not accessible in the inspector and are changed after setting the variable, so pos will be (0, 0, 0)


 valueX = Random.Range( Range1, Range2 );
 valueY = Random.Range( Range3, Range4 );
 valueZ = Random.Range( Range5, Range6 );

These variables are used on the previous line but set afterwards. The random generated numbers will not be used until the next frame, so this does nothing


 if ( myPosition == myOldPosition + new Vector3( 10, 0, 0 ) )

Do you want to check if your current position is your old position + 10, or do you want to move 10 if the current position is the old position?


 for ( int i = 0; i < Random.Range( 1, 5 ); i++ )
 {
     GenerateIsland();
     i = 0;
 }

This code block doesn't make sense. You're comparing i to a random number every loop so it can skip or repeat i but inside the loop you're resetting i back to 0. this will make i return 0 or 1 and nothing else. What type of behaviour are you trying to achieve?

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

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

Vector3 add or subtract 5 Answers

Vector3 x start position doesn't work properly 1 Answer

NGUI - Vector 3 Position different from position 1 Answer

Finding position of a GameObject using its name? 1 Answer

Vector3 sets to different coordinates than specified 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