Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by mgoliveri6 · Jan 19, 2020 at 01:09 AM · scripting problemfunctionsbooleans

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);
     
 }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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
 }

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image mgoliveri6 · Jan 19, 2020 at 02:26 PM 0
Share

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 :)

avatar image mgoliveri6 · Jan 19, 2020 at 02:39 PM 0
Share

I figured it out never $$anonymous$$d! Thank you so much for the reply!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

211 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges