かぴばらさんの覚書ブログ (nonkapibara 自分メモ)

かぴばらさんの覚書ブログ (nonkapibara 自分メモ)

Unity勉強中です。 AR、VR、エンターテイメント全般、ワクワクする事が大好き♪♪ O(≧∇≦)O イエイ!!

【Unity】Version 1 ショットガンから球を発射

Version 1. ショットガンから球を発射。

Assetsストアから「Survival Shooter Tutorial」素材を使ってお勉強中!

環境メモ

⭐️Mac OS Mojave バージョン10.14

⭐️Unity 2018.2.15f1

実際に動かした動画はこちら↓↓

https://twitter.com/nonnonkapibara/status/1065776198347710464 

f:id:nonkapibara:20181123094244p:plain

 

1.Assetsストアから「Survival Shooter Tutorial」ダウンロードする

https://assetstore.unity.com/packages/essentials/tutorial-projects/survival-shooter-tutorial-40756

f:id:nonkapibara:20181123094537p:plain

 

 

2.Floor」をDragDropで配置する

f:id:nonkapibara:20181123094557p:plain

 

3.Floor」を中心に配置する

f:id:nonkapibara:20181123094629p:plain

 

4.Player」を「Main Camera」の直下に配置する

Player -> PlayerGunに名前を変更する。

f:id:nonkapibara:20181123094653p:plain

 

 

Survival Shooter TutorialPlayerのキャラクターがなかったら

https://github.com/madvas/survival-shooter-multiplayer-unity3d-files/tree/master/Assets/Models/Characters

↑Player.fbx

ダウンローする

f:id:nonkapibara:20181123094725p:plain

 

5.作業するために、一旦、キャラクター(Player)のみを非表示にする

f:id:nonkapibara:20181123094754p:plain

6.球を作る Sphereを選択して、名前を「Bullet」にする

f:id:nonkapibara:20181123094811p:plain

7.球をPrefabにする。Purefabにしたら、元のBulletは削除する

f:id:nonkapibara:20181123094832p:plain

8.ShooterScript」を作成し、PlayerGunに、配置する

f:id:nonkapibara:20181123094857p:plain

9.bulletを選択しRigidbodyを追加する。

Use GravityのチェックをOFFにする。重力の影響を受けない。

f:id:nonkapibara:20181123094919p:plain

10.bulletにスクリプトを追加する

f:id:nonkapibara:20181123095148p:plain

 

f:id:nonkapibara:20181123100515p:plain

 

f:id:nonkapibara:20181123100538p:plain

 

11.BulletにDestroyScriptを追加する

f:id:nonkapibara:20181123130056p:plain

■ShooterScript.cs

>|cs|

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ShooterScript : MonoBehaviour {
    // 銃弾
    [SerializeField] GameObject bulletPrefab = null;
    // 銃口
    [SerializeField] Transform ganPoint = null;

    // Update is called once per frame
    void Update () 
    {
        if (Input.GetKeyDown("space"))
        {
            Createbullet();
        }
    }

    private void Createbullet()
    {
        // 銃弾を生成する
        if (bulletPrefab != null && ganPoint != null) {
            Instantiate(bulletPrefab, ganPoint.position, ganPoint.rotation);
        }
    }
}

||<

BulletScript.cs

>|cs|

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[RequireComponent(typeof(Rigidbody))]
public class BulletScript : MonoBehaviour {
    //RequireComponentは、アタッチし忘れを防ぐ。ここではRigidbodyは必須。
    // 球のスピード
    [SerializeField] float spped = 10f;

    void Start () {
        // 速度
        var velocity = spped * transform.forward;
        var rigid = GetComponent<Rigidbody>();
        // AddForceはrigidbodyへの継続的な力を追加
        // VelocityChangはrigidbodyに瞬時に速度変化を追加
        rigid.AddForce(velocity, ForceMode.VelocityChange);
    }
}
 

||< 

 

■DestroyScript.cs

>|cs|

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DestroyScript : MonoBehaviour {

    [SerializeField] float life = 1f;
    void Start () {
        // 球のゲームオブジェクトを破棄する
        Destroy(gameObject, life);
    }
}

 ||<