攻撃モーションが発生してる時だけisAttack変数をtrueにすれば武器スクリプトのOnTriggerEnterのif文を使えばダメージを入れることだできるのだと思ったのですがこれはどうすればいいのでしょうか?
質問 コメント部で囲ってる部分のコードですが一瞬だけisAttackがtrueになりすぐfalseになってしまうのですがどうすれば攻撃モーションが発生してる時だけtrueにできるのでしょうか?
Playerスクリプト
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Player : MonoBehaviour { private float input_h; private float input_v; private Vector3 move; private Rigidbody rb; private float walk_speed;//移動速度 private float run_speed;//歩き速度 private float move_speed;//移動速度 private const float gravity_force = -50.0f; private const float jump_force = 500.0f; private float gravity;//ジャンプの上下数値管理 private Animator ani; AnimatorStateInfo nowState; private GameObject weapon; private bool isJump; private bool isGround; private bool rGround; private bool isSide; private bool isRun; /*外部系*/ public bool isAttack; // Use this for initialization void Start() { walk_speed = 100.0f; run_speed = 200.0f; move_speed = walk_speed; weapon = GameObject.Find("Ethan/Hammer"); isAttack = false; isJump = false; isGround = false; rGround = false; isSide = false; isRun = false; rb = GetComponent<Rigidbody>(); ani = GetComponent<Animator>(); nowState = ani.GetCurrentAnimatorStateInfo(0); } void anime_update() { /*アニメーション*/ float speed = Mathf.Sqrt((move.x * move.x) + (move.z * move.z)); ani.SetFloat("Move_Speed", speed); // Debug.Log(speed); } // Update is called once per frame void Update() { input_h = Input.GetAxis("Horizontal"); input_v = Input.GetAxis("Vertical"); Vector3 move_x = new Vector3(); Vector3 move_y = new Vector3(); move_y = Vector3.Scale(Camera.main.transform.forward, new Vector3(1, 0, 1)).normalized * input_v * move_speed; move_x = Camera.main.transform.right * input_h * move_speed; move = move_x + move_y; if (move != Vector3.zero) { transform.rotation = Quaternion.LookRotation(move.normalized); } anime_update(); ray_update(); //Debug.Log(rb.velocity); /*ジャンプ*/ if (Input.GetKeyDown(KeyCode.Space)) { isJump = true; } else { // Debug.Log("false"); isJump = false; } /*走るCtrlキー*/ if (Input.GetKey(KeyCode.LeftControl)) { move_speed = run_speed; } else { move_speed = walk_speed; } /////////////////////////////////////////////////////////////////////////////// /*攻撃 left shift キー*/ if (Input.GetKeyDown(KeyCode.LeftShift)) { isAttack = true; ani.SetBool("isAttack",isAttack); } else { if (nowState.IsName("BaseLayer.isAttack") == true) { isAttack = true; } else { isAttack = false; } ani.SetBool("isAttack", isAttack); } ///////////////////////////////////////////////////////////////////////////////// Debug.Log("isAttack: "+ isAttack); } void ray_update() { Ray ray = new Ray(transform.position, new Vector3(0,-1,0)); RaycastHit rayhit; float range = 2.0f; Debug.DrawRay(ray.origin, ray.direction * range, Color.red); if(Physics.Raycast(ray,out rayhit,range)) { rGround = true; // Debug.Log("Ray hit!"); } else { // Debug.Log("Ray "); rGround = false; } // Debug.Log(ray.direction); } void FixedUpdate() { if(isJump == true && rGround == true) { // Debug.Log("jump !!!"); move.y = jump_force; isJump = false; rGround = false; rb.velocity = Vector3.zero; } else { move.y = gravity_force; } rb.AddForce(move); } private void OnCollisionEnter(Collision c) { /* if(c.gameObject.tag == "Coin") { // Debug.Log("hit!!"); } */ } void Hit() { } }
武器スクリプト(プレイヤーの子のオブジェクト)
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Weapon : MonoBehaviour { private GameObject player; private Player sPlayer; // Start is called before the first frame update void Start() { player = GameObject.Find("Ethan"); sPlayer = player.GetComponent<Player>(); } // Update is called once per frame void Update() { } private void OnCollisionEnter(Collision c) { } private void OnCollisionStay(Collision c) { } ///////////////////////////////////////////////////////////////////////////////// private void OnTriggerEnter(Collider c) { Debug.Log("ヒット!!!"); if(c.gameObject.tag == "Coin" && sPlayer.isAttack == true) { Destroy(c.gameObject); } } ///////////////////////////////////////////////////////////////////////////////// }
質問内容と無関係な話で恐縮ですが… 私はUnityに触れたことがありませんので,回答依頼を頂いてもお力になれそうにありません.関係タグの付いているユーザへ回答依頼を出されると良いのではないかと存じます.
回答1件
あなたの回答
tips
プレビュー