• 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 TicTac · Apr 22, 2013 at 05:42 PM · cubeballbounce

How do I get a sphere to bounce of a cube at an angle

I am trying to create a pong style game and I am having trouble getting my ball to bounce off the paddle at angles so that it doesn't just move up and down against the AI.

The ball has a sphere collider and rigidbody and the paddle just has the Box Collider and no rigid body, I'm not sure if I need to add one? Here is the code for the ball

 #pragma strict
 
 //Variable to hold paddle speed
 var speed:int = 10;
 
 function Update () 
 {
     if(Input.GetKey(KeyCode.Escape)){
         Application.LoadLevel (0);
     }
     //player input
     if(Input.GetButton("LEFT"))
     {
         transform.Translate(Vector3(-speed,0,0) * Time.deltaTime);
     }        
     
     if(Input.GetButton("RIGHT"))
     {
         transform.Translate(Vector3(speed,0,0) * Time.deltaTime);
     }
     if(transform.position.x <-7.88)
     {
         transform.position.x = -7.88;
     }
     if(transform.position.x >7.88)
     {
         transform.position.x = 7.88;
     }
  }

This for the paddle (Just trying to give you an idea of the setup.

 #pragma strict
 
 var hasreset = false;
 var cSpeed:float = 10.0;
 var sFactor:float = 10.0;
 //two vars to hold score
 static var playerScore:int = 0;
 
 function Start () 
 {
     hasreset = true;
     playerScore = 0;
     yield WaitForSeconds (1.0);
     rigidbody.AddForce(Random.Range(-5,5),0,0);
 }
 
 function resetball ()
 {    
     yield WaitForSeconds (1.0);
     GameObject.FindWithTag("paddle").transform.position.x = 0;
     GameObject.FindWithTag("paddle2").transform.position.x = 0;
     transform.position.x = 0;
     transform.position.y = 0;
     hasreset = true;
 }
 function takescore ()
 {
     yield WaitForSeconds (1.0);
     playerScore --;
 }
 function addscore ()
 {
     yield WaitForSeconds (1.0);
     playerScore ++;
 
  }
  
 function Update () 
 {
     OnTriggerEnter()
     //this has something to do with smoothing (MATHS)
     var cvel = rigidbody.velocity;
     var tvel = cvel.normalized * cSpeed;
     rigidbody.velocity = Vector3.Lerp(cvel,tvel,Time.deltaTime * sFactor);
     
     //checking bounds for score and reset pos of ball and paddle
     if(transform.position.y <-5.67)
     {    
         resetball();
         if(hasreset == true)
         {
             takescore();
         }
         hasreset = false;
 
     }
     if(transform.position.y >5.67)
     {
         resetball();
         if(hasreset == true)
         {
             addscore();
         }
         hasreset = false;
     }
 }
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 robertbu · Apr 22, 2013 at 07:13 PM

There are syntax errors in the second script above. OnTriggEnter() is nested inside of Update(). And I don't see that the smoothing code is necessary. You have the scripts "named" backwards...the first script is for the paddle, the second for the ball. Is this your code or something you borrowed elsewhere?

One way that may get you what you want in a way that feels realistic is to impart part of the velocity of the paddle to the ball. See the OnCollisionExit() in the following modification of your code:

 #pragma strict
 
 var hasreset = false;
 var cSpeed:float = 10.0;
 var sFactor:float = 10.0;
 //two vars to hold score
 var frictionTransfer : float = 0.15;
 static var playerScore:int = 0;
  
 function Start () 
 {
     hasreset = true;
     playerScore = 0;
     yield WaitForSeconds (1.0);
     //rigidbody.AddForce(Random.Range(-5,5),0,0);
 }
  
 function resetball ()
 {   
     yield WaitForSeconds (1.0);
     GameObject.FindWithTag("paddle").transform.position.x = 0;
     GameObject.FindWithTag("paddle2").transform.position.x = 0;
     transform.position.x = 0;
     transform.position.y = 0;
     hasreset = true;
 }
 function takescore ()
 {
     yield WaitForSeconds (1.0);
     playerScore --;
 }
 function addscore ()
 {
     yield WaitForSeconds (1.0);
     playerScore ++;
  
  }
  
 function OnCollisionExit(collision : Collision) {
      if (collision.collider.tag == "paddle") {
          rigidbody.velocity.x += collision.gameObject.GetComponent(Paddle2).velocity * frictionTransfer;
     }    
 }
  
 function Update () {
 
     //checking bounds for score and reset pos of ball and paddle
     if(transform.position.y <-5.67)
     {  
        resetball();
        if(hasreset == true)
        {
          takescore();
        }
        hasreset = false;
  
     }
     if(transform.position.y >5.67)
     {
        resetball();
        if(hasreset == true)
        {
          addscore();
        }
        hasreset = false;
     }
 }

And the calculation of the velocity in this paddle code:

 #pragma strict
 
 //Variable to hold paddle speed
 var speed:int = 10;
 
 private var lastPosX : float;
 public var velocity = 0.0f;
  
 function Update () 
 {
     if(Input.GetKey(KeyCode.Escape)){
        Application.LoadLevel (0);
     }
     //player input
     if(Input.GetKey(KeyCode.LeftArrow))
     {
        transform.Translate(Vector3(-speed,0,0) * Time.deltaTime);
     }  
     
     if(Input.GetKey(KeyCode.RightArrow))
     {
        transform.Translate(Vector3(speed,0,0) * Time.deltaTime);
     }
     if(transform.position.x <-7.88)
     {
        transform.position.x = -7.88;
     }
     if(transform.position.x >7.88)
     {
        transform.position.x = 7.88;
     }
     
     velocity = (transform.position.x - lastPosX) / Time.deltaTime;
     lastPosX = transform.position.x; 
  }

Note the OnCollisionExit() code requires the paddles have the 'paddle' tag set for both.

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 TicTac · Apr 22, 2013 at 07:22 PM 0
Share

That smoothing code was borrowed and also those fragments of weird code was me just trying stuff out :P. Ill give your changes a try thanks!

avatar image TicTac · Apr 22, 2013 at 07:33 PM 0
Share

I am getting an unknown identifier error on the oncollision exit part BCE0005: $$anonymous$$ identifier: 'Paddle2'.

avatar image robertbu · Apr 22, 2013 at 09:29 PM 0
Share

On line 41 above, you need to replace "Paddle2" with whatever you've name your script that handles the paddle.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

12 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

Related Questions

Everytime ball goes to new plane it bounces 2 Answers

Bounce when hitting wall 1 Answer

How can I show trajectory of a bouncing ball OnCollisionEnter/OnCollisionExit? 0 Answers

How to reduce bounce? 1 Answer

How To Make Ball Bounce On Touch 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges