- Home /
Call Function with a Bool (C#)
Hello, I'm trying to create a Power-Up system that when you collect an object, an upgrade is applied. I am doing this through two different scripts. I have the player movement script that adds the "Dash" upgrades to the character. I am trying to only activate the function that handles the "Dash" when the object is collected. I am unsure of how to do this. Any help will be greatly appreciated.
To clarify I have a Power-Up script applied to the collectible object, and I have the Character Movement script applied to the player. I am only trying to call one (1) function from the Character Movement script and make it active ONLY when the object is collected, else it is not active.
Character Move Script:
void Start()
{
characterController = GetComponent<CharacterController>();
//powerUp.Pickup();
}
void Update()
{
if (characterController.isGrounded)
{
// We are grounded, so recalculate
// move direction directly from axes
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0.0f, Input.GetAxis("Vertical"));
moveDirection *= speed;
//Point Player in Direction
transform.LookAt(transform.position + new Vector3(moveDirection.x, 0, moveDirection.z));
if (Input.GetKey(KeyCode.W) || (Input.GetKey(KeyCode.S)) || (Input.GetKey(KeyCode.A)) || (Input.GetKey(KeyCode.D)))
{
//anim.Play("LittleRun");
anim.SetInteger("RunIdle", 1);
if (Input.GetButton("Jump"))
{
moveDirection.y = jumpSpeed;
anim.SetInteger("RunJump", 2);
}
else
{
anim.SetInteger("RunJump", 0);
}
}
else
{
//anim.Play("LittleIdle");
anim.SetInteger("RunIdle", 0);
}
//Make Player Jump
if (Input.GetButton("Jump"))
{
moveDirection.y = jumpSpeed;
anim.SetInteger("JumpIdle", 1);
}
else
{
anim.SetInteger("JumpIdle", 0);
}
}
else if (characterController.isGrounded == false)
{
moveDirection.x = Input.GetAxis("Horizontal") * speed;
moveDirection.z = Input.GetAxis("Vertical") * speed;
//Point Player in Direction
transform.LookAt(transform.position + new Vector3(moveDirection.x, 0, moveDirection.z));
DashMove();
}
// Apply gravity. Gravity is multiplied by deltaTime twice (once here, and once below
// when the moveDirection is multiplied by deltaTime). This is because gravity should be applied
// as an acceleration (ms^-2)
moveDirection.y -= gravity * Time.deltaTime;
// Move the controller
characterController.Move(moveDirection * Time.deltaTime);
}
public void DashMove() { //Dash if (Input.GetKey(KeyCode.LeftShift)) { moveDirection.z = Input.GetAxis("Vertical") dashSpeed; moveDirection.x = Input.GetAxis("Horizontal") dashSpeed; dash.Play();
}
}
}
Power-Up Script:
public void Start()
{
characterMove.DashMove();
}
void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Player"))
{
Pickup();
}
}
public void Pickup()
{
//Spawn Cool Effect
Instantiate(pickupEffect, transform.position, transform.rotation);
//Apply Effect to Player
characterMove.DashMove();
// Remove Object
Destroy(gameObject);
}
}
Answer by jkpenner · Jan 19, 2020 at 05:30 AM
You could add bool to the player that checks if you can use your dash action. Then add a public method that can be called from your pickup to set if you can dash or not. Something similar to the following:
private bool canDash = false;
// Can be called by pickups
public void SetCanDash(bool value) {
this.canDash = value;
}
Then you can use that bool in your DashMove method to check if you should handle the dash logic.
public void DashMove() {
// Exit early cause player cant dash yet
if (this.canDash == false)
return;
// Dash Code Here
}
would you be able to tell me where in the pick up script I would be able to call the bool? And what the (value) should be. I vaguely understand what you're trying to say, but I am not that good at booleans yet. Thank you for your reply :)
I figured it out never $$anonymous$$d! Thank you so much for the reply!
Your answer
![](https://koobas.hobune.stream/wayback/20220612221751im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Problems calling functions from Scriptable Objects 0 Answers
Typing anything anywhere in script ceases all functions? 1 Answer
Dragging multiple objects onto a script 1 Answer
Event System Calling Functions but they dont Work (No errors) 1 Answer
What are some reasons my gameObject isn't colliding? 1 Answer