Return Gameobject to original position - c#

I am using Unity for my project. What I am trying to do is when a button is pressed it moves a game object to a new position. Then when the same button is press again, the object returns to its original position.
My problem is with the while loop I am using; it just crashes Unity when I press this button. Am I using the while loop wrong or is there a better way of going about this?
Here is what I have so far:
public Gameobject TroubleMove;
public Gameobject TroubleAnchor;
public Gameobject TroubleMain;
public Gameobject CauseMain;
public Gameobject Cause;
public int OnOff = 1;
public void Change ()
{
switch (OnOff)
{
Case 1:
CauseMain.setActive (true);
Cause.setActive (true);
While (CauseMain == true)
{
CauseMove.transform.position = CauseAnchor.tranform.position;
}
OnOff += 1;
break;
Case 2:
CauseMain.setActive (false);
Cause.setActive (false);
OnOff _=1;
break;
}
}

I assume CauseMain is never being set to false and your while loop never exits.
Why do you need the while loop? Don't you want to change the cursor once? I would change while to if()

I will assume that the Change() is called in the OnClick() method.
Firsly, you should not use a while(true) loop when waiting for a user input or response. The while(true) loop will become a never ending loop and crash Unity / Device.
I am confuse with what your GameObjects really are but instead of while(true) loop, you should be using the Update() or FixedUpdate() method to transform the position (can be in relation to Time.DeltaTime). If I am not mistaken, this would be the skeleton of what could be done:
void Update () {
if(CauseMain) {
//TODO: Apply Transformation Changes
CauseMove.transform.position = CauseAnchor.tranform.position;
CauseMain = false;
}
}
This code snippet will only apply the transformation change whenever the player presses a button and once the position is set to "CauseMove", the check is disabled preventing an endless loop which will crash Unity.

Related

Animation not playing when pressing E | unity 3d

I am working on this 3d game were a player has to press E to interact with objects.
the player has a collider that when touching a type of trigger that has this type of code, makes an object pop up showing that the player's is "selecting" something.
when the player is in the trigger I made it were when they press E, the objects animation plays. When adding the objecting selecting thing it made it now that I cant make the animation play when pressing E
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Fixedpress : MonoBehaviour
{
public Animator Tributton;
public GameObject GM;
private Coroutine routine;
private void Start()
{
GM.SetActive(false);
}
private void OnTriggerStay(Collider other)
{
// in general rather use CompareTag instead of ==
// it is slightly faster and also shows an error if the tag doesn't exist instead of failing silent
if (!other.CompareTag("LookTrig")) {
GM.SetActive(true);
return;
}
// just in case to prevent concurrent routines
if (routine != null) StopCoroutine(routine);
// start a new Coroutine
routine = StartCoroutine(WaitForKeyPress());
}
private IEnumerator WaitForKeyPress()
{
// check each FRAME if the key goes down
// This is way more reliable as OnTriggerStay which is called
// in the physics loop and might skip some frames
// This also prevents from holding E while entering the trigger, it needs to go newly down
yield return new WaitUntil(() => Input.GetKeyDown(KeyCode.E));
// set the trigger once and finish the routine
// There is no way to trigger twice except exit the trigger and enter again now
Tributton.SetTrigger("Fiveyon");
Debug.Log("Fiveyon!");
// If you even want to prevent this from getting triggered ever again simply add
enabled = false;
// Now this can only be triggered ONCE for the entire lifecycle of this component
// (except you enable it from the outside again of course)
}
void OnTriggerExit(Collider other)
{
if (!other.CompareTag("LookTrig")) {
GM.SetActive(false);
return;
}
// when exiting the trigger stop the routine so later button press is not handled
if (routine != null)
{
StopCoroutine(routine);
GM.SetActive(false);
}
}
}
OnTrigger methods are called when another object with a Collider enters in your object Collider.
First, make sure everything was well set up well, and maybe you want to use OnCollision instead of OnTrigger methods, if you want to detect a collision whether than a "penetration" if i may say

How to check if a certain animation state from an animator is running?

I created an animator called "m4a4animator". Inside it, the main function is called "idle" (nothing), and other 2 states: "shoot" (mouse0) and "reload" (R). These 2 animation states are transitioned to "idle". Now, everything is working... but the only problem I have is this: if I am in the middle of reloading and and press mouse0 (shoot), the animation running state immediately changes to shoot... but I want to block that.
Now, the question: How can I stop CERTAIN animation changes while an animation is running?
Here is my animator
And here is my script:
using UnityEngine;
using System.Collections;
public class m4a4 : MonoBehaviour {
public Animator m4a4animator;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown (KeyCode.R)) {
m4a4animator.Play("reload");
}
if (Input.GetMouseButton(0)) {
m4a4animator.Play("shoot");
}
}
}
For the legacy Animation system, Animation.IsPlaying("TheAnimatonClipName) is used to check if the animation clip is playing.
For the new Mechanim Animator system, you have to check if both anim.GetCurrentAnimatorStateInfo(animLayer).IsName(stateName) and anim.GetCurrentAnimatorStateInfo(animLayer).normalizedTime < 1.0f) are true. If they are then animation name is currently playing.
This can be simplified like the function like the Animation.IsPlaying function above.
bool isPlaying(Animator anim, string stateName)
{
if (anim.GetCurrentAnimatorStateInfo(animLayer).IsName(stateName) &&
anim.GetCurrentAnimatorStateInfo(animLayer).normalizedTime < 1.0f)
return true;
else
return false;
}
Now, everything is working... but the only problem I have is this: if
I am in the middle of reloading and and press mouse0 (shoot), the
animation running state immediately changes to shoot... but I want to
block that.
When the shoot button is pressed, check if the "reload" animation is playing. If it is, don't shoot.
public Animator m4a4animator;
int animLayer = 0;
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.R))
{
m4a4animator.Play("reload");
}
//Make sure we're not reloading before playing "shoot" animation
if (Input.GetMouseButton(0) && !isPlaying(m4a4animator, "reload"))
{
m4a4animator.Play("shoot");
}
}
bool isPlaying(Animator anim, string stateName)
{
if (anim.GetCurrentAnimatorStateInfo(animLayer).IsName(stateName) &&
anim.GetCurrentAnimatorStateInfo(animLayer).normalizedTime < 1.0f)
return true;
else
return false;
}
If you need to wait for the "reload" animation to finish playing before playing the "shoot" animation then use a coroutine. This post described how to do so.
There are other threads about that: https://answers.unity.com/questions/362629/how-can-i-check-if-an-animation-is-being-played-or.html
if (this.animator.GetCurrentAnimatorStateInfo(0).IsName("YourAnimationName"))
{
//your code here
}
this tells you if you are in a certain state.
Animator.GetCurrentAnimatorStateInfo(0).normalizedTime
this give you the normalized time of the animation: https://docs.unity3d.com/ScriptReference/AnimationState-normalizedTime.html
Try to play with those function, I hope that solve your problem

Pause don't works (Unity c#)

I writing runner game on Unity (C#) for mobile phones.
I made Pause button on screen, using Canvas - Button.
Also I made code for Pause in Platformer2DUserControl script.
Here it is code of this script:
using UnityEngine;
using UnitySampleAssets.CrossPlatformInput;
namespace UnitySampleAssets._2D
{
[RequireComponent(typeof(PlatformerCharacter2D))]
public class Platformer2DUserControl : MonoBehaviour
{
private PlatformerCharacter2D character;
private bool jump;
public bool paused;
private void Awake()
{
character = GetComponent<PlatformerCharacter2D>();
paused = false;
}
private void Update()
{
/*if (Input.GetButton("Fire1"))
{
}*/
if (!jump)
// Read the jump input in Update so button presses aren't missed.
jump = Input.GetButton("Fire1"); //&& CrossPlatformInputManager.GetButtonDown("Jump");
}
private void FixedUpdate()
{
// Read the inputs.
bool crouch = Input.GetKey(KeyCode.LeftControl);
// float h = CrossPlatformInputManager.GetAxis("Horizontal");
// Pass all parameters to the character control script.
character.Move(1, false, jump);
jump = false;
}
public void Pause()
{
if (!jump)
// Read the jump input in Update so button presses aren't missed.
jump = Input.GetButton("Fire1"); //&& CrossPlatformInputManager.GetButtonDown("Jump");
paused = !paused;
if (paused)
{
jump = !jump;
Time.timeScale = 0;
}
else if (!paused)
{
// jump = Input.GetButton("Fire1");
Time.timeScale = 1;
}
}
}
}
My Pause Button is WORKS well. But when I tap it , my character is jumping and game is pausing.
I want to make that , when I tapping the button game just pausing and character don't jump.
How I can make it. Thank's for help.
I would advise you not to use the same input for jumping and pausing. Also, separate your jump and pause functionalities into separate functions. For pausing, create a UI button on the screen and make it call a public function on a Pause script, that will toggle pause. Then, in the same function, check if you are paused or not and adjust Time.timescale accordingly
You will have to attach the script with pause functionality on to an object that will always be in a screen (Say, a panel in your canvas or your MainCamera). Under the button, add a new onClick() function after dragging the GO with the apt script to the box. Then, select the public function aforementioned.
private bool paused = false;
//The function called by the button OnClick()
public void TogglePause()
{
paused = !paused;
if(paused)
Time.timescale = 0f;
else
Time.timescale = 1f;
}
Hope this helped!
Well, your code is messed up, but just remove the jump script from the pause method. So it will just pause...
public void Pause()
{
paused = !paused;
if (paused)
{
Time.timeScale = 0;
}
else
{
Time.timeScale = 1;
}
}
Note that Input.GetButton should only be called in Update
EDIT
The problem that you have to tap your screen to press the button. And the code jump = Input.GetButton("Fire1"); basically means "Am I tapping the screen?" So both JUMP and PAUSE are triggered.
One solution would be to put a canvas filling the whole screen under your buttons. You will trigger a JUMP action only when this canvas is clicked. So when you click your pause button, it will stop the propagation and won't click the full screen canvas, which won't trigger the jump action.
EDIT2
Try changing the lines (in the Update function) :
if (!jump)
// Read the jump input in Update so button presses aren't missed.
jump = Input.GetButton("Fire1");
for :
if (!jump && !paused)
// Read the jump input in Update so button presses aren't missed.
jump = Input.GetButton("Fire1");
I think the button event is called before the Update, see reference
One solution would be possible make sure the raycast on of pause button.
I have always done it like this, essentially surround the entire fixed update (or wherever you have your game motion) with the paused-bool
private void FixedUpdate()
{
if (paused != true){
// Read the inputs.
bool crouch = Input.GetKey(KeyCode.LeftControl);
// float h = CrossPlatformInputManager.GetAxis("Horizontal");
// Pass all parameters to the character control script.
character.Move(1, false, jump);
jump = false;
}
}

unity2D C# - player must press two switches to open door

Essentially I have a door trigger event where if the player presses the switch then the door open. I wish however to do it so that if the door associated needs two switches to open well... then it only opens if two switches are pressed. Here is my code for my InteractSwitch
public class InteractSwitch : MonoBehaviour
{
Animator animator;
public DoorEventTrigger[] doorTriggers;
public bool pressed;
private bool down;
// Use this for initialization
void Start ()
{
animator = GetComponent <Animator> ();
}
// Update is called once per frame
void Update ()
{
}
void OnCollisionEnter2D (Collision2D target)
{
if (target.gameObject.tag == "Player") {
down = true;
animator.SetInteger ("SwitchTrig", 1);
foreach (DoorEventTrigger trigger in doorTriggers)
if (trigger != null)
trigger.Toggle (true);
}
}
When triggered this event checks for player, shows switch has been pressed then sends a bool to a function called Toggle which handles the operation of the door.
Next I have my DoorEventTrigger event which checks if the bool sent = true. If it is then the door will open. Here is where I am stuck. As seen in the code I have created a array of InteractSwitch which stores the amount of switches I want the player to have pressed before the door will open. I then state that if the length > 1 then this if condition is true and some code will be added here which will then open the door only if the player has selected the InteractSwitch[].length switches. My question is how would I check that all instances of InteractSwitch for this DoorEventTrigger equals true?
Here is my DoorEventTrigger code
public void Toggle (bool val)
{
if (switchTriggers.Length > 1) {
Debug.Log ("HAS THIS ACTUALLY DONE ANYTHING");
door.Open ();
}
else
{
if (val)
door.Open ();
else
door.Close ();
}
}
}
The solution I came up with is nowhere near I believe the most efficient answer as it requires code each time u add a new switch but this is what I have
firstly in the InteractSwitch script the bool down is public so it can be accessed by the switchtriggers
public bool down;
Next is the added code to the Toggle() method
public void Toggle (bool val)
{
if (switchTriggers.Length > 1) {
if((switchTriggers[0].down == true) && (switchTriggers[1].down == true)){
Debug.Log ("THIS ACTUALLY DONE SOMETHING!");
door.Open ();
}
}
else
{
if (val)
door.Open ();
else
door.Close ();
}
}
Now from the public SwitchInteract[]; I know the exact amount of elements in the array however this could be checked through switchTriggers.length; Knowing there is two elements in the array my condition switchTriggers.Length > 1 is correct so next I take the two elements in the array and check if they are down; if BOTH are down then the condition continues and the Debug.Log() validates this. Finally Open() is called which opens the door.
You can have the switches increment or decrement a required switch variable in the door object.
Give the door object a requiredSwitch variable.
Give each switch a door variable and set it to the door it controls.
When a switch is activated, have it decrement its door's requiredSwitch variable.
When the door's requiredSwitch variable is 0, then the door opens.
You can also have the switches increment the variable if they get deactivated.
This solution allows you to add switches by just setting the door variable of the switch and incrementing the door's initial requiredSwitch variable.

How to check current status of gameObject and update to a new animation and check again?

I am creating a 2D platform game with Unity and C#. I searching for how I can do the following.
I have 5 sets of animations and one gameObject(later in game there should be more than one object that can be changed, so maybe we can keep that in mind) with the tag "Campfire0" stored in anim0 inside the start function. What I want is when the user pressed E on his keyboard it will check which is the current animation of the current gameObject and change that animation to "Camfire25" or another number like 50, 75 or 100 and update the gameObject tag. After that is done, it should again check all if statements and the second time you pressed E it should look at the if statement with if(anim25) and so on.
Problem
Below code I use currently. But the problem is the following. I declare inside the start function the animator of each game object tag, but this will be fired one time if the game is started. After pressing the second time on E, the error "NULL" NullReferenceException will occur. So I think I should store the values inside an array? And how should I do that.
For checking which animation is the current one, I use if statements, but can it also be done with a foreach loop or is there a shorter way to do this?
Current Code:
public Animator anim0;
public Animator anim25;
public Animator anim50;
public Animator anim75;
public Animator anim100;
// Use this for initialization
void Start () {
count = 0;
setCountText ();
currentValue = startingValue;
//anim = GameObject.Find("Campfires").GetComponent<Animator>(); // Parent of all Campfires
anim0 = GameObject.FindGameObjectWithTag("Campfire0").GetComponent<Animator>();
anim25 = GameObject.FindGameObjectWithTag("Campfire25").GetComponent<Animator>();
anim50 = GameObject.FindGameObjectWithTag("Campfire50").GetComponent<Animator>();
anim75 = GameObject.FindGameObjectWithTag("Campfire75").GetComponent<Animator>();
anim100 = GameObject.FindGameObjectWithTag("Campfire100").GetComponent<Animator>();
}
void Update () {
if (inTrigger){
if (Input.GetKeyDown(KeyCode.E) && count > 0){
// Update counter on press
updateCount();
if (anim0){
anim0.SetTrigger ("Campfire25");
GameObject.FindGameObjectWithTag("Campfire0").transform.tag = "Campfire25";
}
if (anim25){
Debug.Log ("hello");
anim25.SetTrigger ("Campfire50");
GameObject.FindGameObjectWithTag("Campfire25").transform.tag = "Campfire50";
}
if (anim50){
anim50.SetTrigger ("Campfire75");
transform.tag = "Campfire75";
}
if (anim75){
anim75.SetTrigger ("Campfire100");
transform.tag = "Campfire100";
}
}
}
}
Above script works for the first time, it will change the campfire tag to "Campfire25" and show another animation clip, after pressing E for the second time it will give me an error of NullReferenceException. This is because the Start Function will be fired ones.

Categories