Update
또는 FixedUpdate
메서드에서 호출되는 메서드에서이 단계를 수행하십시오. 강체를 사용하는 경우 FixedUpdate
을 사용하는 것이 좋습니다.
먼저 강체를 사용하지 않는 경우 위치에서 점까지의 방향을 찾고 스크립트에 velocity
인스턴스 변수를 정의해야합니다. 이 Rigidbody
을 사용하는 인 경우 rigidbody.velocity
을 대신 사용하십시오. target
은 가속하려고하는 Vector3
위치입니다.
// Use rigidbody.velocity instead of velocity if using a Rigidbody
private Vector3 velocity; // Only if you are NOT using a RigidBody
Vector3 direction = (target - transform.position).normalized;
그런 다음 대상을 이미 통과했는지 여부를 확인해야합니다. 이 검사는 우리가 우리가 필요이 우리 Transform
또는 Rigidbody
을 가속화 수행하면 속도가 같은
// If our velocity and the direction point in different directions
// we have already passed the target, return
if(Vector3.Dot(velocity, direction) < 0)
return;
남아 있는지 확인합니다.
// If you do NOT use rigidbodies
// Perform Euler integration
velocity += (accelMagnitude * direction) * Time.deltaTime;
transform.position += velocity * Time.deltaTime;
// If you DO use rigidbodies
// Simply add a force to the rigidbody
// We scale the acceleration by the mass to cancel it out
rigidbody.AddForce(rigidbody.mass * (accelMagnitude * direction));
나는 이런 일을 할 때 훨씬 더 의미가 있기 때문에 당신이 Rigidbody
를 사용하는 것이 좋습니다.
죄송 합니다만, 그 위치에서 멈추고 있습니다. 스크립트의 세 번째 부분은 어디로 가야합니까? 대답의 두 번째 부분에있는 if 문 안에 있습니까? –
@AlbertoO. * if * if 문이어야합니다. – EvilTak