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 UnityAlexV · Nov 12, 2020 at 09:20 AM · oncollisionenterdelayframe

Ball is colliding further and further forward despite the math showing that it shouldn't?

I know the title is confusing but so is the question haha. Anyway, the scenario is this:

  • A ball is moving forward (positive Z axis) at 12 units/second

  • Every 24 units is a platform, which the ball lands on and is given an upward velocity of 10 units/second (the gravity is -10 units/second, therefore the ball is in the air for 2 seconds and will travel the whole 24 units between platforms)

  • Every 10 platforms (determined by a score that is increased every time a platform is hit), the speed of the ball increases by 3 units/second (12, 15, 18, etc.)

  • Additionally, the upward velocity the ball is given when it hits a platform is actually calculated by 120 / current speed (mathematically this will keep the ball moving 24 units between each platform)

  • The platforms are all the same height and should be collided with at the same interval

  • The platforms have a kinematic rigidbody (which is turned non kinematic when it is collided with so that it can fall)

  • Both the ball and the platforms have no friction on their colliders

Math: alt text

alt text

Code (there's also an update function and a few more variables in this script but they don't pertain to the question):

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class BallScript : MonoBehaviour
 {
     public Rigidbody rb;
     public GameObject scoreComponent;
     public float currentSpeed = 12;
     public int newScore = 0;
     
     void Start()
     {
         rb = GetComponent<Rigidbody>();
         rb.velocity = new Vector3 (0, 0, currentSpeed);
         currentSpeed = 12;
     }
     
     
     void OnCollisionEnter(Collision collision)
     {
         if ((collision.gameObject.name == "Platform" || collision.gameObject.name == "FalsePlatform") && transform.position.y >= 0.58)
         {
             
             newScore += 1;
             currentSpeed = (Mathf.Floor(newScore / 10) * 3) + 12;
             rb.velocity = new Vector3 (0, 120 / currentSpeed, currentSpeed);
             print(newScore + " " + transform.position);
             collision.gameObject.GetComponent<Rigidbody>().isKinematic = false;
             collision.gameObject.GetComponent<Rigidbody>().velocity = new Vector3 (0, -10, 0);
             scoreComponent.GetComponent<ScoreDisplayScript>().score = newScore;
         }
     }
 }
 

The problem is that, eventually, the ball begins moving a little bit faster than the platforms and on each successive platform is relatively farther and farther forward until it falls off the front. Specifically, the Z of the position vector of the ball printed at the end increases by the correct amount each time until the score is 20 (starts at 11.3, then 35.3, 59.3, 83.3... until the score of 20 prints 467.3 and then 21 prints 491.4, then 515.5, 539.6, 563.7...) I'm no expert at Unity but I have absolutely no idea what could be causing this. My guess is it has to do with a possible delay on the onCollisionEnter function or the inaccuracy of the float values (although I would assume the number would have to be a lot larger for that to be an issue). I understand that this is probably a stumper and I really appreciate any input that can be given.

math1.png (42.9 kB)
math2.png (43.2 kB)
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

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

210 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

Related Questions

Oncollisionenter and time,OnCollisonEnter and time. 0 Answers

Player keeps moving after key up 1 Answer

Fix sound delay 6 Answers

Preloading all scenes to remove scene switch delay 1 Answer

Rewarded video takes 3+ clicks to load 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