質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

Q&A

1回答

711閲覧

Unity 矢印キーを押しながら左クリックを反応させたい

jkoman

総合スコア17

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

0グッド

0クリップ

投稿2019/06/12 05:23

Unityチュートリアルのサバイバルシューターをやっているのですが矢印キーで移動しながら左クリックをすると2発程度でとまってしまいます。ctrlキーですと普通に連射できます。また移動中にctrlキーを押した後ですとその移動中に限り左クリックでも連射ができます。
コードは以下の通りです。

C#

1 2using UnityEngine; 3 4public class PlayerMovement : MonoBehaviour 5{ 6 public float speed = 6f; // The speed that the player will move at. 7 8 Vector3 movement; // The vector to store the direction of the player's movement. 9 Animator anim; // Reference to the animator component. 10 Rigidbody playerRigidbody; // Reference to the player's rigidbody. 11 int floorMask; // A layer mask so that a ray can be cast just at gameobjects on the floor layer. 12 float camRayLength = 100f; // The length of the ray from the camera into the scene. 13 14 void Awake () 15 { 16 // Create a layer mask for the floor layer. 17 floorMask = LayerMask.GetMask ("Floor"); 18 19 // Set up references. 20 anim = GetComponent <Animator> (); 21 playerRigidbody = GetComponent <Rigidbody> (); 22 } 23 24 25 void FixedUpdate () 26 { 27 // Store the input axes. 28 float h = Input.GetAxisRaw ("Horizontal"); 29 float v = Input.GetAxisRaw ("Vertical"); 30 31 // Move the player around the scene. 32 Move (h, v); 33 34 // Turn the player to face the mouse cursor. 35 Turning (); 36 37 // Animate the player. 38 Animating (h, v); 39 } 40 41 void Move (float h, float v) 42 { 43 // Set the movement vector based on the axis input. 44 movement.Set (h, 0f, v); 45 46 // Normalise the movement vector and make it proportional to the speed per second. 47 movement = movement.normalized * speed * Time.deltaTime; 48 49 // Move the player to it's current position plus the movement. 50 playerRigidbody.MovePosition (transform.position + movement); 51 } 52 53 void Turning () 54 { 55 // Create a ray from the mouse cursor on screen in the direction of the camera. 56 Ray camRay = Camera.main.ScreenPointToRay (Input.mousePosition); 57 58 // Create a RaycastHit variable to store information about what was hit by the ray. 59 RaycastHit floorHit; 60 61 // Perform the raycast and if it hits something on the floor layer... 62 if(Physics.Raycast (camRay, out floorHit, camRayLength, floorMask)) 63 { 64 // Create a vector from the player to the point on the floor the raycast from the mouse hit. 65 Vector3 playerToMouse = floorHit.point - transform.position; 66 67 // Ensure the vector is entirely along the floor plane. 68 playerToMouse.y = 0f; 69 70 // Create a quaternion (rotation) based on looking down the vector from the player to the mouse. 71 Quaternion newRotation = Quaternion.LookRotation (playerToMouse); 72 73 // Set the player's rotation to this new rotation. 74 playerRigidbody.MoveRotation (newRotation); 75 } 76 } 77 78 void Animating (float h, float v) 79 { 80 // Create a boolean that is true if either of the input axes is non-zero. 81 bool walking = h != 0f || v != 0f; 82 83 // Tell the animator whether or not the player is walking. 84 anim.SetBool ("IsWalking", walking); 85 } 86}
using UnityEngine; public class PlayerShooting : MonoBehaviour { public int damagePerShot = 20; // The damage inflicted by each bullet. public float timeBetweenBullets = 0.15f; // The time between each shot. public float range = 100f; // The distance the gun can fire. float timer; // A timer to determine when to fire. Ray shootRay; // A ray from the gun end forwards. RaycastHit shootHit; // A raycast hit to get information about what was hit. int shootableMask; // A layer mask so the raycast only hits things on the shootable layer. ParticleSystem gunParticles; // Reference to the particle system. LineRenderer gunLine; // Reference to the line renderer. AudioSource gunAudio; // Reference to the audio source. Light gunLight; // Reference to the light component. float effectsDisplayTime = 0.2f; // The proportion of the timeBetweenBullets that the effects will display for. void Awake () { // Create a layer mask for the Shootable layer. shootableMask = LayerMask.GetMask ("Shootable"); // Set up the references. gunParticles = GetComponent<ParticleSystem> (); gunLine = GetComponent <LineRenderer> (); gunAudio = GetComponent<AudioSource> (); gunLight = GetComponent<Light> (); } void Update () { // Add the time since Update was last called to the timer. timer += Time.deltaTime; // If the Fire1 button is being press and it's time to fire... if(Input.GetButton ("Fire1") && timer >= timeBetweenBullets) { // ... shoot the gun. Shoot (); } // If the timer has exceeded the proportion of timeBetweenBullets that the effects should be displayed for... if(timer >= timeBetweenBullets * effectsDisplayTime) { // ... disable the effects. DisableEffects (); } } public void DisableEffects () { // Disable the line renderer and the light. gunLine.enabled = false; gunLight.enabled = false; } void Shoot () { // Reset the timer. timer = 0f; // Play the gun shot audioclip. gunAudio.Play (); // Enable the light. gunLight.enabled = true; // Stop the particles from playing if they were, then start the particles. gunParticles.Stop (); gunParticles.Play (); // Enable the line renderer and set it's first position to be the end of the gun. gunLine.enabled = true; gunLine.SetPosition (0, transform.position); // Set the shootRay so that it starts at the end of the gun and points forward from the barrel. shootRay.origin = transform.position; shootRay.direction = transform.forward; // Perform the raycast against gameobjects on the shootable layer and if it hits something... if(Physics.Raycast (shootRay, out shootHit, range, shootableMask)) { // Try and find an EnemyHealth script on the gameobject hit. EnemyHealth enemyHealth = shootHit.collider.GetComponent <EnemyHealth> (); // If the EnemyHealth component exist... if(enemyHealth != null) { // ... the enemy should take damage. enemyHealth.TakeDamage (damagePerShot, shootHit.point); } // Set the second position of the line renderer to the point the raycast hit. gunLine.SetPosition (1, shootHit.point); } // If the raycast didn't hit anything on the shootable layer... else { // ... set the second position of the line renderer to the fullest extent of the gun's range. gunLine.SetPosition (1, shootRay.origin + shootRay.direction * range); } } }

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

timer >= timeBetweenBulletsの部分が発射可能時間を制御しているようですね。
該当の変数の値を変えるか、条件そのものをはずせばよさそうです。

投稿2019/06/15 15:18

naby

総合スコア126

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

jkoman

2019/06/20 07:14

返信遅れてすいませんその条件は仕様でFire1はctrlと左クリックに対応しているようです。 連射というのはこの弾の間隔を守った連射ということです。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問