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 /
avatar image
0
Question by RonDQuigley · Oct 20, 2015 at 07:06 PM · c#rigidbodyunity5time

On Collision Enter Method Stuck With Slow Motion Time

So I've been trying to get time slow down (and speed ups too) working on a project I'm doing but I've hit a wall. If time has been slowed, the on collision enter method I'm using below (for in this case just testing a bouncing sphere) gets stuck (I suspect it has to do with when the object has been slowed the method thinks the object is colliding with it again). Here's the code below:

 using UnityEngine;
 using System.Collections;
 
 public class testTime2 : MonoBehaviour {
 
     private Rigidbody rb;
     private bool keyPressed; 
 
     void Start() {
         rb = GetComponent<Rigidbody>();
     }
 
     void FixedUpdate()
     {
         if (Input.GetKey(KeyCode.Space))
         {
             keyPressed = true;
             rb.velocity = rb.velocity * Time.deltaTime * 0.2f;
         }
         else
             keyPressed = false;
     } 
 
     void OnCollisionEnter() // method'stuck' with a slow object velocity
     {
         rb.AddForce(transform.up * 7f, ForceMode.Impulse);
         // my attempt to workaround it (doesn't work)
         if (keyPressed)
             rb.velocity = rb.velocity * Time.deltaTime * 1.0f; 
         else
             rb.velocity = rb.velocity * Time.deltaTime * 0.2f;
     }
 }
 

I thought maybe a workaround of temporarily pushing the velocity back up would fix it, but I had no luck. Visually it looks like this:

https://drive.google.com/file/d/0B1wVSPywFDMaMmNCWWN3SGcyYkE/view?usp=sharing

[Here I press the spacebar as it reaches the floor for a slowdown, the spehere position keeps popping, then I let go of the spacebar for velocity to return to normal (which fixes it)]

I know there's a couple of other posts about slowing down time on specific objects but I was hoping that a simple velocity change would do it (in this case it isn't cutting it though). Any ideas on how I could get this to work? I know there's Time.scale but that'll effect all rigidbodies, here I'm doing it on a case by case basis.

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 rageingnonsense · Oct 21, 2015 at 10:42 PM 0
Share

Try: http://docs.unity3d.com/ScriptReference/Time-timeScale.html

This will ACTUALLY slow down time, ins$$anonymous$$d of what you are doing messing with velocities.

avatar image RonDQuigley rageingnonsense · Oct 22, 2015 at 08:10 AM 0
Share

Yep I've tried Time.timeScale (I actually meant that ins$$anonymous$$d of Time.scale in my original post), but Time.timeScale slows every rigidbody down so it's out the window.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by toromano · Oct 22, 2015 at 08:23 AM

If you are bouncing the ball just on planar or rectangular surfaces i would recommend you to implement custom bouncing ball physics. Someting like:

 using UnityEngine;
 using System.Collections;
 
 public class Test : MonoBehaviour
 {
 
     private Rigidbody rb;
     private bool enter = false;
     private bool exit = false;
 
     public Vector3 Velocity = Vector3.zero;
     private Vector3 Gravity = new Vector3(0.0f, -10.0f, .0f);
     private float mass = 1.0f;
     private Vector3 Impulse = new Vector3(0.0f, 500.0f, 0.0f);
     private Vector3 acceleration;
     private Vector3 TempVelocity;
     public float timeMultipler = 1.0f;
 
     void Start()
     {
         rb = GetComponent<Rigidbody>();
     }
 
     void FixedUpdate()
     {
         if (enter)
         {
             acceleration = Gravity + Impulse / mass;
             enter = false;
         }
         else
         {
             acceleration = Gravity;
         }
 
         Velocity += acceleration * Time.fixedDeltaTime * timeMultipler;
         transform.position += Velocity * Time.fixedDeltaTime * timeMultipler;
     }
 
     void OnTriggerEnter()
     {
         enter = true;
         TempVelocity = Velocity; // velocity back up
     }
     void OnTriggerStay() 
     {
         enter = true;
     }
     void OnTriggerExit()
     {
         exit = true;
         Velocity = new Vector3(TempVelocity.x, -TempVelocity.y * 0.9899999f, TempVelocity.z); //magic number for perfect bounciness 0.9899999f // you can modify if you need to
     }
 }

For this script to work, you need to make your rigidbody kinematic and make your sphere collider trigger. You can modify timeMultipler for slowing down. Also you can modify x and z velocity components.

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 RonDQuigley · Oct 22, 2015 at 08:34 AM 0
Share

Hey this looks like a really good solution! I'll give it a shot later. The only thing I'm thinking here is that how would I allow the player to interact with an object that has time slowed down applied to it with your script? I should mention I'm using this with the leap motion and the plan was to have objects slowed down so they could be interacted with easier i.e. this bouncy sphere was maybe a switch that the user needed to press if that makes sense?

avatar image toromano RonDQuigley · Oct 22, 2015 at 08:44 AM 0
Share

If you want to hit a slowing down sphere, you can add impulse as a did in that line:

  if (enter)
  {
      acceleration = Gravity + Impulse / mass;
      enter = false;
  }




avatar image RonDQuigley toromano · Oct 24, 2015 at 01:27 PM 0
Share

Just to follow up and say that this totally worked for me. Thank you!

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

36 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Cronómetro 1 Answer

How to not allow ball to be thrown after first Toss 2 Answers

Returning an IEnumerator as an int? 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