Mouse click movement script Almost done Need help

Hi everyone,

I am working on a mouse click movement script for someone.
This is not pathfinding, just click the player click a point and the player runs to it.

You can try it here

The player in the scene has no character controller.
He moves soley by the code below.

My problem is when he gets to the target, it goes back and forth on the point .

it works great when i use a box instead.

Can someone help me with this problem :frowning:

var speed : float = 1;
var targetRadius = 3;
private var target : Vector3;
private var endTarget : Vector3;
private var controlCenter : ControlCenter;

function Start () {
	controlCenter = FindObjectOfType(ControlCenter);
	animation.Play("idle");
    rigidbody.freezeRotation = true;
    target = transform.position;
}

function Update()
{
	if(transform.position == Vector3.Distance(transform.position, target))
	{
		animation.Play("idle");
	
	}
	//~ //Debug.Log(transform.position);
}

function FixedUpdate () {
	
    target.y = transform.position.y;
	// you can control the player
	if(controlCenter.isSelected == true)
	{
		var hit : RaycastHit;
		if (Input.GetButtonDown ("Fire1")) 
		{
			ray = Camera.main.ScreenPointToRay (Input.mousePosition);
			if (Physics.Raycast (ray, hit)) 
			{
				target = hit.point; 
				endTarget = hit.point;
				Debug.Log(hit.point);			
			}
		}   

		if (Vector3.Distance(transform.position, target) > targetRadius) 
		{
				transform.LookAt(target); 
				transform.Translate(0,0,speed);  
				animation.Play("run");			
		}
		if(Vector3.Distance(transform.position, target) == endTarget)
		{
			transform.LookAt(target);
			transform.Translate(0,0,0);
			animation.Play("idle");
		}
	}
}

You will need to simplify this. First, you are running back and forth because you are translating it farther than the remaining distance. This would be fine, if you adhered to the targetRadius. However, you are not, you are using a second if that says if my current point != to my exact distance, then continue. It never will be. :wink:

So, I adjusted your code and simplified where needed:

var speed : float = 1;
var targetRadius = 3;
private var target : Vector3;
private var controlCenter : ControlCenter;

function Start (){
	controlCenter = FindObjectOfType(ControlCenter);
	animation.Play("idle");
	rigidbody.freezeRotation = true;
	target = transform.position;
}

function Update () {
	target.y = transform.position.y;
	// you can control the player
	if(controlCenter.isSelected == true){
		var hit : RaycastHit;
		if (Input.GetButtonDown ("Fire1")){
			ray = Camera.main.ScreenPointToRay (Input.mousePosition);
			if (Physics.Raycast (ray, hit)){
				target = hit.point;
				Debug.Log(hit.point);			
			}
		}   
	}
	if (Vector3.Distance(transform.position, target) > targetRadius){
				transform.LookAt(target); 
				transform.Translate(0,0,speed * Time.deltaTime);
				animation.Play("run");			
		}else{
			animation.Play("idle");
		}
}

Thanks for the help :slight_smile:

Now I hit another problem :frowning:
I want the player to stop moving when it hit an object with tag .

Too try my code

here is my code

coll.js

var stop : boolean = false;
function Update () {
}

function OnTriggerEnter(hit : Collider)
{
	 if(hit.gameObject.tag == "InWay")
	 {
		
		stop = true;
	 
	 }
	 
	 else
	 
	 stop = false;
}

click move script

var speed : float = 1;
var targetRadius = 3.1;
private var target : Vector3;
private var controlCenter : ControlCenter;
private var coll : Coll;

function Start (){
	controlCenter = FindObjectOfType(ControlCenter);
	coll = FindObjectOfType(Coll);
	animation.Play("idle");
	rigidbody.freezeRotation = true;
	target = transform.position;
}

function Update () {
	target.y = transform.position.y;
	// you can control the player
	if(controlCenter.isSelected == true){
		var hit : RaycastHit;
		if (Input.GetButtonDown ("Fire1")){
			ray = Camera.main.ScreenPointToRay (Input.mousePosition);
			if (Physics.Raycast (ray, hit)){
				target = hit.point;
				Debug.Log(hit.point);			
			}
		}   
	}
	if (Vector3.Distance(transform.position, target) > targetRadius)
	{
	
			
				transform.LookAt(target); 
				transform.Translate(0,0,speed * Time.deltaTime);
				animation.Play("run");
				
					//if collided with box stop moving
					if (coll.stop == true)
					{
						
						transform.Translate(0,0,0 * Time.deltaTime);
						animation.Play("idle");
					
					}
	}
	
	
	else
	{
		
			animation.Play("idle");
	}
}

anyone?
this is frustrating me

OK, you need to do a second ray from the transform.position to transform.forward plus the radius of the character. If it hits, just have it stop. If it doesnt continue it.

You sure do like tabs a lot.

Also, you don’t have to translate zero. if you want it to stop moving, just dont translate it. :wink:

var speed : float = 1;
var targetRadius = 3.1;
private var target : Vector3;
private var controlCenter : ControlCenter;
private var coll : Coll;
private var characterRadius : float = 1.0;

function Start (){
	controlCenter = FindObjectOfType(ControlCenter);
	coll = FindObjectOfType(Coll);
	animation.Play("idle");
	rigidbody.freezeRotation = true;
	target = transform.position;
}

function Update () {
	target.y = transform.position.y;
	var hit : RaycastHit;
	var ray : Ray;
	// you can control the player
	if(controlCenter.isSelected == true){
		
		if (Input.GetButtonDown ("Fire1")){
			ray = Camera.main.ScreenPointToRay (Input.mousePosition);
			if (Physics.Raycast (ray, hit)){
				target = hit.point;
				Debug.Log(hit.point);			
			}
		}   
	}
	if (Vector3.Distance(transform.position, target) > targetRadius){
		transform.LookAt(target);
		var distance=speed * Time.deltaTime;
		ray=Ray(transform.position, transform.forward);
		if(!Physics.Raycast(ray, hit, distance + characterRadius)){
			transform.Translate(0,0,distance);
			animation.Play("run");
		} else {
			animation.Play("idle");
		}
	}else{
		animation.Play("idle");
	}
}

i do not need the col script then?

nope

thanks alot sir :smile: