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 fuj1n · Jun 15, 2013 at 06:35 AM · c#gravityaddforcemoveforce

Applying force to my object has no effect.

I have an object as my player, in my control class, I used to use the Transform.Translate, which worked but it was messing with my collider, after switching it to RigidBody.AddForce, my object does not move, I do have gravity enabled, and the gravity does work to make the player fall when they are up high.

 using UnityEngine;
 using System.Collections;

 public class InputHandler : MonoBehaviour {
     
     Transform playerTransform;
     public Rigidbody body;
     
     void Start () {
         playerTransform = gameObject.GetComponent<Transform>();
         body = gameObject.GetComponent<Rigidbody>();
     }
     
     void Update () {
         if(Input.GetKey(KeyCode.W)){
             //playerTransform.Translate(Vector3.forward * Time.deltaTime * 5);
             body.AddForce(Vector3.forward * Time.deltaTime * 5);
             
         }else if(Input.GetKey(KeyCode.S)){
             //playerTransform.Translate(-Vector3.forward * Time.deltaTime * 3);
             body.AddForce(-Vector3.forward * Time.deltaTime * 5);
         }
         if(Input.GetKey(KeyCode.A)){
             //playerTransform.Translate(Vector3.left * Time.deltaTime * 3);
             body.AddForce(Vector3.left * Time.deltaTime * 5);
         }else if(Input.GetKey(KeyCode.D)){
             //playerTransform.Translate(-Vector3.left * Time.deltaTime * 3);
             body.AddForce(-Vector3.left * Time.deltaTime * 5);
         }
         
         if(playerTransform.position.y <= -30){
             playerTransform.position = new Vector3(0f, 0.5f, 0f);
         }
     }
 }
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
Best Answer

Answer by robertbu · Jun 15, 2013 at 06:43 AM

If your object has the default mass of 1.0, then the amount of force you are applying is very small. Since you are using GetKey() which returns true every frame, you can hold down on one of the four keys. You will see your object begin to move. Try upping the amount of force:

 body.AddForce(-Vector3.forward * Time.deltaTime * 50.0f);

A couple of other notes. You don't have use GetComponent() for the transform and rigidbody. Take a look at the reference page for GameObject. You will see instance variables for many common components like rigidbody that are available. So you can just do this in your scripts:

 rigidbody.AddForce(...);
 transform.Translate(...);


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 fuj1n · Jun 15, 2013 at 07:11 AM 0
Share

Thank you, this is exactly what I was looking for.

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

14 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

Related Questions

How do I spread a movement over several frames instead of one? 1 Answer

My object can't move left and right but only jump? 1 Answer

Get result (force & torque) of AddForceAtPosition? 2 Answers

Change rigidbody's jumping speed 2 Answers

Can someone help me with this Rigidbody2D bug? 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