Why does this script spawn 13 ClickEffect prefabs instead of 1?

private var targetPosition:Vector3;
var speed : float = 60;
var clickEffect : GameObject;

        function Update () {
 
            if(Input.GetKeyDown(KeyCode.Mouse0))
 
            {
 

           speed = 1;
 
                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;
 
                   
 
                     
 
                }
 
             
 
            }
 
           
 
                var dir:Vector3 = targetPosition - transform.position;
 
    var dist:float = dir.magnitude;
 
    var move:float = speed * Time.deltaTime;
 
    if(dist > move){
 
    transform.position += dir.normalized * move;
    Instantiate(clickEffect,targetPosition,Quaternion.identity);
 
    } else {
 
    transform.position = targetPosition;
 
    }
 
           
 
           
 
            transform.position += (targetPosition - transform.position).normalized * speed * Time.deltaTime;
 
           
 
         
 
         
 
         
 
         }

Any ideas how to fix it? Thanks in advance.

On line 51 you are checking if dist is bigger than move, and then instantiating a clickEffect. But then dist is still bigger than move and will keep spawning clickEffects.

Any ideas how to fix that?

2 Answers

2

the problem
you’re instantiating for every frame that dist > move
try putting the code within.

  if(Input.GetKeyDown(KeyCode.Mouse0))     
{
//code goes here
}

that way it will only ever instantiate when you press the mouse button not for every frame that distance is more than move

private var targetPosition:Vector3; 
var speed : float = 60;      
var clickEffect : GameObject;
 
 
    function Update () {
 
            if(Input.GetKeyDown(KeyCode.Mouse0))
            {
				speed = 1;
                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;
 
                }
				if(dist > move){
					Instantiate(clickEffect,targetPosition,Quaternion.identity);
				}

            }
 
 

    var dir:Vector3 = targetPosition - transform.position;
    var dist:float = dir.magnitude;
    var move:float = speed * Time.deltaTime;
 
    if(dist > move){
 
		transform.position += dir.normalized * move;
		//Instantiate(clickEffect,targetPosition,Quaternion.identity);
    } 
	else {
		transform.position = targetPosition;
    }
 
    transform.position += (targetPosition - transform.position).normalized * speed * Time.deltaTime;

     }

I'm getting these errors, I've tried to fix them, but I cant see what's wrong: Assets/Mit/Scripts/Attach to Player/Movement.js(23,21): BCE0005: Unknown identifier: 'move'. & Assets/Mit/Scripts/Attach to Player/Movement.js(23,14): BCE0005: Unknown identifier: 'dist'.

i do, move three above Get Button Down, so that they are declared at the start of the function. the error is caused by the variable being declared after you were trying to use them. var dir:Vector3 = targetPosition - transform.position; var dist:float = dir.magnitude; var move:float = speed * Time.deltaTime; try to keep local variables at the top

Nevermind, I found it, stupid me. Thanks for your help, I really really appreciate it!

Oh, after first time you click somewhere, next time it just teleports, also the ClickEffect is only there for 1 second. Any ideas how to fix it?

Input.GetKeyDown will return true each frame, meaning the duration of the mouse button held down might be 13+/- frames.

Input.GetMouseButtonDown will return true once and wait for the user to release the button and subsequently presses the button again.

Getting errors if I change it

what errors?

dynamic_bitset.test bit out of bounds UnityEngine.Input:GetMouseButtonDown(Int32) Movement:Update() (at Assets/Mit/Scripts/Attach to Player/Movement.js:8)

Actually GetButtonDown Returns once until they key is released yhe pressed again, your thinking of GetButton which returns as long as the button is pressed