유니티 우클릭은 keycode 가뭐야
- User
유니티 우클릭은 keycode 가뭐야
- Assistant
유니티에서 마우스 오른쪽 버튼을 감지하기 위해서는
KeyCode대신Input.GetMouseButtonDown(int button)또는Input.GetMouseButton(int button)메소드를 사용해야 합니다. 마우스 버튼은 정수 값으로 매핑되며, 오른쪽 버튼은 1로 매핑됩니다.예를 들어, 오른쪽 마우스 버튼이 눌렸는지 체크하려면 다음과 같이 작성할 수 있습니다.
void Update() { if (Input.GetMouseButtonDown(1)) { // 오른쪽 마우스 버튼이 눌렸을 때 실행되는 코드 Debug.Log("오른쪽 마우스 버튼이 눌렸습니다."); } }Input.GetMouseButtonUp(int button)을 사용하여 마우스 버튼이 떼어지는 순간을 감지할 수도 있습니다.void Update() { if (Input.GetMouseButtonUp(1)) { // 오른쪽 마우스 버튼이 떼어졌을 때 실행되는 코드 Debug.Log("오른쪽 마우스 버튼이 떼어졌습니다."); } }이러한 메소드를 사용하여 마우스 버튼 클릭 상태를 감지할 수 있습니다.
- User
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class exam05 : MonoBehaviour { Animator animator;
// Start is called before the first frame update void Start() { animator = GetComponent<Animator>(); } // Update is called once per frame void Update() { if(Input.GetKeyDown(KeyCode.Space)) { animator.SetBool("isAtk", true); // "isAtk"를 true로 설정 } if(Input.GetKeyDown(KeyCode.H)) { animator.SetBool("hurt", true); // "hunt"를 true로 설정 } }} using System.Collections; using System.Collections.Generic; using UnityEngine;
public class exam04main : MonoBehaviour { public GameObject boom_prefab; // Start is called before the first frame update void Start() {
} // Update is called once per frame void Update() { if(Input.GetMouseButtonDown(1)) { Vector3 mousePos=Camera.main.ScreenToWorldPoint(Input.mousePosition); mousePos.z=0; Instantiate(boom_prefab,mousePos,Quaternion.identity); } }} using System.Collections; using System.Collections.Generic; using UnityEngine;
public class boom : MonoBehaviour { // Start is called before the first frame update void Start() {
- Assistant
유니티에서 애니메이션이 끝났을 때 다른 애니메이션이 시작되게 하기 위해서는 애니메이션 이벤트와 트리거를 활용할 수 있습니다.
Animator컴포넌트와Animation이벤트를 이용해 앞서 작성한 코드와 결합하여 원하는 동작을 구현할 수 있습니다.먼저,
exam05클래스에서 "hurt" 애니메이션이 끝났을 때 "boom" 애니메이션이 시작되도록 변경합니다.using System.Collections; using System.Collections.Generic; using UnityEngine; public class exam05 : MonoBehaviour { Animator animator; // Start is called before the first frame update void Start() { animator = GetComponent<Animator>(); } // Update is called once per frame void Update() { if (Input.GetKeyDown(KeyCode.Space)) { animator.SetBool("isAtk", true); } if (Input.GetKeyDown(KeyCode.H)) { animator.SetBool("hurt", true); } } // 애니메이션 이벤트 콜백 함수 public void OnHurtAnimationEnd() { // "boom" 애니메이션 트리거를 설정한다 animator.SetTrigger("boom"); } }