Loading [MathJax]/jax/output/HTML-CSS/config.js

【Unity】一定範囲に入ると追ってくるスクリプト

目的

あるオブジェクトを中心にした一定範囲にPlayerが入ったときに、そのオブジェクトが追いかけてくるというスクリプトを作る。

環境

Unity 2020 1.1f1
Windows10

完成系

Playerが一定範囲内に入ると追いかけてきます。

Step1 : プレイヤーの配置

Standard AssetsのEthanを使用します。配置の仕方はこちらで紹介しています。

配置したEthan。

作成したEthanのTagをPlayerにしておきます。

rigidbodyとcapusule colliderもつけておきます。

Step:2 追いかけてくるオブジェクトの配置

今回は敵の想定でCubeを使ってスクリプトを作成したいと思います。

まずはGameObject > 3D Object CubeからCubeを配置します。

次に、CubeにCharacter ControllerとSphere Colliderを設定します。

Character ControllerはPlayerが一定範囲に入ったときにCubeを動かすために使用します。設定は特に触らずデフォルトのままにしておきます。

Shpere ColliderはPlayerが一定範囲に入ったかどうかを検出するために使用します。ここではIs Triggerにチェックを入れておきます。

Step : 3 スクリプトの作成

次のようなスクリプトを作成し、Cubeへアタッチします。

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class Approach : MonoBehaviour
{
 
    CharacterController Controller;
    Transform Target;
    GameObject Player;
 
    [SerializeField]
    float MoveSpeed = 2.0f;
    int DetecDist = 8;
    bool InArea = false;
 
 
    // Use this for initialization
    void Start()
    {
 
        // プレイヤータグの取得
        Player = GameObject.FindWithTag("Player");
        Target = Player.transform;
 
        Controller = GetComponent<CharacterController>();
 
    }
 
    // Update is called once per frame
    void Update()
    {
 
        if (InArea)
        {
            // &#12503;&#12524;&#12452;&#12516;&#12540;&#12398;&#12411;&#12358;&#12434;&#21521;&#12363;&#12379;&#12427;
            this.transform.LookAt(Target.transform);
 
            // &#12461;&#12517;&#12540;&#12502;&#12392;&#12503;&#12524;&#12452;&#12516;&#12540;&#38291;&#12398;&#36317;&#38626;&#12434;&#35336;&#31639;
            Vector3 direction = Target.position - this.transform.position;
            direction = direction.normalized;
 
            // &#12503;&#12524;&#12452;&#12516;&#12540;&#26041;&#21521;&#12398;&#36895;&#24230;&#12434;&#20316;&#25104;
            Vector3 velocity = direction * MoveSpeed;
             
            // &#12503;&#12524;&#12452;&#12516;&#12540;&#12364;&#12472;&#12515;&#12531;&#12503;&#12375;&#12383;&#12392;&#12365;&#12395;&#12461;&#12517;&#12540;&#12502;&#12364;&#28014;&#12363;&#12394;&#12356;&#12424;&#12358;&#12395;y&#36895;&#24230;&#12434;0&#12395;&#22266;&#23450;&#12375;&#12390;&#12362;&#12367;(&#31354;&#20013;&#12418;&#36861;&#24467;&#12373;&#12379;&#12383;&#12356;&#22580;&#21512;&#12399;&#19981;&#35201;)
            velocity.y = 0.0f;
 
            // &#12461;&#12517;&#12540;&#12502;&#12434;&#21205;&#12363;&#12377;
            Controller.Move(velocity * Time.deltaTime);
        }
 
        //&#12503;&#12524;&#12452;&#12516;&#12540;&#12392;&#12461;&#12517;&#12540;&#12502;&#38291;&#12398;&#36317;&#38626;&#12434;&#35336;&#31639;
        Vector3 Apos = this.transform.position;
        Vector3 Bpos = Target.transform.position;
        float distance = Vector3.Distance(Apos, Bpos);
 
        // &#36317;&#38626;&#12364;DetecDist&#12398;&#35373;&#23450;&#20516;&#26410;&#28288;&#12398;&#22580;&#21512;&#12399;&#26908;&#30693;&#12501;&#12521;&#12464;&#12434;false&#12395;&#12377;&#12427;&#12290;
        if (distance > DetecDist)
        {
            InArea = false;
        }
    }
 
    // &#12503;&#12524;&#12452;&#12516;&#12540;&#12364;&#26908;&#30693;&#12456;&#12522;&#12450;&#12395;&#12399;&#12356;&#12383;&#12425;&#26908;&#30693;&#12501;&#12521;&#12464;&#12434;true&#12395;&#12377;&#12427;&#12290;
    private void OnTriggerEnter(Collider other)
    {
        InArea = true;
    }
 
}

Step : 4 動作確認

あとはゲームスタートするだけです。これで最初のgifのような動きをするはずです。

以上

Unityを一から学ぶのにおすすめの本はコチラ

おすすめ

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です