[Unity] Rotate, Raycast, Raycast충돌, 우주선 총쏘기, 우주선 움직이기

APP 2019. 12. 6. 17:16

 

 

 

SampleScene.unity
0.03MB
SampleScene.unity.meta
0.00MB
test1.unity
0.13MB
test1.unity.meta
0.00MB
test2.unity
0.04MB
test2.unity.meta
0.00MB
test3.unity
0.03MB
test3.unity.meta
0.00MB
test4.unity
0.02MB
test4.unity.meta
0.00MB

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class App : MonoBehaviour
{
    public GameObject cube;
    public Button btn1;
    public Button btn2;
    public Button btn3;
    public Vector3 angle;
    // Start is called before the first frame update
    void Start()
    {
        Debug.Log(cube.transform.rotation);
        Debug.Log(cube.transform.rotation.eulerAngles);

        this.btn1.onClick.AddListener(() =>
        {
            Rotate();

        });


        this.btn2.onClick.AddListener(() =>
        {
            Rotate2();

        });

        this.btn3.onClick.AddListener(() =>
        {
            Rotate3();

        });


    }
    public void Rotate()
    {
        
        this.angle = new Vector3(0, 15, 0)+ cube.transform.rotation.eulerAngles;

        cube.transform.rotation = Quaternion.Euler(angle);
    }
    public void Rotate2()
    {
       StartCoroutine(RotateImpl());
    }
    public void Rotate3()
    {
         this.angle = new Vector3(0, 15, 0);
        cube.transform.Rotate(angle);
    }
    IEnumerator RotateImpl()
    {
        while(true)
        {
             this.angle = new Vector3(0, 15*Time.deltaTime, 0) + cube.transform.rotation.eulerAngles;
            cube.transform.rotation = Quaternion.Euler(angle);
            yield return null;
        }

        
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Bullet : MonoBehaviour
{   
    public Vector3[] dir = new Vector3[3];

    // Start is called before the first frame update
    void Start()
    {
        this.dir[0] = this.transform.up;
        this.dir[1] = new Vector3(-0.7f, 0.7f, 0);
        this.dir[2] = new Vector3(0.7f, 0.7f, 0);
    }
    public static Bullet CreateBullet(int num,GameObject bulletPrefab,Transform initPoint)
    {        
        var bulletGo = new GameObject("Bullet");        
        Bullet bullet = bulletGo.AddComponent<Bullet>();       
        var model = Instantiate(bulletPrefab);
        model.transform.SetParent(bullet.transform, false);
        bullet.transform.position = initPoint.position;
        return bullet;
    }
    public void MoveUp()
    {
        StartCoroutine(MoveImplUp());
    }
    public void MoveLeft()
    {
        StartCoroutine(MoveImplLeft());
    }
    public void MoveRight()
    {
        StartCoroutine(MoveImplRight());
    }

    IEnumerator MoveImplUp()
    {
        while (true)
        {
            yield return null;
            this.transform.position += this.dir[0] * 5f * Time.deltaTime;
            if (this.transform.position.y > 11f)
            {
                StopAllCoroutines();
                Destroy(this.gameObject);
            }
        }
    }
    IEnumerator MoveImplLeft()
    {
        while (true)
        {
            yield return null;
            this.transform.position += this.dir[1] * 5f * Time.deltaTime;
            if (this.transform.position.x < -4f)
            {
                StopAllCoroutines();
                Destroy(this.gameObject);
            }
        }
    }
    IEnumerator MoveImplRight()
    {
        while (true)
        {
            yield return null;
            this.transform.position += this.dir[2] * 5f * Time.deltaTime;
            if (this.transform.position.x > 4f)
            {
                StopAllCoroutines();
                Destroy(this.gameObject);
            }
        }
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Hero : MonoBehaviour
{
    public Animation anim;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Ship : MonoBehaviour
{
    public Transform bulletInitPoint;
    public Transform bulletInitPointLeft;
    public Transform bulletInitPointRight;
    // Start is called before the first frame update
    void Start()
    {
        
    }
    public void Shoot(GameObject bulletPrefab)
    {
        var bulletGo = new GameObject("Bullet");
        Bullet bullet =bulletGo.AddComponent<Bullet>();
        var model = Instantiate(bulletPrefab);
        model.transform.SetParent(bullet.transform, false);
        bullet.transform.position = this.bulletInitPoint.position;
        bullet.MoveUp();
    }
    public void ShootOneBullet(GameObject bulletPrefab)
    {
        StartCoroutine(ShootOneImpl(bulletPrefab));
    }
    public void ShootThreeBullet(GameObject bulletPrefab)
    {
        StartCoroutine(ShootThreeImpl(bulletPrefab));
    }
    IEnumerator ShootOneImpl(GameObject bulletPrefab)
    {
        while (true)
        {
            yield return new WaitForSeconds(0.2f);
            var bulletGo = new GameObject("Bullet");
            Bullet bullet = bulletGo.AddComponent<Bullet>();
            var model = Instantiate(bulletPrefab);
            model.transform.SetParent(bullet.transform, false);
            bullet.transform.position = this.bulletInitPoint.position;
            bullet.MoveUp();
        }        
    }
    IEnumerator ShootThreeImpl(GameObject bulletPrefab)
    {
        while (true)
        {
            yield return new WaitForSeconds(0.2f);
            var bullet1 = Bullet.CreateBullet(0,bulletPrefab,this.bulletInitPoint);
            var bullet2 = Bullet.CreateBullet(0, bulletPrefab, this.bulletInitPointLeft);
            var bullet3 = Bullet.CreateBullet(0, bulletPrefab, this.bulletInitPointRight);
            bullet1.MoveUp();
            bullet2.MoveLeft();
            bullet3.MoveRight();
        }
    }
}

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class test1 : MonoBehaviour
{
    public Button btn1;
    public Button btn2;
    public GameObject hero1;
    public GameObject hero2;
    public Vector3 a;
    public Vector3 b;
    private void Start()
    {

        btn1.onClick.AddListener(() =>
        {
            StartCoroutine(GetAngle());
            //GetAngle();
        });
        btn2.onClick.AddListener(() =>
        {
            //StartCoroutine(GetAngle());
            //GetNumbers();
            GetDot();
        });

    }
    void GetDot()
    {
        this.a = this.hero1.transform.forward;
        this.b = this.hero2.transform.position - this.hero1.transform.position;
       Debug.Log(Vector3.Dot(a, b));
    }

    private void GetNumbers()
    {
        this.a = this.hero1.transform.forward;
        this.b = this.hero2.transform.position - this.hero1.transform.position;

        var d = Vector3.Angle(this.a, this.b);
        Debug.Log(d);
        //    var getA = this.hero1.transform.forward;
        //    var getB = this.hero1.transform.rotation.eulerAngles.normalized;
        //    var getC = this.hero1.transform.forward + this.hero1.transform.rotation.eulerAngles.normalized;
        //    Debug.Log($"a.tr.foward:{getA}");
        //    Debug.Log($"a.tr.ro.eulerAngles:{getB}");
        //    Debug.Log($"a.tr.foward*eulerAngles:{getC}");
        //    Debug.Log($"{Vector3.Angle(getC, this.b)}");
    }


    IEnumerator GetAngle()
    {
        //Debug.Log($"hero1:{a} hero2:{b}");
        //Debug.Log($"hero1-hero2:{a - b} hero2-hero1:{b - a}");

        while (true)
        {
            yield return null;
            this.a = this.hero1.transform.forward;
            this.b = this.hero2.transform.position - this.hero1.transform.position;

            var d = Vector3.Angle(this.a, this.b);
            Debug.Log($"a is forward:{this.a} degree:{d}");
            if (d < 180)
            {

                this.hero1.transform.rotation = Quaternion.Euler(this.hero1.transform.rotation.eulerAngles + new Vector3(0, d * Time.deltaTime*10f, 0));
            }
            else
            {
                this.hero1.transform.rotation = Quaternion.Euler(this.hero1.transform.rotation.eulerAngles + new Vector3(0, d * Time.deltaTime*10f, 0));
            }
            if (d < 0.3f)
            {
                break;
            }
        }
    }

    public float GetAngle2(Vector3 from, Vector3 to)
    {
        Vector3 v = to - from;
        return Mathf.Atan2(v.y, v.z) * Mathf.Rad2Deg;
    }

    public float GetAngle3()
    {
        var aDotB = Vector3.Dot(this.a, this.b);
        var ab = this.a.magnitude * this.b.magnitude;
        var angle = Math.Acos(aDotB / (this.a.magnitude * this.b.magnitude));
        Debug.Log(aDotB);
        Debug.Log(ab);
        Debug.Log(angle);
        return (float)angle * Mathf.Rad2Deg;
    }
}

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class test2 : MonoBehaviour
{
    public Camera mainCam;
    public GameObject hero;
    public Animation anim;
    RaycastHit hit;
    private bool isMove;
    Vector3 hitPoint;
    public Coroutine coroutine;
    public Action OnStartMove;

    // Start is called before the first frame update
    void Start()
    {
        this.mainCam = Camera.main;
        this.anim = this.hero.GetComponent<Animation>();
        OnStartMove = () =>
        {
            Move();
        };
    }

    public void Move()
    {
        if(coroutine!=null)
        {
            StopCoroutine(coroutine);
        }
     
        coroutine= StartCoroutine(MoveImpl());
    }
    IEnumerator MoveImpl()
    {

        while (true)
        {
            yield return null;

            Vector3 vc = this.hitPoint - this.hero.transform.position;
            float dis = vc.magnitude;
            Vector3 dir = vc.normalized;
            //Debug.Log($"MoveImpl: from:{this.hero.transform.position} to:{this.hitPoint} dis{dis}, dir{dir}");
            if (dis > 0.1f)
            {
                //Debug.Log(to);
                float angle = Vector3.SignedAngle(this.hero.transform.forward, vc, this.hero.transform.up);
                Debug.Log($"angle{angle}");
                float moveAngle = Mathf.Log10(angle);                
                if (angle>1f)
                {
                    
                    this.hero.transform.rotation *= Quaternion.Euler(new Vector3(0, moveAngle*Time.deltaTime, 0));
                }               
                if(angle<1f)
                {
                    this.hero.transform.position += dir * 1f * Time.deltaTime;
                    this.anim.Play("run@loop");
                }             

            }
            else
            {
                this.hero.transform.position = this.hitPoint;
                this.anim.Play("idle@loop");
            }
        }
    }

    // Update is called once per frame
    void Update()
    {
        //Debug.DrawRay(Vector3.zero, Vector3.forward);
        //Debug.Log($"hitpoint:{this.hitPoint}, hero.tr.po: {this.hero.transform.position}");


        if (Input.GetMouseButtonUp(0))
        {
            Debug.Log("Click!");
            Debug.Log($"{Input.mousePosition}");
            var ray = this.mainCam.ScreenPointToRay(Input.mousePosition);
            Debug.DrawRay(ray.origin, ray.direction * 1000, Color.red, 1f);            
            if (Physics.Raycast(ray, out this.hit))
            {
                Transform objectHit = hit.transform;
                Debug.Log($"맞은 대상: {objectHit}");
                this.hitPoint = this.hit.point;
                OnStartMove();
            }
        }
    }
}

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Test3 : MonoBehaviour
{
    public Ship ship;
    public GameObject bulletPrefab;
    public Camera mainCam;
    public Button btn1;
    //public GameObject collider;
    public RaycastHit hit;
    public Vector3 pos;
    // Start is called before the first frame update
    void Start()
    {
        //Debug.Log(pos.transform.position.normalized);
        this.btn1.onClick.AddListener(() =>
        {
            //this.ship.Shoot(bulletPrefab);
            this.ship.ShootThreeBullet(bulletPrefab);
        });
    }
    public void Update()
    {
        if (Input.GetMouseButton(0))
        {
            var ray = mainCam.ScreenPointToRay(Input.mousePosition);
            Debug.DrawRay(ray.origin, ray.direction * 1000, Color.red, 1f);
            if (Physics.Raycast(ray, out this.hit))
            {
                //if (this.ship.transform.position.x < -3.33f)
                //{
                //    this.pos = this.hit.point;
                //    Vector3 temp = this.pos;
                //    temp.x = -3;
                //    this.ship.transform.position = temp;
                //    Debug.Log($"ShipPos:{ this.ship.transform.position} this.point.position{this.pos}");
                //}
                //else if (this.ship.transform.position.y < -1f)
                //{
                //    this.pos = this.hit.point;
                //    Vector3 temp = this.pos;
                //    temp.y = -1f;
                //    this.ship.transform.position = temp;
                //    Debug.Log($"ShipPos:{ this.ship.transform.position} this.point.position{this.pos}");
                //}
                //else if (this.ship.transform.position.x > 3.33f)
                //{
                //    this.pos = this.hit.point;
                //    Vector3 temp = this.pos;
                //    temp.x = 3;
                //    this.ship.transform.position = temp;
                //    Debug.Log($"ShipPos:{ this.ship.transform.position} this.point.position{this.pos}");
                //}
                //else if (this.ship.transform.position.y > 10f)
                //{
                //    this.pos = this.hit.point;
                //    Vector3 temp = this.pos;
                //    temp.y = 9.5f;
                //    this.ship.transform.position = temp;
                //    Debug.Log($"ShipPos:{ this.ship.transform.position} this.point.position{this.pos}");
                //}
                //else
                //{
                //    this.pos = this.hit.point;
                //    this.ship.transform.position = this.pos;
                //}
                this.pos = this.hit.point;
                if (this.pos.x > -3.33f && this.pos.y > -1f && this.pos.x < 3.33f && this.pos.y < 10f)
                {
                    this.ship.transform.position = this.pos;
                }

            }
        }
    }
}

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

public class testBullet : MonoBehaviour
{
    private void OnTriggerEnter(Collider other)
    {
        Debug.Log($"OnTriggerEnter:{other}");
    }

}

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

public class testShip : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

: