Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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 /
  • Help Room /
avatar image
0
Question by unity_LCiN8_AcVB8u8A · Feb 16 at 11:41 AM · crashes

Stack overflow caused by "while" loops

I'm very new to coding, so forgive me if I'm missing something obvious here.


I'm trying to make a system where if the 2D player suddenly changes direction with a certain amount of velocity, it hops them back a bit in the opposite direction, cancels out their velocity, and increases drag for two seconds.


However, every timed iteration I've done trying to get the drag to increase has resulted in instant crashed the moment the function is called into action.


Is there some infinite loop or contradiction I'm missing here? Maybe I have artist's eye and it's right in front of me.


Code (please don't laugh):

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using System;
 
 public class PlayerMovement : MonoBehaviour
 {
     public float moveSpeed = 5f;
     public Rigidbody2D rb;
     public Vector3 velocity = new Vector3(0f/*x*/, 0f/*y*/, 0f/*z*/);
     public Vector2 movement;
     public Vector2 position;
     public int time = 0;
     public int slowtime = 0;
     public int slowtimer = 2;
     public float bumbdecrease = 0f;
     public float bumbdash = 1.0f;
 
     void Start ()
     {
         StartCoroutine( Example() );
     }
 
     void Update()
     {
         movement.x = Input.GetAxisRaw("Horizontal");
         movement.y = Input.GetAxisRaw("Vertical");
     }
 
     IEnumerator Example ()
     {
         yield return new WaitForSecondsRealtime(1);
         time += 1;
     }
 
     void FixedUpdate ()
     {
         velocity = rb.velocity;
         position = rb.position;
         rb.AddForce(movement.normalized);
 
         if (velocity.x > 6 | velocity.x < -6 | velocity.y > 6 | velocity.y < -6)
         {
             if (velocity.x > 6 & movement.x < 0)
             {
                 rb.inertia = 0;
                 velocity.x = -(velocity.x * bumbdecrease);
                 rb.velocity = velocity;
                 position.x = position.x - bumbdash;
                 rb.position = position;
 
                 slowtime = (time + slowtimer);
 
                 do
                 {
                     rb.drag = 12;
                 } while (slowtime > time);
 
                 rb.drag = 6;
             }
             else if (velocity.x < -6 & movement.x > 0)
             {
                 rb.inertia = 0;
                 velocity.x = Math.Abs(velocity.x * bumbdecrease);
                 rb.velocity = velocity;
                 position.x = position.x + bumbdash;
                 rb.position = position;
 
                 slowtime = (time + slowtimer);
 
                 do
                 {
                     rb.drag = 12;
                 } while (slowtime > time);
 
                 rb.drag = 6;
 
             }
 
             if (velocity.y > 3 & movement.y < 0)
             {
                 rb.inertia = 0;
                 velocity.y = -(velocity.y * bumbdecrease);
                 rb.velocity = velocity;
                 position.y = position.y - bumbdash;
                 rb.position = position;
 
                 slowtime = (time + slowtimer);
 
                 do
                 {
                     rb.drag = 12;
                 } while (slowtime > time);
 
                 rb.drag = 6;
             }
             else if (velocity.y < -3 & movement.y > 0)
             {
                 rb.inertia = 0;
                 velocity.y = Math.Abs(velocity.y * bumbdecrease);
                 rb.velocity = velocity;
                 position.y = position.y + bumbdash;
                 rb.position = position;
 
                 slowtime = (time + slowtimer);
 
                 do
                 {
                     rb.drag = 12;
                 } while (slowtime > time);
 
                 rb.drag = 6;
 
             }
         }
     }
 
 }


Comment
Add comment · Show 2
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 andrew-lukasik · Feb 16 at 09:59 AM 0
Share

These StackOverflowExceptions are caused by all these while loops. Stop using them until you familiarize yourself with what they do exactly. Remove them for now and these exceptions will be gone.

avatar image unity_LCiN8_AcVB8u8A andrew-lukasik · Feb 16 at 02:08 PM 0
Share

How would I accomplish the same thing without the while loops? Is my time iteration incorrect?

0 Replies

· Add your reply
  • Sort: 

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

176 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

Related Questions

jni error has occurred local reference table overflow (max=512) Android crashes on start. 1 Answer

[2019.3.12f1 Steam Build] Game loads past Splash Screen, but screen goes black after 2-3 seconds. Have sound, cursor and UI is responsive. 1 Answer

Reopen unity, then all virtual buttons disappear? 0 Answers

can't solve crash on Mac/Win (32 and 64) 0 Answers

Solasta the new Pathfinder game keeps crashing,Solasta and the new Pathfinder keeps crashing 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