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 crocki134 · May 27, 2013 at 06:38 PM · errorgravityrigidbody.velocitycs1612

rigidbody.velocity.x/y/z - Problem

Hi!

I want to move a rigidbody without affecting the gravity-effect. So I tried

 rigidbody.velocity.x = targetVelocity.x;

but I keep getting: "Cannot modify a value type return value of...[]" Some people wrote that it's working for them but how?

BTW:

 rigidbody.velocity = targetVelocity; //targetVelocity is a Vector3 - (1f,0f,0f)

That works, but the object falls very slow. If I change the .Y in targetVelocity to something >1.0f and .useGravity = false, it gets stuck.

Maybe someone can guide me to the point I'm missing?

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

4 Replies

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

Answer by robertbu · May 27, 2013 at 06:42 PM

Try this:

 Vector3 v3 = rigidbody.velocity;
 v3.x = 1.0;
 v3.z = 0.0;
 rigidbody.velocity = v3;

Note if you want to turn gravity off and to simulate standard gravity, use -9.8 for the 'y' value.

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 crocki134 · May 27, 2013 at 06:52 PM 0
Share

Thank you so much! Works perfectly!

avatar image
0

Answer by Zinatulin · Jun 19, 2014 at 11:23 AM

Also you could do:

 rigidbody.velocity = new vector3(targetVelocity.x, rigidbody.velocity.y, rigidbody.velocity.z);

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 Timtimos · Nov 13, 2016 at 01:21 AM 0
Share

thank you very much, your answer is simple and perfect !

avatar image famestar1236 · Oct 04, 2020 at 12:58 PM 0
Share

hey can you please tell me why do you use rigidbody.velcocity.y what is the use of that.

avatar image
0

Answer by Voltran · May 10, 2015 at 05:17 AM

Yeah completely useless comments for a beginner. Issues like these clearly shows what kind if a project managing does unity do for tutorials. Another wasted junk video tutorial on the topic of space shooter game is added to the junk space in internet for this stupid problem. Yet there is no clear definition of guidance to show people how to solve it. Shame on you.

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 jrjr · May 21, 2015 at 12:36 AM 1
Share

There are literally billions of possible questions. Expecting everything to be documented is an impossible feat to achieve. $$anonymous$$ust stuff can be found with google searches. Once you can't find things any more - you know you are getting somewhere.

Program$$anonymous$$gs not something that can be spoon fed. It takes practice, trial, and error. I understand its frustrating from a beginners stand point but on complexity level this is probably level 10 or 11 out of half a million when considering Unity's capabilities.

They're doing amazing.

avatar image
0

Answer by Gaming_geko · Oct 17, 2017 at 06:58 PM

All the other solutions didn't work for me.

amazing solution:

yVel = rb2d.velocity.y; rb2d.velocity = new Vector2(Input.GetAxis("Horizontal"), yVel);

The problem is, it is getting the velocity (for me Y) and setting it, every frame, however the script is getting the velocity (for me y so i'm going to be saying it's y) let's say 1, so it sets it to 1, great right? wrong, gravity is constant, however the object is going from static to moving, so gravity accelerates the object, however the code rigidbody.velocity = new Vector2 (10f, rigidbody.velocity.y); will keep the y velocity constant, not allowing for the previously mentioned acceleration. to fix this, I made a variable, specifically for the Y velocity, but it gets set before the velocity does (it's in the Update method), so the acceleration can happen.

I had a major problem with this and it was a Eureka moment xD

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.

Follow this Question

Answers Answers and Comments

18 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

Related Questions

Time.deltaTime/Time.fixedDeltaTime are messing with gravity 1 Answer

Playing with the Gravity PROBLEM 0 Answers

When does rigidbody.velocity affect? 1 Answer

Why when i press space button nothing was happened 0 Answers

Getting a parsing error, cant see the problem 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