0

Unity で C# を使用してトップダウンの 2D ゲームを作成しています。壁を避けて追い詰められるのを避けるために、NPC からの短い 2D レイキャストを使用して壁検出システムを実装しようとしています。

2D レイ キャスターを NPC プレハブと必要なカメラにアタッチしましたが、今は NPC を選択できません (彼らは実際には NPC ではありません。制御したいものを選択すると、それが一時的にプレイヤーになります。マウスを持ち上げます)。選択は、OnMouseUp() および OnMouseDown() によって行われます。

これは、FixedUpdate (レイキャスト コードを含む) と、私の OnMouseUp() および OnMouseDown() 関数の両方を含む私のエージェント「My」クラスです。

my.physical.body は GameObject の RigidBody2D です。「the」クラスは、すべてのグローバル変数と関数を保持する単なるユーティリティ クラスです。

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

public class My : Agent {
    // self
    public int index;
    public TextMesh label;
    public CircleCollider2D edge;
    public Physical physical;
    public Mind mind;
    public Structure structure;
    public Wall wall;
    public Square square;
    public List<WayPoint> path;
    public List<My> willAccept;
    // others
    public My target;
    public List<My> targetters;
    public List<Relationship> relationships;
    public List<Relationship> attention;
    public float vicinity;
    public float personalSpace;
    public float speed;
    public float speedLimit;
    public float time;
    public Vector2 restPosition;
    public Vector2 origin;
    public Vector2 gridPos;
    // Type
    public enum Type { Neut, Tadpole };
    public Type type;
    public Tadpole tadpole;
    public Neut neut;

    void Awake () {
        the = FindObjectOfType<The> ();
        my = this;
        origin = transform.position;
        physical.goal = transform.position;
        time = the.thinkOffset;
        the.thinkOffset += 0.1f;

        switch (my.type) {
        case My.Type.Tadpole: tadpole.enabled = true; break;
        default : neut.enabled = true; break; }
    }

    void Start() {
        label.text = name;
    }

    void Update() {

        if (the.player.my == my) {
            my.physical.goal = Camera.main.ScreenToWorldPoint (Input.mousePosition);
        } else if (my.target) {
            my.physical.goal = my.target.transform.position;
        }

        time -= Time.deltaTime;
        if ( time < 0 ) {
            time = 1f;
            my.physical.action.UpdatePath ();
        }

        wall.end.transform.position = physical.goal;
        physical.body.AddForce (physical.pathGoal);
        physical.skin.SetColor (emotional.state);
    }

    void FixedUpdate() {
        RaycastHit2D hit = Physics2D.Raycast (transform.position, Vector2.down, 2);
        if (hit.collider != null) {
            print ("Raycast Hit at " + hit.point);
            float distance = Mathf.Abs (hit.point.y - transform.position.y);
            float difference = 2 - distance;
            float force = difference - my.physical.body.velocity.y;
            my.physical.body.AddForce (Vector3.up * force);
        }
    }

    public int power {
        get {
            return emotional.balance < 0 ? -emotional.balance : emotional.balance;
        }
    }

    void OnMouseUp() {
        the.player.my.physical.biases.Clear ();
        Vector2 mousePosition = Camera.main.ScreenToWorldPoint (Input.mousePosition);

        Bias bias = new Bias(mousePosition, 120);
        the.player.my.physical.biases.Clear ();
        the.player.my.physical.biases.Add (bias);
        the.player.my.physical.action.Untarget ();

        foreach (My target in the.agents.GetComponentsInChildren<My>()) {

            float distanceFromMouse = the.Distance (target.transform.position, mousePosition);

            if (distanceFromMouse < 1f) { 
                if (the.player.my) { 
                    the.player.my.physical.action.Target (target);
                    the.player.my.physical.biases.Clear ();
                }
            }
        }
        the.player.my = null;
        the.player.transform.parent = the.world.transform;
    }

    void OnMouseDown() {                                            

        float distanceFromPointer = the.Distance (this.transform.position, Camera.main.ScreenToWorldPoint(Input.mousePosition));

        if (distanceFromPointer < 1f) { 
            the.player.my = my;
            the.player.transform.parent = transform;
            the.cameraFocus = my.gameObject;
        }
    }
}

どんな助けでも大歓迎です。ありがとう、ジェームズ

コードのコンパイルに必要なその他のクラスは次のとおりです。

using UnityEngine;
using System.Collections;

public class Grammar : MonoBehaviour {

    public The the;
}

クラス...

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

public class The : MonoBehaviour {

    public GameObject world;
    public Grid grid;
    public PathFinder pathFinder;
    public Intersections intersections;
    public GameObject cameraFocus;
    public float thinkOffset = 0.05f;

    public Structure border;
    public Structure map;

    public Player player;

    public GameObject squareParent;
    public GameObject linkParent;
    public GameObject nodeParent;
    public GameObject agents;

    public List<WayPoint> wayPoints = new List<WayPoint>();

    public int numberOfLines;

    public Color red;
    public Color yellow;
    public Color blue;
    public Color orange;
    public Color green;
    public Color purple;
    public Color gray;
    public Color darkGray;
    public Color black;
    public Color white;

    public Vector2 HeadingFrom(float angle) { return new Vector2(Mathf.Cos(angle * Mathf.Deg2Rad), Mathf.Sin(angle * Mathf.Deg2Rad)); }
    public float AngleFrom(Vector2 heading) { return Mathf.Atan2 (heading.y, heading.x) * Mathf.Rad2Deg; }
    public Vector2 RelativeHeadingFrom(Vector2 heading, float rotation) { return (Vector2)(HeadingFrom(AngleFrom(heading) - rotation)); }
    public float Distance(Vector2 from, Vector2 to) { return (Heading(from,to)).magnitude; }
    public Vector2 Heading (Vector2 from, Vector2 to) { return to - from; }
    public Vector2 MidPoint (Vector2 from, Vector2 to) { return (from + to) / 2; }
    public Vector2 Product(Vector2 a, Vector2 b) { return new Vector2 (a.x * b.x, a.y * b.y); }
    public Vector2 Product(Vector2 a, float f) { return new Vector2 (a.x * f, a.y * f); }
}   

物理クラス

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

public class Physical : Need {

    public Skin skin;
    public Action action;
    public Rigidbody2D body;
    public int size;
    public List<My> personalSpace;
    public Vector2 pathGoal;

    public void Awake() {
        goal = my.transform.position;
    }

    public void Start() {
        the = my.the;
        a = my.a;

        biases = new List<Bias> ();
        action = GetComponent<Action>();
        body = GetComponent<Rigidbody2D> ();
    }
}

Agent class

sing System.Collections;

public class Player : Agent {

    void Start () {
        a = FindObjectOfType<A> ();
        the = FindObjectOfType<The> ();
    }

    void Update () {
    }


}
4

0 に答える 0