유니티 7주차 수업 내용 및 코드
2024. 10. 16. 12:46ㆍ게임 프로그래밍/유니티 프로그래밍
반응형
//움직인 위치 알기
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ball_jsy : MonoBehaviour
{
float startingPoint;
void Start()
{
Debug.Log("시작위치");
startingPoint = transform.position.z;
Debug.Log(transform.position.z);
}
void Update()
{
Debug.Log("움직인 위치");
Debug.Log(transform.position.z);
}
}
실습1 시작부터 현재의 위치까지 움직인 거리 출력
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ball_jsy : MonoBehaviour
{
float startingPoint; // 시작위치
float distance; // 움직인거리
void Start()
{
startingPoint = transform.position.z; //처음 위치
Debug.Log("시작위치" + startingPoint);
}
void Update()
{
Debug.Log("움직인 위치" + transform.position.z);
distance = transform.position.z - startingPoint; // 현재위치 - 처음위치
Debug.Log("움직인거리->" + distance);
}
}
//실습2 x축 움직이기
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ball_jsy : MonoBehaviour
{
float startingPoint; // 시작위치
float distance; // 움직인거리
void Start()
{
startingPoint = transform.position.x; // 처음위치
Debug.Log("x축 시작위치" + startingPoint);
}
void Update()
{
Debug.Log("x축 현재위치" + transform.position.x);
distance = transform.position.x - startingPoint; // 현재위치 - 처음위치
Debug.Log("x축 움직인거리->" + distance);
}
}
//좌우로 왔다갔다 하는 소스
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Trap_jsy : MonoBehaviour
{
float delta = 0.1f;
void Start()
{
}
void Update()
{
float newPosition = transform.position.x + delta; // 좌우로 움직임
transform.position = new Vector3(newPosition, transform.position.y, transform.position.z);
if (transform.position.x < -4.26) //-4.26은 실린더의 왼쪽 X좌표 값
{
delta = 0.1f; // 실린더가 왼쪽 끝에 오면 오른쪽으로 움직임
}
else if (transform.position.x > 4.26)
{ delta = -0.1f; }
}
}
//실습3 위아래로 움직이는 소스 y축
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Trap_jsy : MonoBehaviour
{
float delta = 0.1f;
void Start()
{
}
void Update()
{
float newPosition = transform.position.y + delta;
transform.position = new Vector3(transform.position.x, newPosition, transform.position.z);
if (transform.position.y < 0)
{
delta = 0.1f;
}
else if (transform.position.y > 5)
{ delta = -0.1f; }
}
}
//실습4 앞뒤로 왔다 갔다 하는 프로그램
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Trap_jsy : MonoBehaviour
{
float delta = 0.1f;
void Start()
{
}
void Update()
{
float newPosition = transform.position.z + delta; // 앞뒤로 움직이게 함
transform.position = new Vector3(transform.position.x, transform.position.y, newPosition);
if (transform.position.z < -22) // z좌표 값
{
delta = 1f; // 공이 판자 끝에 오면 반대방향으로 움직임
}
else if (transform.position.z > 15)
{ delta = -1f; }
}
}
// 실습 번외 대각선으로 움직이게 하기 1
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Trap_jsy : MonoBehaviour
{
float delta = 0.1f;
void Start()
{
}
void Update()
{
float newPositionx = transform.position.x + delta;
float newPositionz = transform.position.z + delta;
transform.position = new Vector3(newPositionx, transform.position.y, newPositionz);
if (newPositionx < -15 || newPositionz < -15)
{
delta = 0.5f;
}
else if (newPositionx > 15 || newPositionz > 15)
{ delta = -0.5f; }
}
}
// 대각선으로 움직이게 하기 2
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Trap_jsy : MonoBehaviour
{
float delta = 0.1f;
void Start()
{
}
void Update()
{
float newPosition = transform.position.x + delta; // 좌우로 움직임
transform.position = new Vector3(newPosition, transform.position.y, transform.position.z);
if (transform.position.x < -4.26) //-4.26은 실린더의 왼쪽 X좌표 값
{
delta = 0.1f; // 실린더가 왼쪽 끝에 오면 오른쪽으로 움직임
}
else if (transform.position.x > 4.26)
{ delta = -0.1f; }
float newPosition2 = transform.position.z + delta; // 앞뒤로 움직이게 함
transform.position = new Vector3(transform.position.x, transform.position.y, newPosition2);
if (transform.position.z < -22) // z좌표 값
{
delta = 1f; // 공이 판자 끝에 오면 반대방향으로 움직임
}
else if (transform.position.z > 15)
{ delta = -1f; }
}
}
//실습5 중력사용? False or true가 나오게 하는 코드
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GetCom_jsy : MonoBehaviour
{
Rigidbody mRigidbody;
void Start()
{
mRigidbody = GetComponent<Rigidbody>();
Debug.Log("중력사용?" + mRigidbody.useGravity);
}
void Update()
{
}
}
//실습6 실행 후 공을 클릭하면 공이 아래로 떨어지는 코드
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GetCom_jsy : MonoBehaviour
{
Rigidbody mRigidbody;
void Start()
{
mRigidbody = GetComponent<Rigidbody>();
Debug.Log("중력사용?" + mRigidbody.useGravity);
}
private void OnMouseDown()
{
mRigidbody.useGravity = true;
}
void Update()
{
}
}
// 충돌체의 반지름이 점점 커지는 코드
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GetCom_jsy : MonoBehaviour
{
Rigidbody mRigidbody;
SphereCollider mCollider;
void Start()
{
mRigidbody = GetComponent<Rigidbody>();
Debug.Log("중력사용?" + mRigidbody.useGravity);
mCollider = GetComponent<SphereCollider>();
}
void Update()
{
mCollider.radius = mCollider.radius + 0.01f;
Debug.Log("공의 반지름은?" + mCollider.radius);
}
}
//실습7 마찰력이 점점 세져서 공이 내려가다가 정지하는 코드
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GetCom_jsy : MonoBehaviour
{
Rigidbody mRigidbody;
SphereCollider mCollider;
void Start()
{
mRigidbody = GetComponent<Rigidbody>();
Debug.Log("중력사용?" + mRigidbody.useGravity);
mCollider = GetComponent<SphereCollider>();
}
void Update()
{
mRigidbody.drag = mRigidbody.drag + 0.01f;
Debug.Log("공의 마찰력은?" + mRigidbody.drag);
}
}'게임 프로그래밍 > 유니티 프로그래밍' 카테고리의 다른 글
| 12주차 유니티 게임프로그래밍 (0) | 2024.12.10 |
|---|---|
| 11주차 유니티 게임 프로그래밍 (0) | 2024.12.10 |
| 10주차 유니티 게임프로그래밍 (0) | 2024.12.10 |
| 9주차 유니티 게임 프로그래밍 (0) | 2024.12.10 |
| 4주차 유니티 프로그래밍 (0) | 2024.09.25 |