Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to Move Make Objects by Adding Force in Unity

    • 0
    • 4
    • 3
    • 1
    • 0
    • 0
    • 0
    • 0
    • 6.86k
    Comment on it

    There are two methods to make object move in unity:

    1. By changing its Transform (Position).
    2. By adding Force by using AddForce() method.

    In this blog I have mentioned, how we can make object move by adding force.
    Force and Velocity both are component of Physics. So, to make our gameobject come under influence of Physics we must attach a rigid body to it and then we can code it to move by adding force in different direction.

    Below is the script for your reference.

    using UnityEngine;
      using System.Collections;
      public class BallScript : MonoBehaviour 
      {
          void Update () 
          {
            if (Input.GetKey (KeyCode.UpArrow)) 
             {
                    this.gameObject.rigidbody.AddForce(0,0,10,ForceMode.Acceleration);
              }
           if (Input.GetKey (KeyCode.DownArrow)) 
             {
                     this.gameObject.rigidbody.AddForce(0,0,-10,ForceMode.Acceleration);
             }
             if (Input.GetKey (KeyCode.LeftArrow)) 
             {
                   this.gameObject.rigidbody.AddForce(-10,0,0,ForceMode.Acceleration);
             }
             if (Input.GetKey (KeyCode.RightArrow)) 
             {
                   this.gameObject.rigidbody.AddForce(10,0,0,ForceMode.Acceleration);
             }
            if (Input.GetKey (KeyCode.M)) 
             {
                   this.gameObject.rigidbody.AddForce(0,20,0,ForceMode.Acceleration);
            }
         }
      }
    

    AddForce() method have 4 variants and we can use any of them to make our object move.

    public void AddForce(vector3 force);
    public void AddForce(vector3 force , ForceMode mode);
    public void AddForce(float x, float y, float z);
    public void AddForce(float x, float y, float z, vector3 force);
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: