- Home /
Question by
neejammer · Jun 08, 2015 at 07:28 AM ·
physicsrigidbodysphereclicktomovegetmousebutton
How to Click to Move Sphere (With physics)?*
I have a basic click to move character controller script attached to a sphere. However, as it is now my sphere only moves around statically -like gliding across the floor. How do I get my script to make use of the 'Rigidbody' so my sphere rotates(moves) with the right physics? Or do you have any other solution?
Help would be very very much appreciated"! I've been trying to find the answer to this for days now.
Here is the ClickToMove script (http://wiki.unity3d.com/index.php/Click_To_Move)
var smooth:int; // Determines how quickly object moves towards position
private var targetPosition:Vector3;
function Update () {
if(Input.GetMouseButton(0))
{
var playerPlane = new Plane(Vector3.up, transform.position);
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hitdist = 0.0;
if (playerPlane.Raycast (ray, hitdist)) {
var targetPoint = ray.GetPoint(hitdist);
targetPosition = ray.GetPoint(hitdist);
var targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
transform.rotation = targetRotation;
}
}
transform.position = Vector3.Lerp (transform.position, targetPosition, Time.deltaTime * smooth);
}
Comment