- Home /
Direction of rotation and ApplyForce
Hello, I am trying to write a function which will create a visual vector on a rigidbody. Then this vector can be rotated with mouse. When vecotr rotation is finished a force is added to that rigidbody. My code is not working and I'd like to hear tips and suggestions. Thanks in advance for your answers.
var arrowPrefab : GameObject;
var speed : int;
var forceMultiplyer : float;
private var arrowInstance : GameObject;
function Update () {
if (Input.GetMouseButton(1)) {
if (arrowInstance == null) {
arrowInstance = Instantiate(arrowPrefab, transform.position, Quaternion.identity);
}
else {
if (Input.GetMouseButton(0)) {
arrowInstance.transform.Rotate(0, 0, Input.GetAxis("Mouse Y") * Time.deltaTime * speed);
}
else {
arrowInstance.transform.Rotate(Vector3(Input.GetAxis("Mouse Y"), Input.GetAxis("Mouse X"), 0) * Time.deltaTime * speed);
}
}
}
if (Input.GetMouseButtonUp(1)) {
if (arrowInstance != null) {
var forceVector : Vector3 = new Vector3(forceMultiplyer,
forceMultiplyer,
forceMultiplyer);
rigidbody.AddForce(arrowInstance.transform.rotation * forceVector);
Destroy(arrowInstance);
}
}
}
Answer by aldonaletto · Oct 18, 2011 at 01:05 AM
If your problem is the force direction (forceVector is pointing to the middle of the 3 axes), you can use arrowInstance.transform.forward:
if (Input.GetMouseButtonUp(1)) { if (arrowInstance != null) { rigidbody.AddForce(arrowInstance.transform.forward * forceMultiplyer); Destroy(arrowInstance); } }
Yes, the problem is the force direction. If I use your code the force is applied wrong. I want the force vector direction to be the same with my arrowInstance direction. As I said before, arrowInstance can be rotated in 3 axes. Thanks.
And to which direction the arrow model tip is pointing to? Z, Y or X? I assumed the model tip points to Z, and thus used transform.forward. If the model tip points to Y, you must use transform.up; if points to X, transform.right. Notice that I'm talking about the arrow model: when you drag it to the scene, it comes with null rotation, and you can see to which axis the arrow tip initially points.
Your answer
![](https://koobas.hobune.stream/wayback/20220613012608im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Character Controller meets Rigidbody 1 Answer
add force to object that has 2 different rigid bodies 0 Answers
Simple Rigidbody / force question 3 Answers
Aligning 2 Quaternions Using Torque 1 Answer