Pv_log

캐릭터 충돌 시 떨림 현상 개선 본문

Develop Study/Unity

캐릭터 충돌 시 떨림 현상 개선

Priv 2020. 2. 29. 21:25

transform.position을 이용해 캐릭터를 움직일 경우, 충돌 발생 시 떨림 현상이 발생할 수 있음.

물리 계산 과정에서 발생하는 것으로, rigidbody를 사용하면 해결 가능.

(아래 코드 참고)

 

추가) 충돌 이후 비 정상적으로 캐릭터가 떨리면서 움직임이 제대로 제어가 되지 않는 현상이 발생할 경우, Rigidbody 컴포넌트에서 Freeze Rotatiton이 체크되어 있는지 확인할 것.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class RubyController: MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    { 
          
    }
 
    // Update is called once per frame
    void Update()
    {
        float horizontal = Input.GetAxis("Horizontal");
        float vertical = Input.GetAxis("Vertical");
        
        Vector2 position = transform.position;
        position.x = position.x + 3.0f* horizontal * Time.deltaTime;
        position.y = position.y + 3.0f * vertical * Time.deltaTime;
        transform.position = position;
    }
 
}
cs

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public class RubyController : MonoBehaviour
{
   Rigidbody2D rigidbody2d;
    
    // Start is called before the first frame update
    void Start()
    {
        rigidbody2d = GetComponent<Rigidbody2D>();
    }
 
    // Update is called once per frame
    void Update()
    {
        float horizontal = Input.GetAxis("Horizontal");
        float vertical = Input.GetAxis("Vertical");
        
        Vector2 position = rigidbody2d.position;
        position.x = position.x + 3.0f* horizontal * Time.deltaTime;
        position.y = position.y + 3.0f * vertical * Time.deltaTime;
 
        rigidbody2d.MovePosition(position);
    }
}
cs
Comments