- Home /
Question by
neejammer · May 30, 2015 at 11:36 AM ·
rigidbodyraycastgetmousebutton
How to get right Physics with Follow on click
I got a sphere object to follow my mouse while click and dragging. However it just glides without rolling towards my mouse. Is it possible to add a rigidbody to this and so make it rotate?
Something like : GetComponent.().AddTorque(direction * MoveSpeed * Time.deltaTime, ForceMode.Acceleration);
??
This is my current code:
#pragma strict
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
Answer by neejammer · May 30, 2015 at 07:06 PM
the sphere also seems to shoot off in a forward direction on starting the game... before i even click anywhere
Your answer
Follow this Question
Related Questions
Get texture pixel color after raycast on rigidbody object 1 Answer
Get hit direction from one gameObject hitting another 2 Answers
Nail a rigidbody to another 0 Answers
How to Click to Move Sphere (With physics)?* 0 Answers
RaycastHit behind object 2 Answers