Нужна помощь с рикошетом в игре unity2d
Добавлено: 23 мар 2021, 11:54
Проблема такая при попадания в угл коробки при средней скорости пули она просто пролетает прямо как будто там нету бокса , при маленькой скорости все нормально.
пытался использовать bounce но все также, также пытался задавать полет пули разными способами тоже не помогло.
Скрипт пули и выстрела прикрепил ниже. Кто знает помогите пожалуйста
Код пули:
private Rigidbody2D rb;
Vector3 lastVelocity;
public float speed = 0f;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody2D>();
rb.velocity = transform.right * speed;
}
private void Update()
{
lastVelocity = rb.velocity;
}
private void OnCollisionEnter2D(Collision2D collision)
{
Debug.Log("collision");
var speed = lastVelocity.magnitude;
var direction = Vector3.Reflect(lastVelocity.normalized, collision.contacts[0].normal);
rb.velocity = direction * Mathf.Max(speed, 0f);
}
код выстрела:
[list=]
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class weapon : MonoBehaviour
{
public Transform shotposs;
public GameObject bullet;
public float bulletspeed = 0f;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
shoot();
}
}
void shoot()
{
Instantiate(bullet, shotposs.transform.position, shotposs.transform.rotation);
/*GameObject bulletIns = Instantiate(bullet, shotposs.position, shotposs.rotation); // Раньше данным способом задавал полет пули
bulletIns.GetComponent<Rigidbody2D>().AddForce(bulletIns.transform.right * bulletspeed);*/
}
}
пытался использовать bounce но все также, также пытался задавать полет пули разными способами тоже не помогло.
Скрипт пули и выстрела прикрепил ниже. Кто знает помогите пожалуйста
Код пули:
private Rigidbody2D rb;
Vector3 lastVelocity;
public float speed = 0f;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody2D>();
rb.velocity = transform.right * speed;
}
private void Update()
{
lastVelocity = rb.velocity;
}
private void OnCollisionEnter2D(Collision2D collision)
{
Debug.Log("collision");
var speed = lastVelocity.magnitude;
var direction = Vector3.Reflect(lastVelocity.normalized, collision.contacts[0].normal);
rb.velocity = direction * Mathf.Max(speed, 0f);
}
код выстрела:
[list=]
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class weapon : MonoBehaviour
{
public Transform shotposs;
public GameObject bullet;
public float bulletspeed = 0f;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
shoot();
}
}
void shoot()
{
Instantiate(bullet, shotposs.transform.position, shotposs.transform.rotation);
/*GameObject bulletIns = Instantiate(bullet, shotposs.position, shotposs.rotation); // Раньше данным способом задавал полет пули
bulletIns.GetComponent<Rigidbody2D>().AddForce(bulletIns.transform.right * bulletspeed);*/
}
}