Unity 3D Рух з обертанням + постріл

2075 / Unity / 3D / Гравець / Рух з обертанням

 

TankShoot

 

[Header("Tank")]
public float speed = 10.0f;
public float rotationSpeed = 100.0f;

[Header("Bullet")]
public GameObject bullet;
public Transform bulletPos;
public float bulletSpeed = 1000f;
public float bulletTime = 10f;

void Update()
{
  float translation = Input.GetAxis("Vertical") * speed * Time.deltaTime;
  float rotation = Input.GetAxis("Horizontal") * rotationSpeed * Time.deltaTime;
  transform.Translate(0f, 0f, translation); // -translation
  transform.Rotate(0f, rotation, 0f);

  if (Input.GetButtonDown("Fire1"))
  {
    GameObject bull = Instantiate(bullet, bulletPos.position, transform.rotation);
    bull.GetComponent<Rigidbody>().AddForce(-transform.forward * bulletSpeed);
    Destroy(bull, bulletTime);
  }
}


З Rigidbody та на малій швидкості не проходить крізь стіни



 
Не проходити крізь стіни
rb.AddForce(transform.forward * (-translation) * 100 * Time.deltaTime);


Миттєва зупинка
if (translation == 0)
{
  rb.velocity = Vector3.zero;
}

 
Обмеження по швидкості
if (rb.velocity.magnitude > 10)
{
  rb.velocity = rb.velocity.normalized * 10f;
}