Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
This question was closed May 08, 2018 at 08:14 AM by polan31 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by polan31 · May 05, 2018 at 02:38 PM · collisioncoroutineforceienumeratorseconds

How to change the force for seconds using a Coroutine?

2D C#

I have a simple and probably very stupid question, which is why I apologize for any possible littering of the forum.

How to make the object's ("Player") forceFly increase by 1000 when it hit the object: "BrownWorm" (in OnCollisionEnter2D), for 5 seconds, then return to normal?

My main problem is the lack of assigned value to fireFly, because I want to specify it in the inspector.

I know that to return after 5 seconds I should add IEnumerator and yield, but I do not know know how to start this power change.

I would also be grateful if someone in this example would explain how it works,

My script:

         using System.Collections;
         using System.Collections.Generic;
          using UnityEngine;
 
 public class PlayerBehaviour : MonoBehaviour {
 
     public Transform mesh;
     public float forceFly;
     private Animator animatorPlayer;
 
     private float currentTimeToAnim;
     private bool inAnim;
 
     // Use this for initialization
     void Start () {
         animatorPlayer = mesh.GetComponent<Animator>();
 
         this.gameObject.GetComponent<Rigidbody> ();
     }
     
     // Update is called once per frame
     void Update () {
 
         if (Input.GetMouseButtonDown (0)) {
             
             inAnim = true;
 
             GetComponent<Rigidbody2D> ().velocity = Vector2.zero;
             GetComponent<Rigidbody2D> ().AddForce (new Vector2 (0, -1) * forceFly);
 
         }
 
         if (inAnim) {
             currentTimeToAnim += Time.deltaTime;
 
             if (currentTimeToAnim > 0.4f) {
                 currentTimeToAnim = 0;
                 inAnim = false;
 
             }
         }
 
         animatorPlayer.SetBool ("callFly", inAnim);
     }
         void OnCollisionEnter2D(Collision2D coll){
         if (coll.gameObject.tag == ("Enemy"))
 
                 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

2 Replies

  • Sort: 
avatar image
0
Best Answer

Answer by polan31 · May 08, 2018 at 08:13 AM

Problem solved by myself. I added some variables false, true and everything works.

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 bhavinbhai2707 · May 05, 2018 at 03:02 PM

Simple!!

      public float forceFlyAdd;
          public bool isFaster;
      
          void OnCollisionEnter2D(Collision2D coll){
              if (coll.gameObject.tag == ("Player"))
                  isFaster = true;
                  Debug.Log ("Speed Powerup");
                  Destroy (gameObject);
                  PlayerBehaviour theSpeed = coll.gameObject.GetComponent<PlayerBehaviour> ();
                  float temp = theSpeed.forceFly;
                  theSpeed.forceFly = forceFlyAdd;
                  StartCoroutine ("WaitTime", theSpeed);
                  theSpeed.forceFly = temp;
              }
      
      
          public IEnumerator WaitTime(PlayerBehaviour playerForceFly) {
              yield return new WaitForSeconds (5);
              isFaster = false;
          }
      }
 

I made the changes near startCoroutine!!

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

Follow this Question

Answers Answers and Comments

81 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

Related Questions

Mecanim animation, playing once 1 Answer

Play animation then destroy gameOject C# "SOLVED" 1 Answer

How to end a coroutine early 0 Answers

Unwanted rotation 0 Answers

WaitForSeconds is breaking out of my IEnumerator 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