using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CSightVisualizer : MonoBehaviour
{
    public float radius = 3;


    private void OnDrawGizmos()
    {
        GizmosExtensions.DrawWireArc(this.transform.position, this.transform.forward, 360, radius);
    }
}

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;

public class Capsule : MonoBehaviour
{
    public float speed = 5f;
    void Start()
    {
        
    }

    void Update()
    {
        Move();
    }

    private void Move()
    {
        float h = Input.GetAxis("Horizontal");
        float v = Input.GetAxis("Vertical");
        Vector3 movement = new Vector3(h, 0, v);
        this.transform.Translate(movement * speed * Time.deltaTime);
        
    }
}

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Sphere : MonoBehaviour
{
    public float radius = 3f; // 원의 반지름
    public float rotationSpeed = 30f; // 캡슐의 회전 속도 (각도/초)

    private Transform capsuleTransform; // 부모 오브젝트인 캡슐의 Transform

    private void Start()
    {
        // 부모 오브젝트인 캡슐의 Transform 가져오기
        capsuleTransform = transform.parent;
    }

    private void Update()
    {
        if (capsuleTransform != null)
        {
            // 현재 시간에 따른 회전 각도 계산
            float rotationAngle = Time.time * rotationSpeed;

            // 원 주위를 도는 위치 계산
            float x = Mathf.Cos(rotationAngle) * radius;
            float z = Mathf.Sin(rotationAngle) * radius;

            // 부모 오브젝트의 위치를 기준으로 위치 설정
            transform.position = capsuleTransform.position + new Vector3(x, 0f, z);
        }
    }
}

 

Quaternion.

https://docs.unity3d.com/kr/2020.3/Manual/class-Quaternion.html

 

중요 클래스 - Quaternion - Unity 매뉴얼

Unity는 Quaternion 클래스를 사용하여 게임 오브젝트의 3차원 방향을 저장하고, 이를 통해 한 방향에서 다른 방향으로의 상대 회전을 설명합니다.

docs.unity3d.com

https://docs.unity3d.com/ScriptReference/Quaternion.html

 

Unity - Scripting API: Quaternion

They are compact, don't suffer from gimbal lock and can easily be interpolated. Unity internally uses Quaternions to represent all rotations. They are based on complex numbers and are not easy to understand intuitively. You almost never access or modify in

docs.unity3d.com

using UnityEngine;

public class Sphere : MonoBehaviour
{
    public float radius = 3f; // 원의 반지름
    public float rotationSpeed = 30f; // 캡슐의 회전 속도 (각도/초)

    private Transform capsuleTransform; // 부모 오브젝트인 캡슐의 Transform

    private void Start()
    {
        // 부모 오브젝트인 캡슐의 Transform 가져오기
        capsuleTransform = transform.parent;
    }

    private void Update()
    {
        if (capsuleTransform != null)
        {
            // 캡슐의 회전 각도 계산
            float rotationAngle = Time.time * rotationSpeed;

            // 쿼터니언으로 캡슐의 회전을 설정
            Quaternion rotation = Quaternion.Euler(0f, rotationAngle, 0f);

            // 캡슐의 회전을 기반으로 원 주위를 도는 위치 계산
            Vector3 positionOffset = rotation * (Vector3.forward * radius);

            // 부모 오브젝트의 위치를 기준으로 위치 설정
            transform.position = capsuleTransform.position + positionOffset;
        }
    }
}

'산대특 > 게임 클라이언트 프로그래밍' 카테고리의 다른 글

Tank - Rigidbody.MovePosition  (0) 2024.03.06
UniRun  (4) 2024.03.05
Dodge game  (0) 2024.03.04
코루틴 연습  (0) 2024.03.03
StarCraft - Tank  (0) 2024.02.29

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : MonoBehaviour
{
    public float speed = 1f;
    public Rigidbody playerRigidbody;
    public GameObject bulletPrefab;

    void Update()
    {
        PlayerMove();
    }

    private void PlayerMove()
    {
        float h = Input.GetAxis("Horizontal");
        float v = Input.GetAxis("Vertical");
        Vector3 movement = new Vector3(h, 0, v) * speed;
        transform.Translate(movement * Time.deltaTime);
    }

    private void OnTriggerEnter(Collider other)
    {
        // 충돌한 오브젝트가 총알인지 확인
        if (other.CompareTag("Bullet"))
        {
            // 플레이어를 파괴
            Destroy(gameObject);
            Debug.Log("Game over");
        }
    }

}

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Enemy : MonoBehaviour
{
    [SerializeField] GameObject player;
    [SerializeField] public GameObject bulletPrefab;
    private float spawnInterval = 3f; // 총알 생성 간격
    private Vector3 spawnPosition; // 총알이 생성될 위치
    void Start()
    {
        spawnPosition = this.gameObject.transform.position; // 총알이 생성될 위치 초기화
        StartCoroutine(SpawnBullet()); // 코루틴 시작
    }

    IEnumerator SpawnBullet()
    {
        while (player != null)
        {
            // 총알 생성
            Instantiate(bulletPrefab, spawnPosition, Quaternion.identity);

            // 일정한 간격 기다리기
            yield return new WaitForSeconds(spawnInterval);
        }
    }
}

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Bullet : MonoBehaviour
{
    public float speed = 2f;
    public GameObject player;
    void Start()
    {
        player = GameObject.FindGameObjectWithTag("Player");
            // Rigidbody 컴포넌트 가져오기
            Rigidbody bullets = GetComponent<Rigidbody>();
            Vector3 direction = (player.transform.position - transform.position).normalized;
            transform.forward = direction;
            // 총알의 이동 방향 설정 및 속도 적용
            bullets.velocity = transform.forward * speed;

            // 5초 후에 총알 파괴
            Destroy(gameObject, 4.5f);
    }
    private void OnTriggerEnter(Collider other)
    {
        // 충돌한 오브젝트가 총알인지 확인
        if (other.CompareTag("Player"))
        {
            Destroy(gameObject);
        }
        if (other.CompareTag("Wall"))
        {
            Destroy(gameObject);
        }
    }

}

'산대특 > 게임 클라이언트 프로그래밍' 카테고리의 다른 글

UniRun  (4) 2024.03.05
Quaternion  (1) 2024.03.04
코루틴 연습  (0) 2024.03.03
StarCraft - Tank  (0) 2024.02.29
StarCraft - DropShip Logic  (0) 2024.02.28
using System.CodeDom.Compiler;
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.SearchService;

public class coTest : MonoBehaviour
{
    void Start()
    {
        //StartCoroutine(this.Sol1());
        //StartCoroutine(this.Sol2());
    }

    void Update()
    {
       
    }

    //1. 주어진 시간(예: 5초) 동안 특정 작업을 반복 실행하고 그 후에 작업을 중지하는 코루틴을 작성하세요.
    //IEnumerator Sol1()
    //{
    //    Debug.LogFormat("시작"); // 처음에 0초 출력

    //    yield return new WaitForSeconds(1); // 1초 대기

    //    for (int i = 1; i <= 5; i++)
    //    {
    //        Debug.LogFormat("{0}초 경과", i);
    //        yield return new WaitForSeconds(1); // 각 반복마다 1초씩 대기
    //    }

    //    Debug.Log("정지");
    //}

    //2. 리스트에 있는 항목을 하나씩 출력하고 각 항목을 출력한 후에 잠시 대기하는 코루틴을 만들어보세요.
    //IEnumerator Sol2()
    //{
    //    string[] items = { "cap", "clothes", "pants", "shoes" };
    //        foreach (string item in items)
    //        {
    //            Debug.Log(item);
    //            yield return new WaitForSeconds(1);
    //        }
    //        Debug.Log("모든 아이템 출력 완료");
    //}

    //3.플레이어가 특정 키를 누를 때까지 게임을 일시 중지하고, 그 키를 누르면 다시 게임을 계속하는 코루틴을 작성하세요.
 

}

 

using System.Collections;
using UnityEngine;

public class GameLogic : MonoBehaviour
{
    private bool gamePaused = false;

    void Start()
    {
        StartCoroutine(GameLoop());
    }
    //3. 플레이어가 특정 키를 누를 때까지 게임을 일시 중지하고, 그 키를 누르면 다시 게임을 계속하는 코루틴을 작성하세요.
    IEnumerator GameLoop()
    {
        while (true)
        {
            if (!gamePaused)
            {
                // 게임이 진행 중인 경우 여기에 원하는 게임 로직을 추가
                Debug.Log("게임 진행 중");
            }

            // 플레이어가 스페이스바를 누르면 일시 중지 상태를 변경
            if (Input.GetKeyDown(KeyCode.Space))
            {
                gamePaused = !gamePaused;

                if (gamePaused)
                {
                    Debug.Log("게임 일시 중지");
                    Time.timeScale = 0f; // 게임 시간 정지
                }
                else
                {
                    Debug.Log("게임 재개");
                    Time.timeScale = 1f; // 게임 시간 재개
                }
            }

            yield return null;
        }
    }
}

'산대특 > 게임 클라이언트 프로그래밍' 카테고리의 다른 글

Quaternion  (1) 2024.03.04
Dodge game  (0) 2024.03.04
StarCraft - Tank  (0) 2024.02.29
StarCraft - DropShip Logic  (0) 2024.02.28
Find the nearest object  (0) 2024.02.27

+ Recent posts