Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
  • Help Room /
avatar image
1
Question by Andrew15821688 · Feb 17, 2014 at 12:52 AM · functionstop

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

}

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · 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
0

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;
 }
Comment
Add comment · Show 3 · 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 Harvizl · Jul 25, 2018 at 02:29 PM 0
Share

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();
             }
         }
avatar image Harvizl Harvizl · Jul 25, 2018 at 02:32 PM 0
Share

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

avatar image Owen-Reynolds Harvizl · Jul 25, 2018 at 03:07 PM 0
Share

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

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

22 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

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


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