Unity2D 碰撞和一些物理
Unity2D collisions and some physics
我正在制作一款 2D 坦克射击游戏,但遇到了一些问题:
- 我遇到了一些碰撞问题。
GIF of a problem here. Go to tank collision problem.(我不能 post 超过 2 link 秒,因为声誉低,所以你必须手动去图像,抱歉。)
我需要让我的坦克不像上面显示的那样。我在空父体上使用刚体,在坦克体上使用盒子碰撞器。
我在 inspector 中的 "Tank (root)" 和 inspector 中的 "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);
}
}
}
我的子弹像归零一样飞起来gravity/space。我需要他们不要那样悬停(我以前遇到过类似的问题,但我无法修复它..)。 1.st问题中第一个link有gif。
拍摄代码:
使用 UnityEngine;
使用 System.Collections;
public class 射击: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。我使用了 add force。我新的前进和后退看起来像这样:
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,angular 拖动 100。
第二个问题已通过设置拖动和 angular 拖动到更大的值得到解决。就是这样!
我正在制作一款 2D 坦克射击游戏,但遇到了一些问题:
- 我遇到了一些碰撞问题。
GIF of a problem here. Go to tank collision problem.(我不能 post 超过 2 link 秒,因为声誉低,所以你必须手动去图像,抱歉。)
我需要让我的坦克不像上面显示的那样。我在空父体上使用刚体,在坦克体上使用盒子碰撞器。
我在 inspector 中的 "Tank (root)" 和 inspector 中的 "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);
}
}
}
我的子弹像归零一样飞起来gravity/space。我需要他们不要那样悬停(我以前遇到过类似的问题,但我无法修复它..)。 1.st问题中第一个link有gif。 拍摄代码:
使用 UnityEngine;
使用 System.Collections;
public class 射击: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。我使用了 add force。我新的前进和后退看起来像这样:
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,angular 拖动 100。
第二个问题已通过设置拖动和 angular 拖动到更大的值得到解决。就是这样!