Search Unity

HELP !!!increase/decrease local position with mouse click

Discussion in 'Scripting' started by javert, Dec 1, 2013.

  1. javert

    javert

    Joined:
    Oct 5, 2013
    Posts:
    7
    I need help with my script, I can not make the cube move in + 1.0 in relation to its initial position.
    cube current position: x -2, y 0 , z -1 (this is just a example of his position).
    by clicking on the object with the tag "CIMA" , the position z must increase by +1, and clicking on the object with the tag "BAIXO" decrease z position in -1.
    my script in C #
    using UnityEngine;
    using System.Collections;

    public class regraBlocos : MonoBehaviour {
    private Vector3 iniPOS;
    private bool blocoAtivo = true;
    private bool blocoPressionado = false;
    private bool setaCima = false;
    private bool setaDireita = false;
    private bool setaBaixo = false;
    private bool setaEsquerda = false;
    public Texture2D buttonArt;
    // Use this for initialization
    void Start () {
    iniPOS = transform.localPosition;
    }

    // Update is called once per frame
    void Update()
    {
    if (Input.GetMouseButtonDown(0) blocoAtivo ==true)
    {

    RaycastHit hitInfo = new RaycastHit();
    bool hit = Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hitInfo);
    if (hit)
    {
    if (hitInfo.transform.gameObject.tag == "BLOCO" blocoAtivo ==true)
    {blocoPressionado = true;}
    if (hitInfo.transform.gameObject.tag == "CIMA" blocoAtivo ==true)
    {setaCima = true;}
    if (hitInfo.transform.gameObject.tag == "DIREITA" blocoAtivo ==true)
    {setaDireita = true;}
    if (hitInfo.transform.gameObject.tag == "BAIXO" blocoAtivo ==true)
    {setaBaixo = true;}
    if (hitInfo.transform.gameObject.tag == "ESQUERDA" blocoAtivo ==true)
    {setaEsquerda = true;}
    }
    if(setaCima ==true){
    iniPOS.x =+1;
    //iniPOS.y =+0;
    //iniPOS.z =+0;
    transform.localPosition = iniPOS;
    }
    }
    }
    }
     
  2. exiguous

    exiguous

    Joined:
    Nov 21, 2010
    Posts:
    1,749
    if (hitInfo.transform.gameObject.tag == "CIMA" blocoAtivo ==true)
    transform.position = new Vector3(transform.position.x, transform.position.y, transform.position.z + 1.0f);
     
  3. javert

    javert

    Joined:
    Oct 5, 2013
    Posts:
    7
    thanks man its work!