News: Enjoy our Christmas offer 30% off on our latest package Tower Defense Level Kit http://u3d.as/m7h

  • September 24, 2018, 03:09:33 PM

Login with username, password and session length

Author Topic: Set a boolean when an object is touched?  (Read 3364 times)

Offline Jude

  • Newbie
  • *
  • Posts: 7
Set a boolean when an object is touched?
« on: June 02, 2016, 11:20:54 PM »
I've added the Quick Touch gesture to an object and that all works fine. I was just wanted to change a boolean to true while the touch was down. I have the object with the script attached to the gesture. I was just wondering how I can add things to the list of static parameters? Is there a way to have it directly access the script, or do I have to use the sendmessage and have it receive it and then change the variable? Just trying to figure out the most straight forward way to accomplish this.

Offline Jude

  • Newbie
  • *
  • Posts: 7
Re: Set a boolean when an object is touched?
« Reply #1 on: June 03, 2016, 04:46:59 AM »
I figured it out. I just wasn't overly familiar with how sending messages work. I do have another question though. I have a sprite which I want to disappear whenever the player is touching it and reappear when they let go. I use the sprite renderer bool on touch down and it disappears but I can't get it to come back. I can tell that the collider is still there getting hit. Do I have to write a coroutine to enable it? I would assume that there would be an option, or that the bool option would just toggle it.

Offline Nicolas

  • Administrator
  • Hero Member
  • *****
  • Posts: 792
Re: Set a boolean when an object is touched?
« Reply #2 on: June 03, 2016, 09:01:37 AM »
Hi,

The events of the components "QuickGesture" send a parameter of type Gesture, you can't connected directly to your variable.
It must go through a method that you will call from the inspector.

Example script
Code: [Select]
using UnityEngine;
using System.Collections;

public class NewBehaviourScript2 : MonoBehaviour {

public bool myBool;

public void SetBool2True(){
myBool = true;
}

public void SetBool2False(){
myBool = false;
}
}

Inspector setup


What exactly is the condition for the sprite to be displayed again? (If this is not related to the touch, it will therefore manage otherwise)

Offline Jude

  • Newbie
  • *
  • Posts: 7
Re: Set a boolean when an object is touched?
« Reply #3 on: June 08, 2016, 11:28:09 PM »
I changed the way I was going about it and now the touch just changes the alpha of the sprite to 0 when it is down and it works fine.

I do have another question though. My scene has two areas for touches, one on each end of the device. I have a bomb that is instantiated into the scene a couple of seconds after both of the areas are touched. The bomb is only supposed to move if both of the touches are down, if one of them is up it should stop. The problem is the bomb is running the function when the touches occur (I can tell by the Debug.Log) but the rest of the code to change the boolean doesn't happen. If I set the boolean manually in the editor while the game is running it works as intended. Do I have to set it up differently since the bomb is a prefab and not in the scene at the start?

The code on the BombScript and the GameControllerScript are set up the same way. The game controller script works fine, but it is on an object that starts in the scene.

Here is the Bomb Script:

Code: [Select]
using UnityEngine;
using System.Collections;

public class BombScript : MonoBehaviour
{
Vector2 bombPostion = new Vector2 (0,0);
bool facingUp = false;
[SerializeField]
bool p1Thumb;
[SerializeField]
bool p2Thumb;


void FixedUpdate ()
{
if (p1Thumb == true & p2Thumb == true)   // When both thumbs are down the bomb moves toward the lowest side of the device
{
if (0.5f > Input.acceleration.y & Input.acceleration.y > 0f)
{
bombPostion.y = bombPostion.y + 0.01f * Time.timeSinceLevelLoad;
}
if (1f > Input.acceleration.y & Input.acceleration.y > 0.5f)
{
bombPostion.y = bombPostion.y + 0.02f * Time.timeSinceLevelLoad;
}
if (1.5f > Input.acceleration.y & Input.acceleration.y > 1f)
{
bombPostion.y = bombPostion.y + 0.03f * Time.timeSinceLevelLoad;
}


if (-0.5f < Input.acceleration.y & Input.acceleration.y < 0f)
{
bombPostion.y = bombPostion.y - 0.01f * Time.timeSinceLevelLoad;
}

if (-1f < Input.acceleration.y & Input.acceleration.y < -0.5f)
{
bombPostion.y = bombPostion.y - 0.02f * Time.timeSinceLevelLoad;
}

if (-1.5f < Input.acceleration.y & Input.acceleration.y < -1f)
{
bombPostion.y = bombPostion.y - 0.03f * Time.timeSinceLevelLoad;
}


if (Input.acceleration.y > 0 &&!facingUp)
Flip();
else if (Input.acceleration.y < 0 && facingUp)
Flip();

}

transform.position = bombPostion;  // Stops the bomb from leaving the screen

if (bombPostion.y > 4)
{
bombPostion.y = 4;
}

if (bombPostion.y < -4)
{
bombPostion.y = -4;
}

}


void Flip()      //Flips the bomb sprite when it changes direction
{
facingUp = !facingUp;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}


void OnTriggerEnter2D (Collider2D other)  //When the bomb hits a city makes it do everything
{
if (other.gameObject.tag == "City")
{
other.gameObject.GetComponentInChildren<CityAnimate>().hitByBomb = true;
Destroy (gameObject);
}
}

public void ThumbDown1 ()
{
Debug.Log("Down 1");
p1Thumb = true;
}

public void ThumbDown2 ()
{
Debug.Log("Down 2");
p2Thumb = true;
}

public void ThumbUp1 ()
{
Debug.Log("Up 1");
p1Thumb = false;
}

public void ThumbUp2 ()
{
Debug.Log("Up 2");
p2Thumb = false;
}
}

Here is a picture of how I have the editor, both touch areas are set up identically:

https://drive.google.com/file/d/0B_kij92X6sWAYVJYUGF1eHJRbGM/view?usp=sharing

Offline Nicolas

  • Administrator
  • Hero Member
  • *****
  • Posts: 792
Re: Set a boolean when an object is touched?
« Reply #4 on: June 09, 2016, 08:38:26 PM »
Hi,

Your script seems okay, but you tell me that the player must have 2 touches in the same in opposite position, so  on what gameobject did you drop the component quickgesture ?

Offline Jude

  • Newbie
  • *
  • Posts: 7
Re: Set a boolean when an object is touched?
« Reply #5 on: June 10, 2016, 04:20:19 AM »
Both of the red thumbprints (Player 1 Thumb & Player 2 Thumb) have a Quick Touch Start and a Quick Touch Up. Here is a picture of the Unity inspector:

https://drive.google.com/file/d/0B_kij92X6sWANjN6U0FSUHR2ZWc/view?usp=sharing

Player 1 Thumb calls the ThumbDown1 and ThumbUp1 functions like you see in the picture. Player 2 Thumb is set up the same way but calls the ThumbDown2 and ThumbUp2 functions. They work fine on the GameControllerScript and the ThumbScript. Only the BombScript doesn't change the booleans for some reason. It does call the functions though because I can see the Debug.Log which makes me wonder why the other lines of the script don't work.

Offline Nicolas

  • Administrator
  • Hero Member
  • *****
  • Posts: 792
Re: Set a boolean when an object is touched?
« Reply #6 on: June 10, 2016, 08:57:52 AM »
Hi,

If your methods are properly called by EasyTouch is in the conventional use of unity.

Some notes :
- Why do you use the FixedUpdate ?  the FixedUpdate must be use when you manipulate objects by their physic rigidbody

- Why do you use Time.timeSinceLevelLoad as multiplicator ? 
In Update you must use Time.deltaTime
In FixedUpdate you muse use Time.fixedeltatime

- You add log in methods call by EasyTouch,but have you done for your FixedUpdate method ?

Offline Jude

  • Newbie
  • *
  • Posts: 7
Re: Set a boolean when an object is touched?
« Reply #7 on: June 15, 2016, 07:17:46 PM »
I used FixedUpdate for the just so it would be more even. I have changed it back and there is no difference in the scripts performance. I use Time.timeSinceLevelLoad because I want the bomb to keep getting faster the longer it is on the screen. I could do it a different way if you think it is better.

I made a video because showing you in motion is better than trying to explain with pictures.

https://youtu.be/Lh9wkIbxSp4

The beginning is my two scripts that effect this. The GameControllerScript works fine. It instantiates the airplane when both thumbs are pressed together.
The BombScript has two boolean variables for when the thumbs are down just like the GameControllerScript. All of this script works except the part at the bottom with the functions to turn the variables true and false, like I posted before.

I show you how both thumb objects are set up in Unity.

You can see the P1/2 Thumb variables in the GameControllerScript and watch them change as I touch them. When the red thumbprints disappear the touch is down. You can also see this in the console. But they shouldn't appear in the console yet because the BombScript is on an object which is not in the scene yet.

When I touch them both at the same time the airplane comes like it should and instantiates the bomb. It keeps saying both thumbs are down even though you can see me touching the screen and the values changing in the GameControllerScript.

Then I show you the bomb in the inspector and how the values stay true even though you can see me touching the thumbs.

The last part is me showing how if only one thumb is down when the bomb is instantiated it stays that way. When I manually change it's value in the editor it works fine, but for some reason the BombScript will not change by me touching the screen like the GameControllerScript will.


I have tried changing the names of the functions so they are different on both scripts but it still works the same. The only difference I can think of is because the plane is instantiated. I set it up in the inspector using the prefab.

Any help at all would be great, thank you for your time.


Offline Nicolas

  • Administrator
  • Hero Member
  • *****
  • Posts: 792
Re: Set a boolean when an object is touched?
« Reply #8 on: June 15, 2016, 07:37:15 PM »
Hi,

To help you, I need the project, As I have said, if the methods are properly called by quick gestures is that we are in the classic debugging (not relative to easytouch).

If the methods are properly called, it is not possible that the values do not change. That means either you have another script that conflict with this one.

Offline Jude

  • Newbie
  • *
  • Posts: 7
Re: Set a boolean when an object is touched?
« Reply #9 on: June 21, 2016, 02:31:54 AM »
Good News! I figured out what is going on and how to change it to make it work correctly. The bad news is I don't know how to change it.

Since the Bomb object is not in the scene at the beginning I have the prefab of it that gets instantiated in the Easy Touch Inspector. Even though the bomb is not in the scene the script on it is still running. That's why I get the debug messages before it appears. Also when I have it selected in the prefab menu at the bottom I can see that the script on it is working correctly. Like in this picture.

https://drive.google.com/file/d/0B4dYVtTkBtp-VTc4SHBxWTJCVjQ/view?usp=sharing

The problem is the bomb that gets instantiated isn't linked to Easy Touch in the inspector. I thought that if I used the prefab it would work on all instances of the object in the scene, but I guess it doesn't work on any of them. If I pause the game and assign the bomb after it is instantiated everything works as intended. Here is a picture of the clone version not working.

https://drive.google.com/file/d/0B4dYVtTkBtp-cTg3QXRucmdJb1E/view?usp=sharing

So I guess what I need to figure out is how to assign the object to Easy Touch manually after it has been instantiated. Or maybe there is something I have to do differently with prefabs that I don't know about.

Offline Nicolas

  • Administrator
  • Hero Member
  • *****
  • Posts: 792
Re: Set a boolean when an object is touched?
« Reply #10 on: June 21, 2016, 08:23:42 AM »
Hi,

It's a little point, but the fact that the bomb is instantiate changes everything. Objects placed in the Inspector can not be instantiated objects, because they must be known to the launch of the scene.

For your need, you must use non-persistent listener,Ie that the bomb must register itself on your quickgesture to its creation time and unregister at its destructiontime by using Addlistener (https://docs.unity3d.com/ScriptReference/Events.UnityEvent.AddListener.html ) and RemoveListener (https://docs.unity3d.com/ScriptReference/Events.UnityEventBase.RemoveAllListeners.html)


or you can use classical EasyTouch events.


Offline Jude

  • Newbie
  • *
  • Posts: 7
Re: Set a boolean when an object is touched?
« Reply #11 on: June 24, 2016, 05:00:07 PM »
What is the event called that I am listening for? I know I want the Bomb object to listen for the EasyTouch Down event so it can do the ThumbDown1() function. Sorry, I just don't understand the syntax very well.