Hello,
I’m trying to get my character to move on point and click of the mouse and to use his walking animations. As of right now he does virtually what I want, but the issue is he is not switching back to idle after he reaches the spot that was clicked. I used the new Unity 4 Mecanim video to set up the Animator Controller so it is set when Speed is between -0.1 and 0.1 that it will switch back to idle. (Should anyhow). Here is the code I am using at the moment. The if’s toward the bottom that are commented out are different methods I have tried to get it to test if they are close or not.
var smooth:int; // Determines how quickly object moves towards position
var percentageDifferenceAllowed : float = 0.01; // is 1%
private var targetPosition:Vector3;
function Update () {
if(Input.GetKeyDown(KeyCode.Mouse0))
{
var MechControl: Animator = GetComponent(Animator);
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);
if((transform.position - targetPosition).sqrMagnitude <= (transform.position * percentageDifferenceAllowed).sqrMagnitude) {
hitdist = 0.0;
}
//if(Mathf.Approximately(transform.position.magnitude, targetPosition.magnitude)){
// hitdist = 0.0;
//}
MechControl.SetFloat("Speed",hitdist);
}
Honestly the only reason I used hitdist in the SetFloat is because I couldn’t figure out what other float I should use and where. So if that is what is causing the issue, any ideas on how to set up a float specific to it so that the float changes decreases in value as he gets close to the target spot. This way I could even have a slowing run as he reaches his destination.
Any help is of course very much appreciated. If you want to go above and beyond I could always use help figuring out how to get the character to walk to a specific location when there is no one there to target, but if you click on an enemy target it walks up to a certain range of the target before it stops. (Double tap preferred)