- Home /
Question by
GenaralSkar · Feb 13, 2017 at 12:12 PM ·
movementraycastaddforcemousepositionmovetowards
Push an object opposite direction of mouse position
I'm new to Unity and want to make a game where the player moves by clicking somewhere, and the player object gets pushed in the opposite direction. It's a 3D game but from a top down perspective. This code sort of works, but when the object gets too far away from the origin it doesn't seem to care where the mouse is and just starts getting pushed away from the origin. Also when the object's y position is above 0 it starts floating off into space. I imagine this is because the force is getting added from a point below the object, but I don't know how to fix this.
void FixedUpdate () {
// Player Movement
Ray cameraRay = Camera.main.ScreenPointToRay(Input.mousePosition);
Plane groundPlane = new Plane(Vector3.up, Vector3.zero);
float rayLength;
if(groundPlane.Raycast(cameraRay, out rayLength))
{
Vector3 pointToLook = cameraRay.GetPoint(rayLength);
Debug.Log(cameraRay.GetPoint(rayLength));
if(Input.GetMouseButton(0))
{
myRigidBody.AddForce(Vector3.MoveTowards(transform.position, pointToLook, -moveSpeed));
}
if(Input.GetMouseButton(1))
{
}
}
}
Like I said I'm new to Unity so I don't know if I'm even using the right code. Any help would be greatly appreciated.
Comment