Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 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.

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

Moving object disappears then comes back 1 Answer

Sprite will not roate when arrow keys are pressed 2 Answers

How to know if my player is not moving? 2 Answers

problem with twin stick shooter controls 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