update가 아닌 함수 내에서 물체를 부드럽게 이동시키고 싶습니다.
조회수 2835회
Update 함수 내에서는 Lerp와 time.deltaTime을 이용하여 물체를 부드럽게 이동시킬 수 있습니다. 하지만 제가 정의한 함수 내에서는 어떻게 이를 구현시킬 지 모르겠습니다. using System.Collections; using System.Collections.Generic; using UnityEngine;
public class TitanAttack : MonoBehaviour {
public GameObject player;
public GameObject titan;
public GameObject Rhand;
public GameObject Lhand;
private Vector3 position;
private Vector3 Rhandpos;
private float distance;
public float speed;
void smashGo()
{
Vector3 pos = new Vector3(position.x, 10, position.z);
Rhand.transform.position = Vector3.Lerp(Rhandpos, pos, 1);
}
void smashCome()
{
Vector3 pos = new Vector3(position.x, 10, position.z);
Rhand.transform.position = Vector3.Lerp(Rhandpos, pos, 0);
}
void smash()
{
smashGo();
Invoke("smashCome", 3);
}
void swing()
{
}
void attack()
{
position = player.transform.position;
distance = Vector3.Distance(position, titan.transform.position);
if (distance > 26)
{
smash();
}
else
{
swing();
}
}
// Use this for initialization
void Start () {
Rhandpos = new Vector3(-15, 5, 35);
InvokeRepeating("attack", 3, 15);
}
// Update is called once per frame
void Update () {
}
}
참고로 아직 swing함수는 만들지 않았습니다
1 답변
-
Unity랑 C#을 안 써봐서 설치 후 코딩하느라 좀 헤멨네요.
Invoke를 사용하시려는 것 같은데요.
위에 질문자분이 작성하신 코드 그대로는 제가 검증이 안되서 그냥 제가 비행기 하나 띄워놓고 테스트 해 본 코드만 남깁니다.
참고만 해주세요.
using UnityEngine; using System.Collections; public class Aircraft : MonoBehaviour { private Vector3 from; private Vector3 to; private float startTime; private const float totalTime = 5.0f; void go () { to.Set (20, 10, -50); startTime = Time.time; // 매끄러운 애니메이션을 위해 16ms 마다 move 메소드 invoke InvokeRepeating ("move", 0, 0.01667f); } void move () { float deltaTime = Time.time - startTime; if (deltaTime < totalTime) { this.transform.position = Vector3.Lerp (from, to, deltaTime / totalTime); } else { this.transform.position = to; CancelInvoke ("move"); // 애니메이션이 종료되면 invoke repeater 종료 } } // Use this for initialization void Start () { from.Set (0, 0, 0); this.transform.position = from; Invoke ("go", 2); // 2초 후 실행 } // Update is called once per frame void Update () { } }
P.S. 유니티에서는 frame 처리를 위해 Update, FixedUpdate, Coroutine, InvokeRepeating 중에서 특정 상황에는 어떤 것을 사용해야 하는지에 대해 여러 논의들이 있습니다. 이것들을 찾아보시고 지금 수행해야 하는 작업에 알맞는 방법인지 확인이 필요할 듯 하네요.
댓글 입력