• 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 SebGM2019 · Sep 22, 2018 at 03:35 AM · scripting problemgamedesign

if position not working

Hi, I have a problem with checking if my gameobject is on a position, let me explain: I'm making a game with a kind of "Slider", like the Unity UI Slider, (But not using the slider, it's just to compare), this Slider was made with GameObjects. The cursor was designed to move automatically and you press the screen when the cursor is in a specific zone. This is the cursor code:

 private System.Random FirstRnd = new System.Random();
     private int FirstCounter;
     private float Speed = 50f;
     private float AntiSpeed = -50f;
     private int SecondCounter;
     private System.Random SecondRnd = new System.Random();
     private Vector3 StartLVector = new Vector3(-2.05f, -0.55f, 10f);
     private Vector3 StartRVector = new Vector2(2.05f, -0.55f);
     private Vector3 CenterL = new Vector3(-0.02f, 0.55f, 10f);
     private Vector3 CenterR = new Vector3(1.14f, -0.55f, 10f);
     private GameObject Cursor;
 
     // Use this for initialization
     void Start ()
     {
         Cursor = GameObject.Find("Cursor");
     }
     private void FixedUpdate()
     {
         SecondStarting();
     }
     void Starting()
     {
         FirstCounter = FirstRnd.Next(1, 3);
         if (FirstCounter == 1)
         {
             transform.position = StartLVector;
         }
         else if (FirstCounter == 2)
         {
             transform.position = StartRVector;
         }
     }
     void SecondStarting()
     {
         Debug.Log("Puga");
         if (Cursor.transform.position == StartLVector)
         {
             Cursor.transform.Translate(Speed, 0, 0);
         }
         else if (Cursor.transform.position == StartRVector)
         {
             Cursor.transform.Translate(AntiSpeed, 0, 0);
         }
         else if (Cursor.transform.position == CenterL)
         {
             SecondCounter = SecondRnd.Next(1, 3);
             if (SecondCounter == 1)
             {
                 Cursor.transform.Translate(Speed, 0, 0);
             }
             else if (SecondCounter == 2)
             {
                 Cursor.transform.Translate(AntiSpeed, 0, 0);
             }
         }
         else if (Cursor.transform.position == CenterR)
         {
             SecondCounter = SecondRnd.Next(1, 3);
             if (SecondCounter == 1)
             {
                 Cursor.transform.Translate(Speed, 0, 0);
             }
             else if (SecondCounter == 2)
             {
                 Cursor.transform.Translate(AntiSpeed, 0, 0);
             }
         }
     }
 }


As far I can see, I don't detect a problem here. Do you see something?

Comment
Add comment · Show 2
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 SebGM2019 · Sep 22, 2018 at 03:38 AM 0
Share

I want to add something, the message "Puga" yes prints, but, below it, nothing seems to compile

avatar image Seth-Bergman · Sep 22, 2018 at 08:02 AM 0
Share

Have you added debug logs to each of your loops to see what’s getting called when? Good place to start.. does it move at all? What happens? Offhand, you tell the object to translate if it has a certain position, that will only happen for one frame before it is no longer true.. also, you repeat a block of code, that should be a separate method you call twice for more manageable code.. overall, I still can’t quite put together how this is meant to work though..

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by LCStark · Sep 22, 2018 at 07:58 AM

You are comparing Cursor.transform.position to some values and none of them match. Try displaying the Cursor.transform.position in the Debug.Log and see what value it really has. You probably will want to use transform.localPosition instead.

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

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

Trying to disable the Script below on trigger or the closest thing 0 Answers

MonoDevelop won't open on my mac 0 Answers

How to sync multiple animations on a sprite? 0 Answers

CollectPapers script doesn't work in Unity5 0 Answers

Proper item usage design 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges