【发布时间】:2015-09-19 17:08:30
【问题描述】:
我正在制作一款 2D 坦克射击游戏,但遇到了一些问题和疑问:
- 我遇到了一些碰撞问题。
GIF of a problem here. Go to tank collision problem.(由于声誉低,我不能发布超过 2 个链接,所以您必须手动访问图片,抱歉。)
我需要让我的坦克不要像上面显示的那样。我在空父对象上使用刚体,在坦克体上使用盒子碰撞器。
inspector 中的“Tank (root)”和inspector 中的“tankBody”(hull) 是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;
使用 System.Collections;
公开课射击: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); } }}
【问题讨论】:
-
只是一个旁注:使用平移来移动启用了刚体的对象并不是最好的主意。您应该像使用火箭一样在坦克上使用 addforce 或直接设置速度。您可能还想为刚体添加插值。
-
@PockeTiger 是的,我知道刚体上的变换不是最好的使用方法,但我不知道如何在没有它的情况下移动它们。我尝试了 Rigidbody2D.MovePosition 但它没有帮助而且我对力量的理解不够好。你能帮忙吗?
标签: c# unity3d 2d game-physics collision