NPC上的RaycastHit2D干扰Unity中的OnMouseUp()和OnMouseDown()

时间:2016-12-08 16:18:48

标签: c# unity3d 2d raycasting onmouseup

我正在使用C#在Unity中创建一个自上而下的2D游戏。我正在尝试使用来自NPC的短2D射线投影来实现墙壁检测系统,以避免墙壁并避免走投无路。

我已将2D射线脚轮连接到我的NPC预制件和所需的相机,但现在我无法选择我的NPC(好吧他们不是真正的NPC,你选择了你想要控制的NPC并且直到你举起鼠标才会暂时成为玩家)。选择是通过OnMouseUp()和OnMouseDown()

完成的

这是我的经纪人"我的"包含FixedUpdate(包含光线投射代码)和OnMouseUp()以及OnMouseDown()函数的类。

my.physical.body是GameObject的RigidBody2D """ class只是一个包含我所有全局变量和函数的实用程序类。

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 () {
    }


}

0 个答案:

没有答案