Point and Click Player Walks

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)

I’d say using hitDist for you’re speed may be a bad idea, because that’s not you’re character’s actual speed. My suggestion would be to use a mixture between the character’s actual speed and hitDist to still get that slowing down effect at the end of the walk.

So you may want to find the speed of your character at all times first
an easy way to do that is to sample you’re character’s position at two different points in two different frames, then convert that into units per second.

It sounds hard, but it’s just

var lastPosition : Vector3 = Vector3.zero;

function Start ()
{
lastPosition = transform.position;
}
function Update ()
{
var speed :float = Vector3.Distance(lastPositon,transform.position)*(1/Time.deltaTime);

lastPosition = transform.position;
}

When the code goes down the stack it doesn’t define lastPosition until the end of the stack, so when speed is defined, it uses the position from the last frame. So finding the distance between the object’s position in one frame and the same object’s position in another frame is it’s speed.

(1/Time.deltaTime) is just because you’re object will move at different speeds depending on how long it takes to draw the frame, so this function just multiplies it so the end speed isn’t dependent on the draw time.

Now that you have a speed variable you want that effect that slows the character’s animation down as it approaches the point, for that you can use hitDist again, but when the distance between the character and the point is greater than one, then you’re animation will play faster than you’re character is moving. I rarely like to use clamps, but it works here. So when you set the float

MeshControl.SetFloat("Speed",speed*Mathf.Clamp(,hitDist,0,1);

That way your character’s animation will only play at maximum, the player’s actual speed, and will slow down as he approaches his target.

So that was a lot of information, and I’m bound to get things wrong. I really hope this helps you out, and if you need any thing else don’t hesitate to PM me on the forums

Thank you that really helped a lot. I still haven’t gotten it working, but atleast now I have a speed attribute that is increasing when he is moving toward and slowly comes back down as he reaches the destination.

I noticed when I used that code with Clamp that it basically displayed the distance from the clicked point and didn’t actually change to a lower value. So what I ended up trying was MechControl.SetFloat(“Speed”, speed); . Then since it shows the float going as high as 200+ and then doesn’t drop all the way to 0. It instead drops to somewhere around 6ish and fluctuates constantly. So I went into the Animator and changed the Idle to Locomotion connector’s Conditions to say when greater than 20 and less than 20, but that doesn’t seem to work for some reason even though what is being displayed for me as far as speed is less than 20 when he reaches his destination. So he should be switching back to idle and isn’t.

Another thing that might work well is if there is a way to get speed to stay between 0 and 1. When I displayed speed inside of a Clamp it stayed as either 0 or 1 with no in between. (It was 0 until I clicked a place to go to and then it stayed as 1).

Not sure if you or anyone else has any input that might help. Thanks again for taking the time to help.

sorry, to get the speed value at 0, just put in some sort of threshold, it does like to stay at some extremely small value.

like

if(speed < 0.1)
speed = 0;

Thank you very much. You were a huge help. It’s not entirely solved, but it doesn’t appear to have to do with that any longer. I get it to display 0 so it should be working. There must be an issue within the Animator Controller or something. When I click on a spot in front of him he animates to it and continues running in place after reaching it even though Speed is now 0 and technically if speed is 0 the Animator Controllers connector’s conditions should be triggered back to idle since it is less than 0.1. The only way I can now get him to stop running forward is if I click directly behind him or very close to him he stop moving and switches to idle.

Well thanks again for your help. You have been most helpful.

No problem, happy to help. Need anything else, I’m here. Good Luck :smile: