• 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 GamerJose · Jun 12, 2015 at 09:46 PM · c#2dmovementdiagonal

How do I make my balls initial speed make it go in a diagonal direction while still being able to bounce off of walls?

I want my ball to start of with an initial speed of 30, going diagonally, while still being able to bounce off of walls. This ball isnt controlled by the player, it should move diagonally, bounce off of a wall, and keep moving diagonally, in an opposite direction. The bounce Is covered by the physics2d material, which has a bounce setting, but I cant figure out how to make the ball move diagonally. This is my script, which makes it move upwards, while still bouncing:

 using UnityEngine;
 using System.Collections;
 
 public class EnemyMovement : MonoBehaviour {
     public float speed = 30;
     
     void Start() {
         // Initial Velocity
         GetComponent<Rigidbody2D>().velocity = Vector3.up * speed;
     }
 }

I tried to copy paste the code below //Initial Velocity so that there was one for up and one for right, but it only runs right. Any suggestions?

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 Dave-Carlile · Jun 12, 2015 at 09:51 PM

The direction is controlled by the velocity vector. A vector points in a direction in 3D space. Vector3.up has the components x = 0, y = 1, z = 0. The y axis points up in other words. If you want to move right, you need x = 1, y = 0, z = 0, for example. So if you want to move at an angle you need a vector that points in that direction.

To make it simple, moving both up and right gives you a 45 degree angle. We also want to normalize the vector which gives it a length of 1, which then allows you to multiply it by your speed to increase the magnitude and move faster.

So your new vector can be created like:

 Vector3 direction = new Vector3(1, 1, 0);
 direction.Normalize();  

Then replace Vector3.up * speed in your script with direction * speed.

Comment
Add comment · Show 2 · 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 GamerJose · Jun 12, 2015 at 10:34 PM 0
Share

Thank you for the answer, works perfect.

avatar image Dave-Carlile · Jun 13, 2015 at 12:19 AM 0
Share

You're welcome. Feel free to check the check mark to select this answer as solving your question.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Making a bubble level (not a game but work tool) 1 Answer

Question on Sprite and Movement 0 Answers

Better way to move avatar than transform.position? 1 Answer

Simple C# script to move a gameObject toward the direction of the player at the instance of it's spawning. 1 Answer

2D movimentation "Lag" 0 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