Code like Bookworm Adventures in Unity - c#

How do you animate if the button is correct and the other buttons are not correct
if(btnA.isCorrect){
//btnA Animation
} else if(btnB.isCorrect){
//btnB Animation
} else if (btnC.isCorrect){
//btnC Animation
} else {
//btnD Animation
}
I have that code inside the method UserSelect(int n) wherein n determines the btnNumber in my buttons,
and I do think that this is the logic of what I wanted to do if the user selected the buttons
PS: if the user selected a button and it is correct, the player will attack an enemy, otherwise, the enemy will attack the player

Related

Unity OnMouseDown too slow

I am coding a clicker game and I do not want my program to calculate whenever I press the UI. I have tried using OnMouseDown, OnPointerDown and !EventSystem.current.IsPointerOverGameObject() but all of them ruin the clicker-feel because you can't spam click. Are there any shorter alternatives? Thank you in advance!
Edit: I've done all the things you've commented and I've found the problem. Whenever I click my mouse button, text appears with the number of coins I get per tap. Therefore, it counts as a UI element, making it not possible to spam-click. I have now turned off the "Raycast Target" for the text and now it works as it should. Thank you for your help!
You can use Input class along with a check:
void Update()
{
// Check if the left mouse button was clicked
if (Input.GetMouseButtonDown(0))
{
// Check if the mouse was clicked over a UI element
if (EventSystem.current.IsPointerOverGameObject())
{
Debug.Log("Clicked on the UI");
}
}
}
Or you can use OnMouseDown on specific object where you want to detect the click but with a UI check:
void OnMouseDown()
{
// Check if the mouse was clicked over a UI element
if (EventSystem.current.IsPointerOverGameObject())
{
Debug.Log("Clicked on the UI");
}
}

Clicking anywhere triggers the button

In my scene, I have this set-up:
Game Controller
Level Controller
Main Camera
Event System
Canvas
Player
PlayerCanvas (Render Mode is World Space)
Button
I set the PlayerCanvas into the same width and height of the button. And both of them are just small. I put Debug.Log to check everytime I press the button. But somehow, it triggers the button even if I click way off the screen. Can help to explain why is this happening. Thanks!
Note: I'm trying to add a button beside the Player so that even if the Player moves, the button will just follow.
I think your hierarchy is wrong.
Try something like
Game Controller
Level Controller
Main Camera
Player
Event System
Canvas (Screen Space - Overlay)
Button
Not sure what Player is, but I'm sure on my hierarchy Event System and Canvas are on the same level. And every button is son of Canvas. Not sure you can make multiple Cavas (you should not need it anyway).

Unity Button Animation

Does anyone know a possible way for an animation that makes it so when a button gets clicked text pops up above the button that says [Clicked]. It must fade away after 3 seconds and it continues to go up. (The text will move up.)
Pretend like this is how it would look
Clicked but faded away
Clicked
Clicked
Clicked
Button
(Unity Using C#)
How about using a Prefab for this?
You'd create a Prefab with a root object and a text label as a child. Then you animate the text label upwards and fading out over three seconds using the Unity animation window.
On button click, you instantiate the prefab, make it a child of the button, and position it at the same position as your button AND auto-destroy it after 3 seconds.
Here's a code example, pretending that your prefab name is "myAnimatedText"
GameObject animatedButtonText = Instantiate(Resources.Load("myAnimatedText"), transform);
animatedButtonText.transform.position = transform.position;
Destroy(animatedButtonText, 3.0f);

Press a in game Button to play a animation

I recently thought about adding a InGame Button to my Game. It's not a GUI or UI Button, it's a Block, added to a Wall for example.
Dummy Code:
OnTriggerEnter(c:Collider) {
if(c.gameObject.tag =="Player")
{
//Text = "E to interact!"
if(key.pressed("e")
{
//Connect the Button to a specific Block, play a Animation
}
}
}
So how do I connect a specific Block to the Button, and if I press e, Play the Animation just on the specific block? Please keep in mind that I'm new in Unity.
Thanks for helping out!
I don't know if you already managed to create your animation, so I-ll explain from scratch. When in the editor, select your Door and press ctrl+6 to open the animation window. From here you can animate your block. When you will be done creating the animation, your block object will have a new script attached to it: an animator. You can see the animator state machine in the animator window
These are two different things:
Animation: Defines a single animation (translation, rotation, change of color, ...)
Animator: defines when an animation occurs for the corresponding gameobject. An animator can have variables (for example, a bool) that define which is the next animation to be played
Any object can have an animator (your button can have one to move when it is pressed. You door can have another one to open / close)
For instance, in your button animator, you should have three states: Idle, Press, UnPress.
The state Press will contain the animation "press" with speed 1. The state UnPress will contain the animation "press with speed -1
Then, still in the animator window, You will create links between Idle and the two other states and add a trigger condition called "OnPress" (for example)
You can do the same to animate your door
In your Button code, you will then write
public Animator Door; // In the editor, give a reference to your door. It must have an Animator script for this to work
OnTriggerEnter(c:Collider) {
if(c.gameObject.tag =="Player")
{
//Text = "E to interact!"
if(key.pressed("e")
{
GetComponent<Animator>().SetTrigger("OnPress"); // The button's animator goes to "pressed" state
Door.SetTrigger("Open"); // The door's animator goes to "open" state
}
}
}
Then you could add another trigger to unpress the button
One more thing: When you say "Connect the button to the block", I feel like you misunderstood something: Your button script should be already added to the block in the editor
Look at these two links for more information on animations:
http://docs.unity3d.com/Manual/animeditor-UsingAnimationEditor.html
http://docs.unity3d.com/Manual/AnimatorWindow.html

Is there a way to cancel Touches on Awake of a Screen?

I'm having an issue with a game I'm working on. As the user drags his character around the screen, if the character dies while dragging, I switch to the "dead" scene. The problem is that on the new scene there are two buttons using the new Unity UI. Buttons and if the player happens to have his finger over one of the buttons when the scene loads and lets go as a reaction they end up executing the button before they even see what was on the screen.
Is there a way I can say on Awake, Start or Enable to cancel all touches? Forcing the user to lift their finger off the screen and then tap the button they want after they digest the screen?
IMHO better user experience is to play an animation when the character dies. The animation could even be as simple as blurring or dimming the view slowly. Then the player has some time to react and he or she will quite likely end the ongoing drag.
If you still want to disable all touches, one work around could be to add boolean telling if any new touches has started at the new scene. Unfortunately, I am guessing that this will not work robustly:
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour {
private bool newTouchesInThisScene = false;
void Update() {
for(int i = 0; i < Input.touchCount; ++i) {
if (Input.GetTouch(i).phase == TouchPhase.Began) {
newTouchesInThisScene = true;
}
}
}
}

Categories