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 Ab30657 · Jan 10, 2018 at 12:25 PM · collisionvector3velocitybeginnereffect

Wht isn't the sphere moving and rebounding properly?

I just moved into learning Unity, I want to make a ping pong typed game, so I have one cubical player that will try to stop the ball from going beyond it. In order to make that, I need a ball moving continuously within the field created using 4 rigid body cubes. In start Sphere is made to move in random direction with a particular speed but once it rebounds to the wall it does not rebound Elastically(not even it rebounds inelastically). So My code must be missing something. Also to note the sphere has been attached with Physics Material for bounciness with minimum friction and maximum bounciness.

  using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;

 public class BallMotion : MonoBehaviour {
  public float speed;
  private Rigidbody Ball;
  Vector3 direction;
  Vector3 oldvel;
  bool reflect;
  int counteradd;

 // Use this for initialization
 void Start () {

     Ball = GetComponent<Rigidbody> ();
     float rad = 0.0f;
     while (rad==0.0f){
         rad = Random.Range (0, 2 * Mathf.PI);
     }
     direction = new Vector3 (Mathf.Cos (rad), 0.0f, Mathf.Sin (rad));
     direction.Normalize ();
     direction *= speed;


 }
 
 // Update is called once per frame
 void Update () {
     
 }
 void FixedUpdate(){
     oldvel = Ball.velocity;
 }
 void OnCollisionEnter(Collision c){
     if (counteradd < 1) {
         if (c.gameObject.tag == "Wall") {
             reflect = true;
         }
         ContactPoint cp = c.contacts [0];
         Ball.velocity = Vector3.Reflect (oldvel, cp.normal);

         Ball.velocity += cp.normal ;
         counteradd++;
     }
 }
 }

Here is Picture of what my game field looks like.

alt text

image.png (21.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

1 Reply

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

Answer by sisse008 · Jan 10, 2018 at 12:34 PM

what is this bool reflect? please post all relevant code. any ways you dont need to write any code to make the ball bounce off the wall. unity's physics engine does that for you. and walls should not be rigidbodies, a rigidbody is for a moving gameobject. remove the rigidbody from all the walls and add box colliders (NOT triggered).

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

136 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

Related Questions

(Steam VR / Vive) Rigidbody moving in only one direction after collision 1 Answer

Incorrect velocity after collision 1 Answer

Speed after Vector3.Reflect Decreases,Speed of ball after Vector3.Reflect decreases 0 Answers

Why isn't my move on collision script working? 0 Answers

Making 2D objects solid, so that they do not pass through eachother? 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