Search Unity

How to find the x value of a tagged object in 2d mode

Discussion in '2D' started by CrapyComputer, Mar 28, 2015.

  1. CrapyComputer

    CrapyComputer

    Joined:
    Dec 13, 2014
    Posts:
    3
    I'm a teenage developer and I have never really coded in my life so I'm relying on video tutorials on how to use unity. What I want to be able to do is when I click a button the object move to the left or right of the screen. My solution to this is to create an if statement so that if the integer ''xvalue' is more than 0 then the object moves to the left else it moves to the right. This is my script so far, feel free to tell me if I'm doing anything wrong:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class lineswich : MonoBehaviour
    5. {
    6.     int linepos =  
    7.    
    8.     // Update is called once per frame
    9.     void Update () {
    10.    
    11.  
    12.         if(Input.GetKey(KeyCode.Space))
    13.             GetNewLocation();
    14.    
    15.     }
    16.  
    17.  
    18.     void GetNewLocation ()
    19.     {
    20.         // if line pos is more than zere move 5 units to left
    21.         if( linepos > 0)
    22.         {
    23.             // ... do this.
    24.        
    25.         }
    26.  
    27.         // If it is neither of those then...
    28.         else
    29.         {
    30.             // ... do this.
    31.  
    32.         }
    33.     }
    34. }
    The voids are underlined with a red zigzag and when I hover over it is says "parser error unexpected symbol".
    Thanks for the help you give me.
     
  2. CrapyComputer

    CrapyComputer

    Joined:
    Dec 13, 2014
    Posts:
    3
    So I edited my code a bit and her it is now:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class lineswich : MonoBehaviour
    5. {
    6.     public int x = 2.5;
    7.  
    8.  
    9.     // Update is called once per frame
    10.     void FixedUpdate () {
    11.         if(Input.GetKey(KeyCode.Space))
    12.             GetLocation();
    13.    
    14.     }
    15.  
    16.  
    17.     void GetLocation () {
    18.         // if line pos is more than zere move 5 units to left
    19.         if(x > 0)
    20.         {
    21.             // ... do this once.
    22.             transform.Translate(new Vector3(-5,0));
    23.                
    24.         }
    25.  
    26.         if (x = 0)
    27.         {
    28.             transform.Translate(new Vector3(2.5,0))
    29.        
    30.         }
    31.             // If it is neither of those then...
    32.  
    33.         else
    34.         {
    35.             // ... do this once.
    36.             transform.Translate(new Vector3(5,0));
    37.  
    38.         }
    39.     }
    40. }
    41.  
    This is seriously the first code I have ever written but I realized that I dont need to know the new x value by finding the location ingame but I can just do something like this

    Code (CSharp):
    1.  
    2. else
    3.         {
    4.             // ... do this once.
    5.             transform.Translate(new Vector3(5,0));
    6.             x+5
    7.  
    So in that example when the object moves forward 5, 5 is added to the current value of x.

    I also have the problem of when I press the spacebar once it doesn't just change positions once but multiple times.
     
  3. CrapyComputer

    CrapyComputer

    Joined:
    Dec 13, 2014
    Posts:
    3
    I figured this out myself
     
  4. blizzy

    blizzy

    Joined:
    Apr 27, 2014
    Posts:
    775
    Sharing is caring, so please share your solution :)