我正在创造一个游戏,我创造了一个向右和向左移动的桥梁。 我希望我的球员在与桥梁相撞时继续前进。 我正在尝试设置位置球员的位置,但当我的球员在桥上他做小跳跃
我该怎么做?
我正在尝试这个。
public class MoveBridge : MonoBehaviour {
private bool isLeft = false;
public float speed = 5f;
public float delaySpeed;
private float moveTime;
public GameObject player;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
move();
}
private void move(){
moveTime += Time.deltaTime;
if (moveTime <= delaySpeed){
if (isLeft){
gameObject.transform.Translate(-Vector2.right * speed * Time.deltaTime);
}else{
gameObject.transform.Translate(Vector2.right * speed * Time.deltaTime);
}
}else{
isLeft = !isLeft;
moveTime = 0f;
}
}
void OnCollisionStay2D(Collision2D coll){
if(coll.gameObject.name.Equals("PlayerObject")){
player.transform.position = gameObject.transform.position;
}
}
}
答案 0 :(得分:0)
我解决了这个问题。
void OnCollisionStay2D(Collision2D coll){
if(coll.gameObject.name.Equals("PlayerObject")){
coll.gameObject.transform.parent = transform;
}
}