GameObject的领导者/C问题 [英] GameObject's Following a Leader / Centipede Issue
问题描述
我很难将一些GameObject链接在一起以跟随领导者".
I am having difficulty chaining together some GameObjects to "Follow the leader".
这是我要完成的工作:
有头"对象的地方,它随它一起拉body
对象.像蛇或cent.
Where there is a "head" object and it pulls the body
objects with it. Like a snake or centipede.
但是,这是正在发生的事情
However, this is what is happening:
它们都跟着头部走,但是它们似乎整体上都在运动.
They all kinda follow the head and but they seem to move as a whole.
有人可以看到我在做什么错吗?
Can anyone see what I am doing wrong?
这是其他类继承的my对象. 我开始尝试只让CentipedeObject移动所有独立GameObjects的想法.但这是行不通的.
Here is my Centipede Object that the others classes inherit from. I started to tinker with the idea of just making the CentipedeObject move all of the independent GameObjects. But that didn't work.
CentipedeObject.cs
public abstract class CentipedeObject : MonoBehaviour {
[SerializeField]
private float moveSpeed = 1.5f;
public float MoveSpeed {
get {
return moveSpeed;
}
}
}
这是进行移动的 CentipedeHead .
public class CentipedeHead : CentipedeObject {
private Rigidbody2D _body;
private Vector2 moveDirection = Vector2.zero;
private void Awake() {
_body = GetComponent<Rigidbody2D>();
_body.gravityScale = 0;
_body.constraints = RigidbodyConstraints2D.FreezeRotation;
}
private void Start() {
InvokeRepeating("ChangeDirection", 0, Random.Range(1.25f, 3.0f));
}
private void FixedUpdate() {
_body.velocity = moveDirection.normalized * MoveSpeed;
}
private void ChangeDirection() {
moveDirection = Vector2.zero;
switch (Random.Range(0, 4)) {
case 0:
moveDirection += Vector2.up;
break;
case 1:
moveDirection += Vector2.right;
break;
case 2:
moveDirection += Vector2.down;
break;
case 3:
moveDirection += Vector2.left;
break;
}
}
}
C身体部分应该紧跟头部或其他身体部分.
And the CentipedeBody part that should just follow the head or another body part.
public class CentipedeBody : CentipedeObject {
public GameObject objectToFollow;
private Vector3 followSize;
void Start() {
//Get the size of the object we are following
followSize = objectToFollow.GetComponent<SpriteRenderer>().bounds.size;
//Set initial distance
transform.position = objectToFollow.transform.position + followSize;
}
private void FixedUpdate() {
Vector3 followFromPosition = objectToFollow.transform.position + followSize;
transform.position = Vector3.MoveTowards(transform.position, followFromPosition, MoveSpeed * Time.deltaTime);
}
}
推荐答案
我认为更新像素的逻辑是错误的.当头部向左移动一个像素时.您正在使所有其他身体部位向左移动1个像素.您所需要做的是(从图中)2确切地位于1所在的位置. 3取代了2,以此类推.
I think the logic of updating the pixels is wrong. When the head moves one pixel left. You are making all other body parts move left by 1 pixel. Where as what you need to do is that (from your diagram) 2 takes the exact place where 1 was. 3 takes the place where 2 was and so on.
这篇关于GameObject的领导者/C问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!