블로그 이미지
Every unexpected event is a path to learning for you.

카테고리

분류 전체보기 (2731)
Unity3D (814)
Programming (474)
Server (33)
Unreal (4)
Gamebryo (56)
Tip & Tech (228)
협업 (57)
3DS Max (3)
Game (12)
Utility (136)
Etc (96)
Link (32)
Portfolio (19)
Subject (90)
iOS,OSX (51)
Android (14)
Linux (5)
잉여 프로젝트 (2)
게임이야기 (3)
Memories (20)
Interest (38)
Thinking (38)
한글 (30)
PaperCraft (5)
Animation (408)
Wallpaper (2)
재테크 (18)
Exercise (3)
나만의 맛집 (3)
냥이 (10)
육아 (16)
Total
Today
Yesterday
03-29 00:00

Mathf.SmoothDamp

Unity3D/Math / 2012. 11. 6. 00:00

Mathf.SmoothDamp



함수 원형


static function SmoothDamp ( current : float, 

                                          target : float, 

                                          ref currentVelocity : float, 

                                          smoothTime : float, 

                                          maxSpeed : float = Mathf.Infinity, 

                                          deltaTime : float = Time.deltaTime) : float Parameters

    


매개변수


current             : 현재 위치 

target               : 타겟 위치

currentVelocity  : 호출할때마다 이 함수에 의해 변경( 계산 )되는 현재속도

smoothTime      : 현재 위치에서 목적 위치까지 이르는데 걸리는 시간. 이 값이 작을수록 목적지에 빠르게 도착한다.

maxSpeed        : 스피드의 상한치.

deltaTime         : By default Time.deltaTime.



설명


Gradually changes a value towards a desired goal over time.


The value is smoothed by some spring-damper like function, which will never overshoot.

The function can be used to smooth any kind of value, positions, colors, scalars.


시간 내에 정해진 목표(값)로 점점 값을 변화시킨다.


결코 특정값을 넘지 않게 하는 스프링 제동 기능으로써 값을 자연스럽게 변화하게 한다.

이 함수는 어떤 종류의 값, 위치, 색, 스칼라등의 어떤 종류의 값이라도 자연스럽게 변화시키는데 이용될수 있다.





사용예


using UnityEngine;

using System.Collections;


public class example : MonoBehaviour {


    public Transform target;

    public float smoothTime = 0.3F;

    private float yVelocity = 0.0F;


    void Update() {

        float newPosition = Mathf.SmoothDamp(transform.position.y, target.position.y, ref yVelocity, smoothTime);

        transform.position = new Vector3(transform.position.x, newPosition, transform.position.z);

    }

}





이 함수를 이용하는 목적은 자연스러운( 부드러운 ) 감속, 가속 효과를 얻기 위함이다.

'럽츠 대탈출' 튜토리얼 에선 주인공 캐릭터를 부드럽게 따라오는 카메라의 움직임

- GPG1권에 나온 용수철 카메라 움직임

을 위해 이 함수가 사용되었다.


보통 이 함수는 다음의 형식으로 사용된다.


void Update()

{  

    ...

    current = Mathf.SmoothDamp( current, target, velocity, smoothTime );

    ...

}


시간(smoothTime)이 일정 하다면 시간이 흐를수록 

current 와 target 간의 차이가 줄어들어 결국 속도가 점점 줄어들게 될것이며,

이것을 통해 자연스로운 카메라 움직임을 표현할수 있다.


'럽츠 대탈출' 메인 게임 씬 첫 부분을 보면

카메라가 꽤 멀리떨어진 상태에서 시작해 캐릭터에게 일정한 속도로 다가가는 것을 확인할수 있는데

위의 공식대로(Mathf.SmoothDamp)라면 최초 시작 상태에서 target 과 current 의 차이가 상대적으로 크므로 

속도가 엄청 빨라야할것 같지만

- 시간은 항상 일정하고 거리차( 캐릭터와 카메라 )는 최초 시작이 가장 크므로

일정 기간 동안 속도의 변화(방향은 제외)는 없는 편이다.


이것은 smoothTime 의 다음 매개변수인 maxSpeed 의 역할 덕분인데

현재계산된 속도가 maxSpeed 값을 넘지 못하게 처리된다.


<출처: Unity Script Reference >



출처 : http://blog.naver.com/kzh8055/140154419294

반응형
Posted by blueasa
, |