• 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 anthony · Sep 24, 2010 at 11:57 AM · directionchangepong

Question : Pong(ball to change direction)

hello I am creating this pong game to train on but I am faced with a problem that I can't solve. I read everywhere but no one really helped a lot.They told me to find the distance but then what?? So here is my problem :

How can i make my ball change direction depending on the place it hits the cube. For example if the ball hits it from down it changes direction accordingly.Here is my code till now

var other : Transform; var dist :int; function Update () {

if (other) { dist = Vector3.Distance(other.position, transform.position); print ("Distance to other: " + dist); }

}

function OnTriggerEnter(collisionInfo : Collider) { if(collisionInfo.gameObject.tag == "player1") { //transform.position.x = 1130.362; rigidbody.velocity = Vector3.left * 4000;

}

if(collisionInfo.gameObject.tag == "player2") { //transform.position.x = -1130.362; rigidbody.velocity = Vector3.right * 4000;

}

}

thank you

Comment
Add comment · Show 3
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 spinaljack · Sep 24, 2010 at 03:32 PM 0
Share

There's no Vector3.left, only up, forward, right, one and zero. If you want to go left you type -Vector3.right with a $$anonymous$$us

avatar image efge · Sep 24, 2010 at 03:59 PM 0
Share

Atari is the owner of all rights on Pong. Please be aware of when using this name :-)

avatar image anthony · Sep 24, 2010 at 05:31 PM 0
Share

okay thanks guys but there is just one issue! the ball is slowing down although the bouncyness is 1 and the bounce is set to maximum. I tried adding that when we have collision to give it a force but it didnt work(rigidbody.AddForce)! do you have any idea guys thanks!

3 Replies

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

Answer by skovacs1 · Sep 24, 2010 at 03:31 PM

I'm not really sure why your script is trying to do things the hard way, but I'll try to give you the simplest advice that I can to making a "pong" game. You say people told you to find a distance, but I'm not sure why you would need that - what sort of questions did you ask?

Physics = Pong

Unity's physics system can do most of the work for you as you may have noticed since you're using a rigidbody. If the paddles and the walls have colliders and your ball has a rigidbody, then your Pong game is almost done. Create a physic material with bounciness set to 1 and no friction and apply it to everything. Write a script to position and start your ball moving with AddForce or by setting velocity once when the player in possession of the ball fires it (and when you first start if a player doesn't start in possession of the ball). Add triggers to the ends of your play area with scripts to manage scoring and who has control of the ball when someone scores). You have Pong.

Pong la mode

Not using physics to do the work, you would actually have to think and do some setup, but Pong's rather easy. Assuming everything is the same as above, except you have no rigidbody or Physic material and you are moving the ball yourself, all you would need different is the ball's movement/trigger script and to make some assumptions about your setup.

var velocity : Vector3;

function Update() { transform.Translate(velocity * Time.deltaTime, Space.World); }

function OnTriggerEnter(other : Collider) { //All collision objects must be oriented to face the play area velocity = Vector3.Reflect(-velocity, other.transform.forward); }

That's kind of boring though, as the speed of the paddles at the time of impact isn't really taken into account, so let's add that.

var velocity : Vector3;

function Update() { transform.Translate(velocity * Time.deltaTime, Space.World); }

function OnTriggerEnter(other : Collider) { //All collision objects must be oriented to face the play area velocity = Vector3.Reflect(-velocity, other.transform.forward);

 //paddles are tagged player, but named differently
 if(other.gameObject.tag == "player") {
     //Paddles have a script called PlayerControl which contains their velocity
     var script : PlayerControl = other.GetComponent(PlayerControl);
     velocity += script.velocity;
 }

}

Comment
Add comment · Show 7 · 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 anthony · Sep 24, 2010 at 05:32 PM 0
Share

okay thanks guys but there is just one issue! the ball is slowing down although the bouncyness is 1 and the bounce is set to maximum. I tried adding that when we have collision to give it a force but it didnt work(rigidbody.AddForce)! do you have any idea guys thanks! anthony 0 secs ago

avatar image skovacs1 · Sep 27, 2010 at 02:00 PM 0
Share

Did you apply the Physic material to everything?

avatar image anthony · Sep 27, 2010 at 07:45 PM 0
Share

yeah i did! anyway im still try to see what the problem is but i guess its the only way i learn!

avatar image skovacs1 · Sep 28, 2010 at 02:02 PM 0
Share

Is drag turned off? If not, your object will slow down as it moves.

avatar image skovacs1 · Sep 28, 2010 at 02:16 PM 0
Share

As a test, I've a simple scene. 1. Create a sphere and two cubes, one above the sphere and one below. 2. Add a bounciness=1 material to all three. 3. Add a rigidbody to the sphere and turn off gravity. 4. Add the script 'function Start(){rigidbody.AddForce(Vector3.up*1000);}function FixedUpdate(){Debug.Log(rigidbody.velocity);}' to the sphere. The ball bounces perpetually without losing velocity (as noted in the log). Your Pong is just an extension of this simple setup, so I imagine that your problem stems from something simple like missing the physic material on something.

Show more comments
avatar image
0

Answer by JaredRollo · Sep 11, 2012 at 07:13 PM

How do i put that in here like where would i put it in this script and is that all i have to do ?

using UnityEngine; using System.Collections;

public class BallMovement : MonoBehaviour {

// Use this for initialization void Start () { } // Update is called once per frame void Update () { } }

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
avatar image
0

Answer by spinaljack · Sep 24, 2010 at 03:37 PM

You can easily make the game using physics and rigid bodies so long as the ball doesn't move too fast the the walls aren't too thin.

You can set the movement to a single plane by constantly setting z velocity to 0 on the ball and set the rigid body to have 0 drag and no gravity and freeze rotation to be on the safe side.

Then you set all the bounce properties of every material to maximum bounce and 1 bounciness to make sure the ball doesn't slow down with each hit.

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 spinaljack · Sep 24, 2010 at 03:38 PM 0
Share

what skovacs1 said :)

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

1 Person is following this question.

avatar image

Related Questions

Change the direction of velocity upon tapping a button!!?? 1 Answer

change ball direction when hitting paddle 1 Answer

Change rigidbody direction 1 Answer

how to change orbit direction when button clicked? 0 Answers

addForce (IMPRECISION?) object changing direction after a few frames of launch... 6 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