- Home /
character moving after moving
My character moves after spawning and stop on one place. I cant find the problem. I have a code like this :
function OnLevelWasLoaded(thisLevel: int)
{
transform.position = GameObject.FindWithTag("spawn").transform.position;
standing = true;
}
I dont know what is the caused of the unnecessary movement of my character.
I control my character using mouse click and here is the code which I Got from the internet:
var smooth:float; // Determines how quickly object moves towards position
var isWalking : boolean;
var lastP;
var move;
var pressed: boolean;
var standing : boolean;
var objectP ;
private var targetPosition:Vector3;
function Start()
{
animation.Stop();
//animation.Play("idle"); // this stops Unity from playing the character's default animation.
standing = spawnPointScript.standing;
}
function Update () {
pressed = false;
if(standing==false){
if(pressed== false)
{
if(Input.GetKeyDown(KeyCode.Mouse1))
{
smooth=5;
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;
}
}
move =transform.position = Vector3.MoveTowards(transform.position, targetPosition, Time.deltaTime*smooth);
if(lastP != move)
{
if(!isWalking){
isWalking= true;
animation.Play("walk");
}
}
else
{
if(isWalking)
{
isWalking = false;
animation.Play("idle");
}
}
lastP =move;}
pressed = true;
}
else if(standing == true)
{
animation.Stop();
if(Input.GetKeyDown(KeyCode.Mouse1))
standing = false;
}
}
I already spent 2 hours looking for the solution but still, I cant find one.. hope you can help.. thanks