Если не работает RigidBody.velocity
Скрипт прикреплён к gun, bullet имеет компоненты RB2D, CapsuleCollider2D. Происходит создание объекта bullet, но объект не двигается, в остальном код работает. В чём может быть проблема ? Значения bulletSpeed = 20, а вот instantiatedBullet.velocity = (0.0, 0.0)
public Transform target;
public float offsetGun;
public GameObject gun;
private Vector3 targetPos;
private Vector3 thisPos;
private float angle;
private float nextFire = 0.0f;
public float fireRate = 1.0f;
public Rigidbody2D bullet;
public float bulletSpeed = 20.0f;
public void Start()
{
target = GameObject.FindGameObjectWithTag("Player").GetComponent<Transform>();
}
public void Update()
{
targetPos = target.position;
thisPos = gun.transform.position;
targetPos.x = targetPos.x - thisPos.x;
targetPos.y = targetPos.y - thisPos.y;
angle = Mathf.Atan2(targetPos.y, targetPos.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(new Vector3(0, 0, angle + offsetGun));
}
void FixedUpdate()
{
attack();
}
void attack()
{
float dist = Vector3.Distance(target.position, transform.position);
if (dist <= 50)
{
if (Time.time > nextFire)
{
//Debug.Log(bullet);
nextFire = Time.time + fireRate;
Rigidbody2D instantiatedBullet = Instantiate(bullet, gun.transform.position, gun.transform.rotation) as Rigidbody2D;
instantiatedBullet.velocity = transform.forward * bulletSpeed;
}
}
}
Ответы (1 шт):
Автор решения: Freeze Matic
→ Ссылка
Скрипт для игрока.
public class Player : MonoBehaviour
{
public GameObject Bullet;
public Transform SpawnPoint;
void Update()
{
if (Input.GetMouseButtonDown(0))
{
Instantiate(Bullet, SpawnPoint.position, SpawnPoint.rotation);
}
}
}
Скрипт для пули
public class Bullet : MonoBehaviour
{
public float Speed;
void Update()
{
transform.Translate(transform.forward * Speed * Time.deltaTime);
// Или любой другой способ перемещения
}
}