Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 SuperCrusher22 · Aug 15, 2021 at 12:00 AM · bounce

How to make this happen when object collides with another?

I have a script that I found while searching for a way to make a ball bounce whenever it hits a jump pad. Although this script works for the bouncing, I need a way for it to only bounce when it hits the jump pad. Here is the script:

 using UnityEngine;
 using System.Collections;
 
 public class bouncer : 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
     }
 }
 
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 The-Peaceful · Aug 15, 2021 at 12:48 PM

Hi @SuperCrusher22 ,

You probably just have to give the jump pads a tag (in the code below: "JumpPad") and then ask if the colliding object has that tag, like so:

      void OnTriggerEnter(Collider col)
      {
          if(col.CompareTag("JumpPad")){
             enter = true;
             TempVelocity = Velocity; // velocity back up
          }
      }
      void OnTriggerStay(Collider col)
      {
          if(col.CompareTag("JumpPad")){
             enter = true;
          }
      }
      void OnTriggerExit(Collider col)
      {
          if(col.CompareTag("JumpPad")){
             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
          }
      }
Comment
Add comment · Show 1 · 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 SuperCrusher22 · Aug 15, 2021 at 02:18 PM 0
Share

Ok. But, whenever I run the script again it bounces whenever the scene loads..

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

124 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

Related Questions

How do you keep a bouncing ball from "breaking" through cloth 1 Answer

Help me bounce (make player bounce when landing from a jump) 0 Answers

pong question 2 Answers

Objects bouncing when change texture 0 Answers

[Closed]My house object bouncing a little when collide with Plane floor 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