Unity 3D Developer Question: Download Questions PDF
Explain me The Following Code Snippet Below:
Class Mover : Monobehaviour
{
Vector3 Target;
Float Speed;
Void Update()
{
}
}
Finish This Code So The Gameobject Containing This Script Moves With Constant Speed Towards Target, And Stop Moving Once It Reaches 1.0, Or Less, Units Of Distance?
![Unity 3D Developer Interview Answer Unity 3D Developer Interview Question](https://globalguideline.com/public/images/interview/3.png)
Answer:
class Mover : MonoBehaviour
{
Vector3 target;
float speed;
void Update()
{
float distance = Vector3.Distance(target,transform.position);
// will only move while the distance is bigger than 1.0 units
if(distance > 1.0f)
{
Vector3 dir = target - transform.position;
dir.Normalize(); // normalization is obligatory
transform.position += dir * speed * Time.deltaTime; // using deltaTime and speed is obligatory
}
}
}
{
Vector3 target;
float speed;
void Update()
{
float distance = Vector3.Distance(target,transform.position);
// will only move while the distance is bigger than 1.0 units
if(distance > 1.0f)
{
Vector3 dir = target - transform.position;
dir.Normalize(); // normalization is obligatory
transform.position += dir * speed * Time.deltaTime; // using deltaTime and speed is obligatory
}
}
}
Download Unity 3D Developer Interview Questions And Answers
PDF