-
2020-05-12 [금일 수업 반복 / 구체 캐릭터 따라다니기, 왕복이동]C#/과제 2020. 5. 13. 01:58
다시 회전하는거 해보는데 생각없이 Vector3.right 쓰고 왜 오른쪽으로 안돌지 하고 있었음
직접 회전해보니 Y가 변해야하니까 UP이나 down이 맞음.
APP에서 회전하는
this.shiel.transform.Rotate(Vector3.down * 200 * Time.detaTime);
로 Rotate 적어봤더니 안움직여서 다시 Shield에 적음. APP에서 오브젝트 움직일수 있지 않을까 싶었는데 다시 제대로 알아야 할거 같음. 이 차이 모르는듯 싶음.
Transform.LookAt >>> transform을 회전해서 taget의 현재 위치를 전방 벡터가 가리킴
해당건은 말그대로 바라본다고 이해하는게 좋을거 같음.
근데 아까 왜 LookAt 과 Translate로 움직였는지 ???????
magnitude >> 가르쳐주셨지만 명확하게 잡혀있진 않은거 같음.
해당 설명으로는 벡터의 길이를 반환함.
0,0,0에 오브젝트를 두고 hero 와 사용.
다른 것들 사용하니 거리값이라 증가만하길래 이거 쓰니 목적지 도착후 돌아오면서 다시 줄어들어서 왕복이동가능했음.
https://docs.unity3d.com/kr/530/ScriptReference/Vector3-magnitude.html
Unity - 스크립팅 API: Vector3.magnitude
벡터의 길이는 (x*x+y*y+z*z)의 제곱근입니다. 일부 벡터의 길이만 비교하는 경우에, sqrMagnitude를 사용해서 길이의 제곱값을 비교할 수 있습니다. (길이의 제곱값을 사용하는 경우가 더 빠릅니다.) Se
docs.unity3d.com
우선 구체가 따라다니면서 캐릭터도 왕복 운동은 했음.
버튼 생성하고 없어져있다가 생기는거 하려고했는데 프리팹으로 빼면 객체가 없어 Update 위치 업데이트를 못하는 점으로 숨겼다 안숨겼다 해야하는건지 싶은데 우선 오늘 시간 부족으로 여기까지.
APP 코드 >
더보기12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576using System.Collections;using System.Collections.Generic;using UnityEditor.Rendering;using UnityEditorInternal;using UnityEngine;using UnityEngine.UI;public class APP : MonoBehaviour{public GameObject hero;public Shield shield;public float speed;public GameObject zero;public Button button;private Animation anim;private bool isMove;private Vector3 target;private float elapsedtime;// Start is called before the first frame updatevoid Start(){this.target = new Vector3(0, 0, 5);this.anim = hero.GetComponent<Animation>();//button.onClick.AddListener(() => {// var shieldfab = Resources.Load("Prefabs/Shield") as Shield;// var shieldGO = Instantiate(shieldfab, Vector3.zero, transform.rotation);// this.shield = shieldGO;// this.shield.transform.position = this.hero.transform.position;//});}//public Shield OnShield()//{// var shieldfab = Resources.Load("Prefabs/Shield") as Shield;// var shieldGO = Instantiate(shieldfab, Vector3.zero, transform.rotation);// this.shield = shieldGO;// return this.shield;//}// Update is called once per framevoid Update(){var dis = Vector3.Distance(this.target, this.hero.transform.position);// var backdis = Vector3.Distance(this.zero.transform.position, this.target);this.hero.transform.Translate(Vector3.forward * this.speed * Time.deltaTime);this.shield.transform.position = this.hero.transform.position;this.anim.Play("run@loop");var backdis = (this.zero.transform.position - this.hero.transform.position).magnitude;if (dis <= 0.02f){this.hero.transform.LookAt(this.zero.transform.position);}if (backdis <= 0.02f){this.hero.transform.LookAt(this.target);}//Destroy(this.shield);}}cs Hero / Shield >>
더보기1234567891011121314151617181920using System.Collections;using System.Collections.Generic;using UnityEngine;public class Hero : MonoBehaviour{public GameObject model;// Start is called before the first frame updatevoid Start(){}// Update is called once per framevoid Update(){}}cs 12345678910111213141516171819202122using System.Collections;using System.Collections.Generic;using UnityEngine;public class Shield : MonoBehaviour{public GameObject model;// Start is called before the first frame updatevoid Start(){}// Update is called once per framevoid Update(){this.transform.Rotate(Vector3.down * 200 * Time.deltaTime);}}cs 'C# > 과제' 카테고리의 다른 글
2020-05-11 [unity 복습] (0) 2020.05.12 문제 3 / 문제 5 (0) 2020.04.26 2020-04-23 [JSON 활용 및 초반 과정 및 생각 정리] (0) 2020.04.23 객체지향 3요소 / 5 원칙 둘러보고 간단 서술 (0) 2020.04.23 2020-04-18/19 [1차원 배열 활용] (0) 2020.04.20