'유니티/스크립트'에 해당되는 글 123건

  1. 2014.02.17 타워에서 가까운 적을 판별 하는 스크립트
  2. 2014.02.16 적이 메인카메라 쪽 바라보게 하기
  3. 2014.02.13 플레이어 가속도 주는 함수.
  4. 2014.02.06 Vector3.Slerp 두 벡터 사이의 곡선 이동 (1)
  5. 2014.02.06 자식오브젝트에서 부모 오브젝트 스크립트 실행
  6. 2014.02.06 OnMouseDown 및 List 활용한 이웃 블록 저장.
  7. 2014.02.06 큐브를 사용해서 커다란 보드 만들기.
  8. 2014.02.05 유니티 2D 메카닉 애니메이션 관련
  9. 2014.02.05 레이어 끼리의 충돌 무시하기
  10. 2014.02.04 유니티 내에서 List 사용시

타워에서 가까운 적을 판별 하는 스크립트

|



InvokeRepeating 함수는 반복해서 첫번째 인자에 전달된 함수를 호출하는 API입니다.

두번째 인자는 첫 함수를 언제 호출할지 결정되는 인자이며 세번째 인자는 얼마나 자주 반복할지 결정되는 인자입니다. 

위 스크립트에서는 0초만에 함수를 호출하고 1초마다 함수를 반복해주는 스크립트입니다.


Update 문에서는 DrawLine을 그려주어서 잘 판별되는지 파악하고


getClosestEnemy 함수에서는 해당되는 태그를 찾아서( 이 스크립트에서는 enemyAim) taggedEnemys 변수에 집어 넣고 for문을 돌려주어서


타워의 위치에서 적의 위치를 판별하여 sqrMagnitude로 벡터의 길이를 구해줍니다. 그 길이가 3.0 보다 작을때는 해당적의 트랜스폼을 저장하고 거리또한 저장해줍니다.


그리고 마지막으로 타겟에 변수를 저장해줍니다.







Vector3.sqrMagnitude

var sqrMagnitude: float;
Description

Returns the squared length of this vector (Read Only).

The magnitude of a vector v is calculated as Mathf.Sqrt(Vector3.Dot(v, v)). However, the Sqrt calculation is quite complicated and takes longer to execute than the normal arithmetic operations. Calculating the squared magnitude instead of using themagnitude property is much faster - the calculation is basically the same only without the slow Sqrt call. If you are using magnitudes simply to compare distances, then you can just as well compare squared magnitudes against the squares of distances since the comparison will give the same result.

See Also: magnitude.
	// detects when the other transform is closer than closeDistance
	// this is faster than using Vector3.magnitude

var other : Transform; var closeDistance = 5.0; function Update() { if (other) { var offset = other.position - transform.position; var sqrLen = offset.sqrMagnitude; // square the distance we compare with if( sqrLen < closeDistance*closeDistance ) print ("The other transform is close to me!"); } }


Trackback 0 And Comment 0

적이 메인카메라 쪽 바라보게 하기

|
y축으로만 회전할수 있도록 x,z 값은 0으로 만들어주고 Lookat함수를 사용해서 카메라를 바라보게함
Trackback 0 And Comment 0

플레이어 가속도 주는 함수.

|
플레이어 가속도 주는 함수 IncrementTowards sign 함수로 음수인지 양수인지 검사후 n에 가속도를 주어서 더해줌
Trackback 0 And Comment 0

Vector3.Slerp 두 벡터 사이의 곡선 이동

|


두 벡터 사이의 곡선이동

start 지점과 end 지점중 하나의 좌표가 0,0,0 이면 그냥 직선이동을 하는것 같다.

그래서 아래 함수 처럼 센터를 구한다음 센터의 값을빼고  다시 더해주는식으로 무조건 곡선이동을 할수 있게 하는듯.

선형보간법이라는게 참 어렵다..




Vector3.Slerp


static Vector3 Slerp(Vector3 fromVector3 to, float t);
Description

Spherically interpolates between two vectors.

Interpolates between from and to by amount t. The difference between this and linear interpolation (aka, "lerp") is that the vectors are treated as directions rather than points in space. The direction of the returned vector is interpolated by the angle and its magnitude is interpolated between the magnitudes of from and to.

t is clamped between [0...1]. See Also: Lerp function.
using UnityEngine;
using System.Collections;

public class Example : MonoBehaviour {
    public Transform sunrise;
    public Transform sunset;
    public float journeyTime = 1.0F;
    private float startTime;
    void Start() {
        startTime = Time.time;
    }
    void Update() {
        Vector3 center = (sunrise.position + sunset.position) * 0.5F;
        center -= new Vector3(0, 1, 0);
        Vector3 riseRelCenter = sunrise.position - center;
        Vector3 setRelCenter = sunset.position - center;
        float fracComplete = (Time.time - startTime) / journeyTime;
        transform.position = Vector3.Slerp(riseRelCenter, setRelCenter, fracComplete);
        transform.position += center;
    }
}


Trackback 0 And Comment 1
  1. BlogIcon 2 2015.05.27 09:21 address edit & del reply

    대단하군요

자식오브젝트에서 부모 오브젝트 스크립트 실행

|


OnTriggerEnter 는 충돌이 일어날때 발생되는 함수
OnTriggerExit 는 해당 충돌한 물체와 충돌에서 벗어날때 발생하는 함수

OnTriggerEnter 가 발생하기 위해서는 해당물체중 하나는 Rigidboyd 가 추가 되어야만함
Gem 프리팹에 리지드바드 추가후 충돌이 제대로 일어나는지 판단.
충돌이 일어나면 Feeler 에서 Gem 스크립트의 함수를 호출해서 주변 큐브의 정보를 더하고 빼주는 로직을 실행한다.

충돌 발생시 보통 태그나 변수값등을 지정해서 구분해주는데. 현재는 태그값으로 지정해서 구분해줌

박스 콜라이더 옵션중에서는 튕기는 옵션중에 공 처럼 튕긴다던가 뭐 여러가지 튕기는 옵션이있음
Physical Material 에 따라 여러가지 상황이 발생할수있슴.

유니티에서 기본적으로 제공하는 Bouncy 메트리얼을 추가해주면 공처럼 튕김.
Rigidbody 추가시 물리 효과에 따라서 x,y,z 축은 고정해주는 옵션이 있음
Constraints 에서 y를 제외한 나머지는 다 체크해주면 2D 게임에 적합하게 됨.


Trackback 0 And Comment 0

OnMouseDown 및 List 활용한 이웃 블록 저장.

|

Board 스크립트 수정 List -> GameObject 에서 Gem 으로 수정 Gem 스크립트 저장

Gem 스크립트 OnMouseDown 으로 오브젝트의 마우스 클릭 판단가능

Gem 스크립트 Neighbors로 상하좌우 블록 저장

Gem 스크립트의 상하좌우에 박스 콜라이더 생성후 충돌시  List에 저장

Gem 오브젝트에 Feeler 큐브를 자식으로 놓고 스케일을 1, 0.2, 0.2 로 만들어 길쭉한 형태로 만들고 상하좌우에 붙인다.
 
그리고 큐브 메시필터와   메쉬 렌더러 삭제후 박스 콜라이더에는 isTrigger 온으로 설정한다.









Trackback 0 And Comment 0

큐브를 사용해서 커다란 보드 만들기.

|






빈게임 오브젝트 Board 에 위 스크립트 할당 

큐브로 만든 Gem 프리팹 할당





Trackback 0 And Comment 0

유니티 2D 메카닉 애니메이션 관련

|

apply Root Motion -> 애니메이션의 루트 기준점의 움직임에 따라서 게임 오브젝트가 같이 이동합니다.


 


Trackback 0 And Comment 0

레이어 끼리의 충돌 무시하기

|

Physics2D.IgnoreLayerCollision(8,9);


Start함수에 이걸 적어주면 해당되는 번호의 레이어끼리는 충돌을 무시한다.

레이어 번호는 Tags & Layers 에서 볼수 있다.


아래는 API







Physics2D.IgnoreLayerCollision

static void IgnoreLayerCollision(int layer1, int layer2, bool ignore = true);
Parameters

layer1ID of the first layer.
layer2ID of the second layer.
ignoreShould collisions between these layers be ignored?
Description

Choose whether to detect or ignore collisions between a specified pair of layers.


Trackback 0 And Comment 0

유니티 내에서 List 사용시

|

using System.Collections.Generic;를 추가해줘야 된다.

private List<GameObject> _objList = new List<GameObject>(); 이런식으로 선언후



_objList .Add(gameObject) ; ->이런식으로 추가할수있다.


참조할때는 배열 처럼 참조하면된다.

Vector3 pos = _objList[0].transform.position; 

이런식으로

Trackback 0 And Comment 0
prev | 1 | ··· | 3 | 4 | 5 | 6 | 7 | 8 | 9 | ··· | 13 | next