Unity_beginner #7
210711
unity_beginner #7
- GetComponent
transform 이외에도 다른 component가 많다. 이런 component를 script 에서 읽기위해선 어떻게 하는가?
inspector 옆 설정 -> edit script
-아래 코드는 Ball의 Script이다
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Ball : MonoBehaviour // Ball -> 클래스의 이름 , MonoBehaviour -> 유니티 클래스에서 기본적으로 적어야하는 코드
{
int count = 1;
float startingPoint;
bool shouldPrintOver20 = true;
bool shouldPrintOver30 = true;
// Method
// Start is called before the first frame update 시작될 때
void Start()
{
Rigidbody myRigidbody = GetComponent<Rigidbody>();
Debug.Log("UseGravity?:"+myRigidbody.useGravity);
Debug.Log("Start"); //괄호안의 내용을 콘솔에 출력
startingPoint = transform.position.z;
}
// Update is called once per frame 매 프레임마다
void Update()
{
float distance;
distance = transform.position.z - startingPoint;
if(distance > 30)
{
if (shouldPrintOver30)
{
Debug.Log("Over 30:" + distance);
shouldPrintOver30 = false;
}
}
else if(distance > 20)
{
if (shouldPrintOver20)
{
Debug.Log("Over 20:" + distance);
shouldPrintOver20 = false;
}
}
}
}
useGravity 를 사용하는지 여부를 출력하도록 하였다.
- 카메라가 공을 따라가도록 하기
C# script -> CameraWork 생성 -> camera에 component로 추가
-edit
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraWork : MonoBehaviour
{
GameObject ball;
// Start is called before the first frame update
void Start()
{
ball = GameObject.Find("Ball");
}
// Update is called once per frame
void Update()
{
Debug.Log("I am Camera. And ball is at " + ball.transform.position.z);
transform.position = new Vector3(0, ball.transform.position.y+3, ball.transform.position.z - 14);
}
}
카메라와 ball의 좌표를 고려해서 해당 좌표 만큼 거리를 두고 위치를 이동시키면 공을 계속 따라가도록 할 수 있음.
참고
https://programmers.co.kr/learn/courses/1/lessons/573#note
Author And Source
이 문제에 관하여(Unity_beginner #7), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@kimhaech/Unitybeginner-7저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)