我正在使用 C# 在 Unity 中创建一个自上而下的 2D 游戏。我正在尝试使用来自我的 NPC 的短 2D 射线投射来实现墙壁检测系统,以避免墙壁并避免走投无路。
我已将 2D 射线投射器连接到我的 NPC 预制件和所需的相机,但现在我无法选择我的 NPC(好吧,他们不是真正的 NPC,你选择你想要控制的那个,它会暂时成为玩家,直到你抬起鼠标)。选择是通过 OnMouseUp() 和 OnMouseDown() 完成的
这是我的代理“My”类,它包含 FixedUpdate(包含光线投射代码)和我的 OnMouseUp() 和 OnMouseDown() 函数。
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 () {
}
}