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 shade399 · Oct 21, 2011 at 05:11 AM · javascriptrigidbodyforce

Adding force to rigidbody

Hi! I'm trying to make a game which involves flying a rocket. The code I have for the force is:

function Update () {

 rigidbody.AddForce(new Vector3(Input.GetAxis("Horizontal") * 2, 0, 0));
 rigidbody.AddForce(new Vector3(Input.GetAxis("Vertical") * 5, 0, 0));

}

The horizontal works, because when the rocket is on the ground I can make it lean with left arrow and right arrow. The vertical, however, doesn't work as intended. The rocket slides at an angle, and stays on the ground. Help??

Comment
Add comment · Show 2
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 shade399 · Oct 21, 2011 at 04:44 AM 0
Share

Just re-wrote the above code, and now all it does (horizontal or vertical) is slide on an angle.

avatar image syclamoth · Oct 21, 2011 at 05:18 AM 0
Share

It probably doesn't help that you are putting both the horizontal and vertical forces into basically the same thing. Try using something more like

 var horizontalForce = Input.GetAxis("Horizontal");
 var verticalForce = Input.GetAxis("Vertical");

 rigidbody.AddForce(new Vector3(horizontalForce, verticalForce, 0);

However, if you want the rocket to rotate, this is still probably not what you want.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by syclamoth · Oct 21, 2011 at 05:16 AM

Instead of using rigidbody.AddForce, try using rigidbody.AddTorque. AddForce adds a force to the centre of gravity of the rigidbody- which doesn't change the angle of the object! Use AddTorque to rotate an object the way you want to.

Also, try using transform.TransfomPoint(inputVector) to transform from local coordinates to global coordinates! That way, your inputs will always be the same relative to the position and rotation of the rocket.

Comment
Add comment · Show 5 · 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 shade399 · Oct 21, 2011 at 11:15 PM 0
Share

Thanks! Perhaps I should explain more...

I want the rocket to be launched by pressing space, apart from that the player will have no control (no it's not a rocket launcher weapon) Also, the force should stop after a variable amount of time depending on which "engine" the player has selected.

This should be pretty simple, but I can't seem to get it to work.

avatar image syclamoth · Oct 22, 2011 at 12:42 AM 0
Share

Well, if the player isn't supposed to have any control over the rocket after it's been launched, why are you reading off the input axes?

Try doing it with some kind of timer-

 var thrustTime = 10;
 var power = 1000;
 function Update()
 {
     if(thrustTime >= 0)
     {
         rigidbody.AddForce(-transform.up * power);
         thrustTime -= Time.deltaTime;
     }
 }

When the rocket is on the ground, allow the player to rotate it with the horizontal axis, and make sure its rigidbody is kinematic. Then, when the time comes to launch it, set thrustTime to the amount of time your current engine allows, and set power to the amount of power your engine should have, set rigidbody.is$$anonymous$$inematic to false, and watch your space program fly!

avatar image shade399 · Oct 23, 2011 at 01:35 AM 0
Share

Hmmm... I must be doing something stupid, because no matter how high I set the power, the rocket jumps slightly, then falls slooooooowly back to the terrain.

avatar image aldonaletto · Oct 23, 2011 at 02:59 AM 1
Share

I suggest some changes in the @syclamoth code:
1- move it to FixedUpdate to have frame rate independent results;
2- declare thrustTime as a float;
3- change -transform.up to transform.up;

var thrustTime: float = 10; var power: float = 1000;

function FixedUpdate(){ if (thrustTime >= 0){ rigidbody.AddForce(transform.up * power); thrustTime -= Time.deltaTime; } }

avatar image syclamoth · Oct 23, 2011 at 03:43 AM 0
Share

FixedUpdate doesn't help with being frame-rate independent, exactly, but @aldonaletto is correct in saying that rigidbody physics should be put there anyway, because FixedUpdate only gets called once per physics step, ins$$anonymous$$d of every single frame. I think there should be a Time.deltaTime in there somewhere, too- just to make things more generally sane (and consistent between different physics step times)

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to set max speed for this car ? 1 Answer

How to AddForce OverTime? 1 Answer

How to make a RigidBody move in a certain direction 1 Answer

Getting error transform.position assign attempt for "Player" is not valid on rigidbody 2 Answers

Applying force to a rigidbody 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