攻撃力の作成
![E9135965 0286 47eb a898 b5122a0341cc](https://codegenius.org/uploads/slide/image/2081/e9135965-0286-47eb-a898-b5122a0341cc.jpeg)
敵を破壊する
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Destroy : MonoBehaviour {
public AudioClip effectSound;
public GameObject effectPrefab;
void OnTriggerEnter(Collider other){
if (other.CompareTag ("Enemy")) {
Destroy (other.gameObject);
AudioSource.PlayClipAtPoint (effectSound, Camera.main.transform.position);
GameObject effect = (GameObject)Instantiate (effectPrefab, other.transform.position, Quaternion.identity);
Destroy (effect, 0.5f);
}
}
}
![C22352eb bcbd 4004 adfa c14a20f0ebf2](https://codegenius.org/uploads/slide/image/2082/c22352eb-bcbd-4004-adfa-c14a20f0ebf2.jpeg)
![8e9e64fd 0a93 426c a9e0 37312fcc48c0](https://codegenius.org/uploads/slide/image/2083/8e9e64fd-0a93-426c-a9e0-37312fcc48c0.jpeg)
![F9a12789 25b8 4d0a 9a80 c363d6abaa25](https://codegenius.org/uploads/slide/image/2084/f9a12789-25b8-4d0a-9a80-c363d6abaa25.jpeg)
![6944b45b 177c 481f 9a5f 06393911f0fe](https://codegenius.org/uploads/slide/image/2085/6944b45b-177c-481f-9a5f-06393911f0fe.jpeg)
![78aeb6e5 1858 4b3f 989a 4102ee8df858](https://codegenius.org/uploads/slide/image/2086/78aeb6e5-1858-4b3f-989a-4102ee8df858.jpeg)
![Be3c07e7 28e9 46ca b32d 78b9d7c60ece](https://codegenius.org/uploads/slide/image/2087/be3c07e7-28e9-46ca-b32d-78b9d7c60ece.jpeg)
![4a69d359 6ef7 4785 ab81 f36ffb25a1f4](https://codegenius.org/uploads/slide/image/2088/4a69d359-6ef7-4785-ab81-f36ffb25a1f4.jpeg)
![0d9fa792 58c5 4000 8289 46eb092705b6](https://codegenius.org/uploads/slide/image/2089/0d9fa792-58c5-4000-8289-46eb092705b6.jpeg)
パンチを繰り出した時だけコライダーをオンにする
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerAttack : MonoBehaviour {
private Animator anim;
public AudioClip punchSound;
// ★追加
private Collider left_hand_col;
void Start () {
anim = GetComponent<Animator> ();
// ★追加
left_hand_col = GameObject.Find("Character1_LeftHand").GetComponent<SphereCollider>();
}
void Update () {
// Qボタンを押すとパンチ
if (Input.GetKeyDown (KeyCode.Q)) {
anim.SetBool ("Jab", true);
AudioSource.PlayClipAtPoint (punchSound, Camera.main.transform.position);
// ★追加
// 当たり判定をとるためにコライダーをオンの状態にする。
left_hand_col.enabled = true;
// ★追加
// 0.15秒後にコライダーをオフの状態に戻す。
Invoke("Reset", 0.15f);
}
// Eボタンを押すとハイキック
if (Input.GetKeyDown (KeyCode.E)) {
anim.SetBool ("Hikick", true);
}
// Sボタンを押すと回転キック
if (Input.GetKeyDown (KeyCode.S)) {
anim.SetBool ("Spinkick", true);
}
// Zボタンを押すとムーンソルトキック
if (Input.GetKeyDown (KeyCode.Z)) {
anim.SetBool ("SAMK", true);
}
}
// ★追加
void Reset(){
left_hand_col.enabled = false;
}
}
![A80ca4b0 b443 45f6 8f57 6b9501a83393](https://codegenius.org/uploads/slide/image/2090/a80ca4b0-b443-45f6-8f57-6b9501a83393.jpeg)
![2065a981 5444 42cb b1a3 8becf37112ee](https://codegenius.org/uploads/slide/image/2091/2065a981-5444-42cb-b1a3-8becf37112ee.jpeg)
![E9135965 0286 47eb a898 b5122a0341cc](https://codegenius.org/uploads/slide/image/2081/e9135965-0286-47eb-a898-b5122a0341cc.jpeg)
敵を破壊する
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Destroy : MonoBehaviour {
public AudioClip effectSound;
public GameObject effectPrefab;
void OnTriggerEnter(Collider other){
if (other.CompareTag ("Enemy")) {
Destroy (other.gameObject);
AudioSource.PlayClipAtPoint (effectSound, Camera.main.transform.position);
GameObject effect = (GameObject)Instantiate (effectPrefab, other.transform.position, Quaternion.identity);
Destroy (effect, 0.5f);
}
}
}
![C22352eb bcbd 4004 adfa c14a20f0ebf2](https://codegenius.org/uploads/slide/image/2082/c22352eb-bcbd-4004-adfa-c14a20f0ebf2.jpeg)
![8e9e64fd 0a93 426c a9e0 37312fcc48c0](https://codegenius.org/uploads/slide/image/2083/8e9e64fd-0a93-426c-a9e0-37312fcc48c0.jpeg)
![F9a12789 25b8 4d0a 9a80 c363d6abaa25](https://codegenius.org/uploads/slide/image/2084/f9a12789-25b8-4d0a-9a80-c363d6abaa25.jpeg)
![6944b45b 177c 481f 9a5f 06393911f0fe](https://codegenius.org/uploads/slide/image/2085/6944b45b-177c-481f-9a5f-06393911f0fe.jpeg)
![78aeb6e5 1858 4b3f 989a 4102ee8df858](https://codegenius.org/uploads/slide/image/2086/78aeb6e5-1858-4b3f-989a-4102ee8df858.jpeg)
![Be3c07e7 28e9 46ca b32d 78b9d7c60ece](https://codegenius.org/uploads/slide/image/2087/be3c07e7-28e9-46ca-b32d-78b9d7c60ece.jpeg)
![4a69d359 6ef7 4785 ab81 f36ffb25a1f4](https://codegenius.org/uploads/slide/image/2088/4a69d359-6ef7-4785-ab81-f36ffb25a1f4.jpeg)
![0d9fa792 58c5 4000 8289 46eb092705b6](https://codegenius.org/uploads/slide/image/2089/0d9fa792-58c5-4000-8289-46eb092705b6.jpeg)
パンチを繰り出した時だけコライダーをオンにする
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerAttack : MonoBehaviour {
private Animator anim;
public AudioClip punchSound;
// ★追加
private Collider left_hand_col;
void Start () {
anim = GetComponent<Animator> ();
// ★追加
left_hand_col = GameObject.Find("Character1_LeftHand").GetComponent<SphereCollider>();
}
void Update () {
// Qボタンを押すとパンチ
if (Input.GetKeyDown (KeyCode.Q)) {
anim.SetBool ("Jab", true);
AudioSource.PlayClipAtPoint (punchSound, Camera.main.transform.position);
// ★追加
// 当たり判定をとるためにコライダーをオンの状態にする。
left_hand_col.enabled = true;
// ★追加
// 0.15秒後にコライダーをオフの状態に戻す。
Invoke("Reset", 0.15f);
}
// Eボタンを押すとハイキック
if (Input.GetKeyDown (KeyCode.E)) {
anim.SetBool ("Hikick", true);
}
// Sボタンを押すと回転キック
if (Input.GetKeyDown (KeyCode.S)) {
anim.SetBool ("Spinkick", true);
}
// Zボタンを押すとムーンソルトキック
if (Input.GetKeyDown (KeyCode.Z)) {
anim.SetBool ("SAMK", true);
}
}
// ★追加
void Reset(){
left_hand_col.enabled = false;
}
}
![A80ca4b0 b443 45f6 8f57 6b9501a83393](https://codegenius.org/uploads/slide/image/2090/a80ca4b0-b443-45f6-8f57-6b9501a83393.jpeg)
![2065a981 5444 42cb b1a3 8becf37112ee](https://codegenius.org/uploads/slide/image/2091/2065a981-5444-42cb-b1a3-8becf37112ee.jpeg)
攻撃力の作成