I’ve been working on an Action RPG for the iPhone and I want the character to move like in the Diablo games. In this case, when the user drags a finger, the character moves towards that point. It’s working, but it works weird. Sometimes the character moves incredibly fast. Also, the farther away the point is he’s moving towards, the faster he goes! Here is the source code:
var destpoint:Vector3;
function Start(){
destpoint=transform.position;
animation.CrossFade(“Idle”);
animation.wrapMode=WrapMode.Loop;
}
function Update(){
var t:GameObject=GameObject.FindWithTag(“Target”);
transform.LookAt(t.transform);
var hit : RaycastHit;
//--------------------------------- SET THE NEW DESTINATION ON MOUSE DOWN
for(touch in iPhoneInput.touches){
if(touch.phase==iPhoneTouchPhase.Moved){
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast (ray, hit)) {
Debug.DrawLine (ray.origin, hit.point);
destpoint=hit.point;
// t.transform.position=destpoint;
}
}
}
//----------------------------------- MOVE TO THE NEW DESTINATION IF WE ARE OUT OF RANGE
var dist = Vector3.Distance(transform.position, t.transform.position);
t.transform.position = Vector3.Slerp(t.transform.position, destpoint, Time.time*.002);
transform.position = Vector3.Slerp(this.transform.position, t.transform.position, Time.time*.001);
//ANIMATION CODE
if(dist>1){
animation.CrossFade(“Walk1”);
animation.wrapMode=WrapMode.Loop;
}
else{
animation.CrossFade(“Idle”);
animation.wrapMode=WrapMode.Loop;
}
//$$$$$$$$
}