• 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 /
  • Help Room /
avatar image
0
Question by Jussumman · Jan 01, 2016 at 03:44 PM · transform.forward

How to make object move forward and up simultaneously

I have an object like a hot air balloon spawned and moving forward, now I want that object to rise (go up or down) at the same time, but when I try to use both lines of code below it only accepts one of the directions and ignores the other. How can I make the object move from left to right and up at the same time? My search here found related question with answer to use Quaternion.LookRotation but I don't want to rotate my character (spawned objects) I just need (as example) a hot air balloon going sideways with the wind and then goes up higher using a set speed. Is LookRotation really the way?

 GetComponent<Rigidbody>().velocity = Vector3.forward * speed;

 GetComponent<Rigidbody>().velocity = Vector3.up* speed;
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
1

Answer by NoseKills · Jan 01, 2016 at 05:12 PM

So .velocity is a vector. It has components .x .y and .z to indicate movement in all directions in 3D space. What you are doing now is setting that vector to 2 different values one after the other and only the latter applies like you have noticed

 GetComponent<Rigidbody>().velocity = new Vector3(0, 0, 1)* speed;
 GetComponent<Rigidbody>().velocity = new Vector3(0, 1, 0) * speed;

What you want to do is set the value to be a vector that has the correct values for each component

 Vector3 v = (Vector3.up + Vector3.forward) * speed;
 GetComponent<Rigidbody>().velocity = v;
 
 // or 
 
 GetComponent<Rigidbody>().velocity = new Vector3(0, speed, speed);

write it any way you want as long as you set .y and .z to the value you want. Also not that the way you are doing it would result in the balloon traveling faster when it's going up or down since the length of a vector (0,speed,speed) is longer than that of (0,0,speed). To solve that you should normalize the vector (make its length 1) before multiplying it with speed

  Vector3 v = (Vector3.up + Vector3.forward).normalized * speed;
  GetComponent<Rigidbody>().velocity = v;
     
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

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

38 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

Related Questions

Transform.Forward help 1 Answer

transform.forward not pointing to the blue axis with raycast 1 Answer

Transform.Forward in 2D 2 Answers

using LookAt(), transform.forward but move only in X axis 0 Answers

Gun script is shooting wrong direction,Script is shooting wrong direction 1 Answer


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