Question by
ThunderEntertainment · Nov 02, 2018 at 08:45 AM ·
movementupdatebooleanefficiency
Is there a better way to do this?
Hello, Take a look at the code below:
bool moveToChair;
void Update()
{
if (Input.GetKeyDown("e"))
{
moveToChair = true;
}
if (moveToChair)
{
Vector3 target = new Vector3(chair.transform.position.x - transform.parent.parent.position.x, 0f, chair.transform.position.z - transform.parent.parent.position.z);
Quaternion rot = Quaternion.LookRotation(target);
transform.parent.parent.rotation = Quaternion.Slerp(transform.parent.parent.rotation, rot, 0.1f);
transform.parent.parent.Translate(Vector3.forward * 0.05f);
if (Vector3.Distance(transform.parent.parent.position, chair.transform.position) < 0.5f)
{
transform.parent.parent.position = chair.transform.position;
print("moved");
}
}
}
Using a bool to determine when to move does not seem like the most efficient solution. Is there a better way to do this? Using an Ienumerator, threading or something else maybe?
I appreciate your help.
Comment
@ThunderEntertainment - Hi, why not just do:
if (Input.Get$$anonymous$$eyDown("e"))
{
// Your code here...
}
@eses Hey eses, thanks for your comment. Thats because Quaternion.Slerp and transform.Translate need to be run every frame to move the player.
Your answer
Follow this Question
Related Questions
2D: Why does my character stutter if I add deltaTime to moveSpeed? 1 Answer
Checking another script on update/Efficiency 2 Answers
How move object on Bezier path? 0 Answers
GetKeyUp does not get called sometimes. 1 Answer
Server Command Call not working when inside an if which checks for hasAuthority and a bool 0 Answers