Is There a way to AddForce to a object so that it goes in the direction of the mouse position such as Input.mousePosition.x, Input.mousePosition.y. It’s for my 2d shooter I’m making. I used look at function and thought If I add force to the z direction then my look at would guide my projectile, but I was horribly mistaken.
I wanted to make it that wherever I aimed my mouse cursor it would directly out of that position. I had it to where when it fire add force to the z direction while following x and y coord on screen.
var force: float = 10000;
var CanBall : GameObject;
var cannon2 : GameObject;
function Update() {
CannonBallSpawn.position += Vector3(mousePosition.x, mousePosition.y,0));
cannon2.transform.LookAt(Vector3(Input.mousePosition.x, Input.mousePosition.y, 0));
if(Input.GetMouseButtonUp){
Instantiate(CanBall, transform.position, transform.rotation);
CanBall.attachedRigidbody.AddForce(Vector3(0,0,force));
//audio.PlayOneShot(Sound);
}
}
I believe this is the one script i was using.
I for got to past in the var for //var CannonBallSpawn : GameObject;