Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 dianlosniko · Feb 16 at 01:11 PM · 3dvector3ball

Using Vector3 with multiple directions

Hello! I am currently making a 3d Pong game for an assignment using Unity. However, I have run into an issue with my ball movement using Vector3 & Rigidbody, It can only move in one direction. At the same time. It can't go both left and up at the same time, However it just goes in one direction before hitting a surface and then bounces back in the opposite direction, the only time it's height changes is if I hit it with the top or bottom of my paddles.

Code being used for the ball: using System.Collections; using System.Collections.Generic; using UnityEngine;

 public class Balls : MonoBehaviour
 {
     public int Speed = 100;
 
     Rigidbody Rigidbody;
 
 
     Vector3 velocity;
 
     void Start()
     {
         
         Rigidbody = GetComponent<Rigidbody>();
         Rigidbody.velocity = Vector3.right * Speed;
         
 
 
     }
 
 
     void FixedUpdate()
     {
 
         Rigidbody.velocity = Rigidbody.velocity.normalized * Speed;
         velocity = Rigidbody.velocity;
     }
 
     public void OnCollisionEnter(Collision collision)
     {
         Rigidbody.velocity = Vector3.Reflect(velocity, collision.contacts[0].normal);
     }
 }
 

Any help is appreciated!

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
2
Best Answer

Answer by jackmw94 · Feb 16 at 01:47 PM

I think it makes perfect sense that this happens, since you're starting your ball at a 90 degree angle from the paddle it'll be reflected back out the exact way it came.

Given that you're bouncing off the paddle using reflection, the ball will maintain the same heading throughout the game. This isn't necessarily a problem but I think you might get a more pong-like game by starting your ball's velocity in a direction that isn't perpendicular to the paddle:

 Rigidbody.velocity = new Vector3(1f, 0.5f, 0.5f).normalized * Speed;


To add variation you could randomise the direction in which the ball starts. Happy to lend a hand with implementing that should you want to but I'll wait to see if this answer is any help first!

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 dianlosniko · Feb 16 at 01:55 PM 0
Share

It moves correctly now, thank you! However now it doesn't collide with anything, so I'll have to figure that one out to

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

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

i'm new to unity. I want to make a script that creates and throws a ball. You can't throw the ball again until you pick it up. My throwing part works but the pick up action doesn't 1 Answer

Point game object to another only on the Local X Axis 0 Answers

TPS Camera roll ball 0 Answers

Player doesn't shoot ball object forward like I tell him 2 Answers

How to apply equal force back to player when colliding with a wall? 2 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