- Home /
Add force with direction
i am trying to add a force to my rigidbody with an angle. Let me explain it. i ll get an angle value 0 to 360 degrees. for example 30 degrees. and addforce that comes with 30 degrees. if i get 180 degrees and add a force with this angle my object has to go back. or if its 90 degrees this object start to turn left side. or 270 degrees start to turn right side.
Educate yourself:
Answer by aditya · Mar 28, 2016 at 11:44 AM
i m assuming that you are using this script on a gameobject with rigidbody attached
private bool forceApplied;
void FixedUpdate(){ if(forceApplied){ YOUR RIGIDBODY
.AddForce(transform.Forward * POWER
); forceApplied = false; }
this will apply force just form one physics frame, change for your needs
use this for help : http://docs.unity3d.com/ScriptReference/Transform-forward.html
Answer by Bleackk · Mar 28, 2016 at 03:01 PM
If you want to move the body around using AddForce this is how you do it
using UnityEngine;
using System.Collections;
public class NewBehaviourScript : MonoBehaviour {
public float speed;
//editable speed, you can access it from the editor
private Rigidbody rb;
//Refference to the Rigidbody component in the script
void Start ()
{
rb = GetComponent<Rigidbody> ();
//Accessing the Rigidbody component on your Game Object
}
void Update ()
{
float mH = Input.GetAxis ("Hozirontal");
float mV = Input.GetAxis ("Vertical");
Vector3 move = new Vector3 (mH, 0.0f, mV);
rb.AddForce (move * speed)
}
}
if this is not it, then sorry