Search Unity

<Rigidbody>().AddForce(Vector3.Up?

Discussion in 'Physics' started by Gaminggoat, Nov 2, 2016.

  1. Gaminggoat

    Gaminggoat

    Joined:
    Jan 27, 2015
    Posts:
    7
    Hello i'm trying to apply force up along the y axis to make something hover. I have tried alot of different combinations to make this work and I have failed. Also I would prefer to have it continue to add force while holding mouse down any suggestions.
    Thanks

    using UnityEngine;
    using System.Collections;

    public class Up : MonoBehaviour {
    public float upForce;

    void OnMouseUp()
    {
    GetComponent<Rigidbody>().AddForce(Vector3(0,1,0). * upForce);
    GetComponent<Rigidbody>().useGravity = true;
    }
    }
     
  2. Gaminggoat

    Gaminggoat

    Joined:
    Jan 27, 2015
    Posts:
    7
    Wrong Category I guess it should be in scripting
     
  3. Big_Friggin_Al

    Big_Friggin_Al

    Joined:
    Jan 9, 2014
    Posts:
    37
    You're only applying a tiny bit of force one time, since the mouse up event only happens for one frame.

    You'll need to continually apply force to get it to hover.
     
  4. jaw02jab10

    jaw02jab10

    Joined:
    May 11, 2019
    Posts:
    1
    Try to place the code within a fixed update function
    void FixedUpdate()
    {
    //Place code here
    }

    what this does is it repeats the code for many frames like a loop.