如何解决当我有3个相同的物体时,只有一个游戏物体会统一移动
这已经让我很烦了一段时间,但找不到解决方案。只有ground3
移动,而其他2保持原位。我正在团结一致地进行2D项目。
代码:
using System.Collections.Generic;
using UnityEngine;
public class GroundMovement : MonoBehaviour
{
public float globalspeed;
public GameObject Ground1;
public float Ground1Speed;
Rigidbody2D rb1;
public GameObject Ground2;
public float Ground2Speed;
Rigidbody2D rb2;
public GameObject Ground3;
public float Ground3Speed;
Rigidbody2D rb3;
// Start is called before the first frame update
void Start()
{
rb1 = Ground1.GetComponent<Rigidbody2D>();
rb2 = Ground1.GetComponent<Rigidbody2D>();
rb3 = Ground1.GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
rb1.velocity = new Vector2(globalspeed + Ground1Speed,0);
rb2.velocity = new Vector2(globalspeed + Ground2Speed,0);
rb3.velocity = new Vector2(globalspeed + Ground3Speed,0);
}
}
解决方法
问题出在Start()
方法中,当您分配刚体时,您将它们全部分配给了Ground1
GameObject中的刚体,该方法应如下所示:
void Start()
{
rb1 = Ground1.GetComponent<Rigidbody2D>();
rb2 = Ground2.GetComponent<Rigidbody2D>();
rb3 = Ground3.GetComponent<Rigidbody2D>();
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。