Home c# hang the script on the button in unity

hang the script on the button in unity

Author

Date

Category

How to hang the script button? Management on touchscreen. There is a button, when you click on which the animation should be performed, but not in any case, but only when you find a character from a specific object at a certain distance.
Here is a script, although it may not be correct:

using unityengine;
Using System.Collections;
Public Class Hand: Monobehaviour {
Public Transform Player;
Public Transform Mushrums;
Private Animator Anim;
Private Bool Pick_Up;
void start () {
Anim = GetComponent & LT; Animator & GT; ();
Pick_Up = false;
}
Void Update () {
Anim.SetBool ("Pick_Up", Pick_UP);
Float Dist = vector3.distance (player.position, mushrums.position);
Debug.log (DIST);
if (dist & lt; = 58.5f)
{
Pick_Up = True;
// Here removing the object to which
}
ELSE.
{
Pick_Up = false;
}
}
}

But how is it on the hang button?


Answer 1, Authority 100%

If you use the UI Button in the Inspector window there is a block on click (Button). Clicking on the plus you will need to hang the object on which your script is present. After choosing your function from the list on the right. The access modifier function must be public.


Answer 2, Authority 50%

If you use pure Touch Control (for example, when clicking directly on the subject), the following must work in theory. In the update place the following code:

if (input.touches.length & gt; 0 & amp; & amp; input.touches [0] .phase == TouchPhase.Began) {
  Raycasthit Hit;
  Ray Ray = Camera.Main.ScreenPointToAY (Input.Touches [0] .position);
  If (psyssics.raycast (ray, out hit, float.maxvalue) & amp; & amp; dist & lt; = 58.5f) {
    hit.sendMessage ("Onpress", SendMessageOptions.dontrequireceiver);
  }
}

At the same time, you also add Onpress :

void onpress () {
// Do what it will do .. for example:
  Destroy (this.gameObject);
}

All Code Update :

void update () {
  Anim.SetBool ("Pick_Up", Pick_UP);
  Float Dist = vector3.distance (player.position, mushrums.position);
  Debug.log (DIST);
  if (input.touches.length & gt; 0 & amp; & amp; input.touches [0] .phase == TouchPhase.Began) {
    Raycasthit Hit;
    Ray Ray = Camera.Main.ScreenPointToAY (Input.Touches [0] .position);
    If (psyssics.raycast (ray, out hit, float.maxvalue) & amp; & amp; dist & lt; = 58.5f) {
      hit.sendMessage ("Onpress", SendMessageOptions.dontrequireceiver);
    }
  }
}

You can try another option:
In Update code

foreach (touch in input.touches) // For each touch to the screen (for you can put two fingers simultaneously)
{
  if (touch.phase == TouchPhase.Began) // If the Touch Phase "touched" (i.e., as soon as I poked into the screen)
  {
    Ray Ray = Camera.Main.ScreenPointToAY (touch.position); // Create a ray at the point of touch
    Raycasthit Hit; // register the variable in which the info about the subject we hit
    Physics.Raycast (Ray, Out Hit); // Release the beam and write down all the info in Hit
    if (hit.collider == gameobject.collider & amp; & amp; dist & lt; = 58.5f) // If we hit the object on which this script hangs and the distance is suitable
    {
      // Make something with him - Want right here
    }
  }
}

All Code Update :

void update () {
  Anim.SetBool ("Pick_Up", Pick_UP);
  Float Dist = vector3.distance (player.position, mushrums.position);
  Debug.log (DIST);
  Foreach (Touch Touch in Input.Touches) // For each touch to the screen (for you can put two fingers simultaneously)
  {
    if (touch.phase == TouchPhase.Began) // If the Touch Phase "touched" (i.e., as soon as I poked into the screen) 
{
       Ray Ray = Camera.Main.ScreenPointToAY (touch.position); // Create a ray at the point of touch
       Raycasthit Hit; // register the variable in which the info about the subject we hit
       Physics.Raycast (Ray, Out Hit); // Release the beam and write down all the info in Hit
       if (hit.collider == gameobject.collider & amp; & amp; dist & lt; = 58.5f) // If we hit the object on which this script hangs and the distance is suitable
       {
         // Make something with him - Want right here
       }
     }
   }
}

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions