- Home /
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.
Answer by Fornoreason1000 · Nov 22, 2013 at 06:29 PM
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/$$anonymous$$it/Scripts/Attach to Player/$$anonymous$$ovement.js(23,21): BCE0005: $$anonymous$$ identifier: 'move'.
&
Assets/$$anonymous$$it/Scripts/Attach to Player/$$anonymous$$ovement.js(23,14): BCE0005: $$anonymous$$ 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
Never$$anonymous$$d, 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?
Answer by Landern · Nov 22, 2013 at 05:07 PM
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.
dynamic_bitset.test bit out of bounds
UnityEngine.Input:Get$$anonymous$$ouseButtonDown(Int32)
$$anonymous$$ovement:Update() (at Assets/$$anonymous$$it/Scripts/Attach to Player/$$anonymous$$ovement.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