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 /
  • Help Room /
avatar image
0
Question by wongchris · Feb 05, 2020 at 12:05 PM · unity 2dvelocity

slow down certain objects

I want to slow down all the RedBall and BlueBall by half in my game in 11 seconds.(when pros_control==ture) But for unknown reason, it only work in a redball in the game(I want all the balls can work).

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
  
 public class tragger_red : MonoBehaviour
 {
     public GameObject MusicDraw;
     public GameObject redpart;
     public static float startForce = 3f;
     private float distance;
  
     Rigidbody2D rb;
     GameObject[] backgroundObjects1;
     GameObject[] backgroundObjects2;
  
  
  
     private void Start()
     {
         backgroundObjects1 = GameObject.FindGameObjectsWithTag("BlueBall");
         backgroundObjects2 = GameObject.FindGameObjectsWithTag("RedBall");
         rb = GetComponent<Rigidbody2D>();
         rb.AddForce(-(transform.up) * startForce, ForceMode2D.Impulse);
     } 
     IEnumerator wait()
     {
         yield return new WaitForSeconds(11);
     }
     void Update()
     {
         foreach (GameObject f in backgroundObjects2)
         {
             Rigidbody2D rb = f.GetComponent<Rigidbody2D>();
             if (f != null)
             {
                 Vector3 fVelocity = rb.velocity;
                 if (pros_control.ice == true)
                 {
                     Debug.Log("fVelocity=" + fVelocity);
                     float f_vx = rb.velocity.x;
                     float f_vy = rb.velocity.y;
  
                     rb.velocity = new Vector2(f_vx * 0.5f, f_vy * 0.5f);
                     Debug.Log("f.GetComponent<Rigidbody2D>().velocity=" + rb.velocity);
                     StartCoroutine(wait());
                     pros_control.ice = false;
                 }
                 else
                 {
                     rb.velocity = fVelocity;
                 }
             }
         }
         foreach (GameObject g in backgroundObjects1)
         {
             Rigidbody2D rb = g.GetComponent<Rigidbody2D>();
             if (g != null)
             {
                 Vector3 gVelocity = rb.velocity;
                 if (pros_control.ice == true)
                 {
                     Debug.Log("gVelocity=" + gVelocity);
                     float g_vx = rb.velocity.x;
                     float g_vy = rb.velocity.y;
                     rb.velocity = new Vector2(g_vx * 0.5f, g_vy * 0.5f);
                     Debug.Log("g.GetComponent<Rigidbody2D>().velocity=" + rb.velocity);
                     StartCoroutine(wait());
                     pros_control.ice = false;
                 }
                 else
                 {
                     rb.velocity = gVelocity;
                 }
             }
         }
  
  
     }
 }


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
0

Answer by tormentoarmagedoom · Feb 05, 2020 at 12:15 PM

Hello.

Is backgroundObjects1 still empty?

Is the tag correctly writed?

Have the objects the tag assigned?

Is the physics part wrong?

As you can imagine, we can not say what is happening only seeing the code. Thats why when you have problems like this, you MUST debug the code while program is running (look for tutorials if dont know what I'm talking about) and see the state of each varabile in each frame to detect the problem.

Good luck"!!

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

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

198 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

Related Questions

Jump at fixed height on single touch : Unity 2D 0 Answers

Enemys velocity in 2D platformer 0 Answers

Stuck on using if else to prevent ball from getting stuck in brick breaker. very new to unity & C# :(,How would I reference an object to add onto its velocity in 2D? 1 Answer

Unity2d .velocity dash speed 2 Answers

Why is there no Collision2D.Impulse? (Calculating the force needed to break an object) 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