- Home /
Question by
$$anonymous$$ · Mar 15, 2014 at 08:01 PM ·
physicsaddforcelookat
I need to add force based on the angle I'm looking at the object.
Here, I'm using Unity's add force script from the tutorial.
using UnityEngine;
using System.Collections;
public class MoveBox : MonoBehaviour
{
public float pushForce = 1000;
void OnMouseDown() {
rigidbody.AddForce(-transform.forward * pushForce);
rigidbody.useGravity = true;
}
}
No matter where I'm looking at the object, (in first-person) it only adds negative force along the z axis (the blue arrow). How can I change this, so it will add force forward from where I'm looking at it.
Comment
Answer by fafase · Mar 15, 2014 at 08:07 PM
Try with:
Vector3 direction = (transform.position + transform.forward) - transform.position;
rigidbody.AddForce(direction * pushForce);
Note this is just doing:
Vector3 direction = transform.TransformDirection(Vector3.forward);
Hm... It did not work. This time it just pushes it the other way on the z axis, no matter where I am.