Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Problem about moving

Discussion in 'Scripting' started by lib87, Sep 22, 2015.

  1. lib87

    lib87

    Joined:
    Aug 28, 2015
    Posts:
    17
    hi

    I have a problem. Im creating a game and basically i have 2 boxes. if these boxes loook each other from the side that i define (i put a plane to their surfaces) then a button appears.. If i click to this button one piece moves to another until it collides and it doesnt stop until it reaches to the middle of the other cube..

    Im detecting hits (raycasting) from my first plane to another object plane.. IF hit happens then i add them to an array. If there are 2 objects in array button shows up... I tried many things including deactivating script but then there are other problems shows up.. Like i cannot use raycasting again cuz i deactivate it...

    Here is my code:

    //This is how i move from one object to another


    Code (CSharp):
    1. Transform a = gameTag.gameO[0].transform;
    2.     Transform b = gameTag.gameO[1].transform;
    3.            
    4.     a.position = Vector3.MoveTowards(a.position,b.position, moveSpeed * Time.deltaTime);
    //this is my raycast system. Im putting this script to each of my objects..


    Code (CSharp):
    1. RaycastHit hit;
    2.         if (flagTrigger==false)
    3.         {
    4.          if (Physics.Raycast(transform.position, -transform.forward, out hit, 10))
    5.                {
    6.                  var forward = transform.TransformDirection(Vector3.forward) * 10;
    7.           arrayListForSelectables gameTag = GameObject.FindGameObjectWithTag("TheGame").GetComponent<arrayListForSelectables>();
    8.         if (hit.transform.tag == "Select")
    9.                         {
    10.                             if (!gameTag.gameO.Contains(this.gameObject))
    11.                                 gameTag.gameO.Add(this.gameObject);
    12.                         }
    13.                         else
    14.                         {
    15.                             gameTag.gameO.Remove(this.gameObject);
    16.                         }
    17.                     }
    //Lastly here my kind of trigger system


    Code (CSharp):
    1. void OnTriggerEnter(Collider other)
    2.     {
    3.         Raycast u = other.GetComponent<Raycast>();
    4.         if (u != null)
    5.         {
    6.     arrayListForSelectables gameTag = GameObject.FindGameObjectWithTag("TheGame").GetComponent<arrayListForSelectables>();
    7.                 if (enabled && gameTag.gameO.Contains(this.gameObject))
    8.                 {
    9.                     gameTag.gameO.Remove(this.gameObject);
    10.                     flagTrigger = true;
    11.                     //enabled = false;
    12.                 }
    13.             }
    14.         }
    I commented out the enabled part for now.. If i use it i cannot use the same script again... I know the problems but i donno the answer for it.. Any help please..
     
    Last edited: Sep 22, 2015
  2. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
  3. lib87

    lib87

    Joined:
    Aug 28, 2015
    Posts:
    17
    done