How do you stop a function with another function?
I'm kind of new to unity and i don't know how to do this, although i think it's going to be a really stupid answer I need that when the Death function is called, the TrackPlayer function stops, so the camera can be lacked in place. Here is my script:
using UnityEngine; using System.Collections;
public class camera : MonoBehaviour { public float xMargin = 1f; public float yMargin = 1f; public float xSmooth = 8f; public float ySmooth = 8f; public Vector2 maxXAndY; public Vector2 minXAndY;
private Transform player;
void Awake ()
{
player = GameObject.FindGameObjectWithTag ("Player").transform;
}
bool CheckMargin()
{
return Mathf.Abs (transform.position.x - player.position.x) > xMargin;
}
bool CheckMargin1()
{
return Mathf.Abs (transform.position.y - player.position.y) > yMargin;
}
void FixedUpdate ()
{
TrackPlayer ();
}
void TrackPlayer ()
{
float targetX = transform.position.x;
float targetY = transform.position.y;
if(CheckMargin());
targetX = Mathf.Lerp(transform.position.x, player.position.x, xSmooth = Time.deltaTime);
if(CheckMargin());
targetY = Mathf.Lerp(transform.position.y, player.position.y, ySmooth = Time.deltaTime);
targetX = Mathf.Clamp(targetX, minXAndY.x, maxXAndY.x);
targetY = Mathf.Clamp(targetY, minXAndY.y, maxXAndY.y);
transform.position = new Vector3(targetX, targetY, transform.position.z);
}
public void Death()
{
float targetX = -9.57f;
float targetY = -18.09f;
float targetZ = -5.0f;
transform.position = new Vector3(targetX, targetY, targetZ);
audio.Stop ();
}
}
Answer by THDigi · Feb 17, 2014 at 03:42 AM
You just need to know if your object is dead, when Death() occurs just memorize that and check it in TrackPlayer().
Like this:
private boolean dead = false;
void TrackPlayer ()
{
if(dead)
return;
//...
}
public void Death()
{
dead = true;
//...
}
Also... your 'if(CheckMargin());' conditions are useless, they don't check anything because you added a ; and it stops there.
Answer by Owen-Reynolds · Feb 17, 2014 at 01:35 AM
This is just basic programming. Simplest way to tell stuff to other people is a flag. Any decent intro to programming book will have more examples.
The trick is, have FixedUpdate only track the player if they aren't dead. To know if someone is dead, have the Death function write it somewhere. It's like raising the flag for the mailman:
bool alive = true; // the flag
FixedUpdate() {
if(alive)
TrackPlayer();
}
Death() {
....
alive=false;
}
Hi Owen,
How about with conditions such as charging an attack and dying whilst charging?
if (playerID == PlayerID.PLAYER_ONE && isAlive)
{
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.$$anonymous$$))
{
ChargeUniqueAttack();
Debug.Log(chargeTime.ToString());
chargeTime -= Time.deltaTime;
}
if (chargeTime > 0 && Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.$$anonymous$$))
{
chargeTime = 3;
baseBallBatSwing.SetActive(false);
}
else if (chargeTime < 0 && Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.$$anonymous$$))
{
UnleashUniqueAttack();
}
}
To be more specific with what I'm after, I want to effectively trigger the second if statement on death without having to let go of the button and cancel the first if statement (charging).
You might try searches on things like "Unity charging attack," and also follow the "other similar searches" suggestions. It was a commonly asked Q. The trick is thinking about the frame after pressing the button: how do you know it's in charge mode? You don't need a flag like isCharging
, but sometimes that can help make it more readable -- the whole thing can be "if not charging, else if charging." But you should find many many examples.
Your answer
Follow this Question
Related Questions
not all code paths return a value? 1 Answer
How do I convert this into C# and where should this be added? 1 Answer
Boolean set to true, false in Update() function because of Start() 1 Answer
onEnable & onDisable & onCollision never get called i have no idea y ?! 1 Answer
How to rotate camera 90 degrees but still keep control orientation in 2.5d? 0 Answers