我正在制作 2D 坦克射击游戏,但遇到了一些问题和疑问:
- 我遇到了一些碰撞问题。
GIF of a problem here. Go to tank collision problem. (由于声誉较低,我无法发布超过 2 个链接,因此您必须手动查看图像,抱歉。)
我需要让我的坦克不会像上面所示的那样。我在空父级上使用刚体,在坦克体上使用盒对撞机。
检查器中的“Tank(根)”和检查器中的“tankBody”(船体)是 here.
坦克移动代码:
using UnityEngine;
using System.Collections;
public class Movement : MonoBehaviour {
public float thrust;
public float rotatingspeed;
public Rigidbody rb;
void Start () {
rb = GetComponent<Rigidbody>();
}
void Update () {
if (Input.GetKey (KeyCode.W)) {
transform.Translate (Vector2.right * thrust);
}
if (Input.GetKey (KeyCode.S)) {
transform.Translate (Vector2.right * -thrust);
}
if(Input.GetKey(KeyCode.A)) {
transform.Rotate(Vector3.forward, rotatingspeed);
}
if(Input.GetKey(KeyCode.D)) {
transform.Rotate(Vector3.forward, -rotatingspeed);
}
}
}
我的子弹就像在零重力/太空中一样飞行。我需要它们不要像那样悬停(我之前遇到过类似的问题,但无法修复它..)。第一个问题的第一个链接中有 gif。 拍摄代码:
使用UnityEngine;
使用系统集合;
公共(public)类射击:MonoBehaviour {
public Rigidbody2D projectile; public float speed = 20; public Transform barrelend; void Update () { if (Input.GetButtonDown("Fire1")) { Rigidbody2D rocketInstance; rocketInstance = Instantiate(projectile, barrelend.position, barrelend.rotation) as Rigidbody2D; rocketInstance.AddForce(barrelend.right * speed); } }
}
最佳答案
我设法解决了我的这两个问题。 为了解决问题1。我使用了addforce。我新的前进和后退看起来像这样:
if (Input.GetKey (MoveForward)) {
//transform.Translate (Vector2.right * thrust); OLD !!
rb2D.AddForce(transform.right * thrust * Time.deltaTime);
}
if (Input.GetKey (MoveBackward)) {
//transform.Translate (Vector2.right * -thrust); OLD !!
rb2D.AddForce(transform.right * -thrust * Time.deltaTime);
我必须将质量调整为更小(从 2000 到 1),将推力调整为更大(从 0.2 到 50000),并将阻力设置为 50,角度阻力设置为 100。
第二个问题通过将阻力和角度阻力设置为更大的值得到解决。就是这样!
关于c# - Unity2D 碰撞和一些物理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31180020/