Post by James :) (aka Madcow) on May 27, 2012 16:24:43 GMT -5
Maths has never been my strong point.
In Unity I've been creating a game where you can pick up objects drag them around and then drop them in or flick them and the object will keep going i the direction you flicked it.
I've been trying to create a way of calculating the velocity based on the mouse speed on release. i.e if it wasn't moving fast it just drops it and if the user moved it fast (i.e a flick) it goes fast.
At the moment my code is a bit erratic I hope this helps:
Does anyone else have ideas?
In Unity I've been creating a game where you can pick up objects drag them around and then drop them in or flick them and the object will keep going i the direction you flicked it.
I've been trying to create a way of calculating the velocity based on the mouse speed on release. i.e if it wasn't moving fast it just drops it and if the user moved it fast (i.e a flick) it goes fast.
At the moment my code is a bit erratic I hope this helps:
var yWhilstDragging:float = 10;
var flickSpeed:float = 5;
private var isDragging:boolean = false;
private var screenSpace:Vector3;
private var offset:Vector3;
private var lastPosition:Vector3;
private var mouseVelocity:Vector3;
function OnMouseDown():void{
if (!isDragging){
isDragging = true;
gameObject.rigidbody.useGravity = false;
gameObject.rigidbody.isKinematic = true;
gameObject.rigidbody.rotation = Quaternion.identity;
}
}
function OnMouseDrag():void{
var curScreenSpace = Vector3(Input.mousePosition.x, Input.mousePosition.y, yWhilstDragging);
var curPosition = Camera.main.ScreenToWorldPoint(curScreenSpace) + offset;
mouseVelocity = curPosition - lastPosition;
lastPosition = curPosition;
transform.position = curPosition;
}
function OnMouseUp():void{
if (isDragging){
isDragging = false;
gameObject.rigidbody.useGravity = true;
gameObject.rigidbody.isKinematic = false;
Debug.Log(mouseVelocity);
gameObject.rigidbody.velocity = mouseVelocity * flickSpeed;
lastPosition = Vector3.zero;
mouseVelocity = Vector3.zero;
}
}
function Update():void{
if (isDragging){
screenSpace = Camera.main.WorldToScreenPoint(transform.position);
offset = transform.position - Camera.main.ScreenToWorldPoint(Vector3(Input.mousePosition.x, Input.mousePosition.y, screenSpace.z));
}
}
Does anyone else have ideas?