- Home /
rigidbody.AddForce() - I just don't understand it.
Hi. I'm very new to Unity3D, and trying to code something, which lets a ball move. My Code: using UnityEngine; using System.Collections;
public class ActPos : MonoBehaviour {
private GameObject playercam;
private GameObject player;
private Rigidbody playerbody;
// Use this for initialization
void Start () {
playercam = GameObject.Find ("playercam");
player = GameObject.Find ("player");
Rigidbody playerbody = player.AddComponent<Rigidbody> ();
}
// Update is called once per frame
void Update () {
Vector3 distance = new Vector3 (10, -2, 0);
playercam.transform.position = player.transform.position - distance;
if (Input.GetKey ("up")) {
Vector3 move = new Vector3 (10, 0, 0);
playerbody.AddForce(????); //Heres my question
}
}
}
What to put into the playerbody.AddForce() to let the player move 10 on the x axis? And did I even declare the Rigidbody the right way? Sorry, as already mentioned, I'm new to Unity, and to C#
Answer by nightbane30 · May 03, 2015 at 11:23 PM
This is the documentation on rigidbody.AddForce:
http://docs.unity3d.com/ScriptReference/Rigidbody.AddForce.html
Also, I'd recommend using GetComponent instead of AddComponent (line 9) with a rigidbody on your ball by default, but I guess it's whatever floats your boat. This is how you could write your AddForce line:
playerbody.AddForce(transform.[DirectionYouWantYourBallToMove. In X-Axis case it'd either be transform.left or transform.right] * speed, ForceMode.Force);
You could declare a variable speed and then make it public so you can set it in the editor. I'm assuming in this case you'll want it set to 10. Here's also the documentation for ForceMode if you want to read up on how that works:
http://docs.unity3d.com/ScriptReference/ForceMode.html
Let me know if that helped!
To clarify: when I said use GetComponent, I meant that you'd add a rigidbody component in the actual editor to your ball, then change AddComponent to GetComponent so it's just accessing a component that already exists ins$$anonymous$$d of creating an entirely new one.
Adding to this, you should think of add force by reading it. If it is on update it will literally add a force every frame, giving the object kinetic energy. If you just want to move your object you can either use rigidbody.velocity or transform.translate. Check these commands on the documentation
Changed AddComponent to GetComponent and changed my update function: void Update () { Vector3 distance = new Vector3 (10, -2, 0); playercam.transform.position = player.transform.position - distance; if (Input.Get$$anonymous$$ey ("up")) { playerbody.AddForce(player.transform.forward * 100, Force$$anonymous$$ode.Force); //Heres my question } } Still nothing happens when pressing up arrow.
Change your Input.Get$$anonymous$$ey part to this:
Input.Get$$anonymous$$ey($$anonymous$$eyCode.UpArrow)
That should do the trick. If you're only planning on publishing for PC, I make it a practice to use $$anonymous$$eyCode ins$$anonymous$$d of strings, etc. Although if you're planning on publishing for something where more than one form of input may be used, you'd be better of using something other than $$anonymous$$eyCode.