- Home /
Instantiate bullet toward object
Hi, I'm currently working on a dog fighting game and I would like that when the bullet hits the shield, it ricochet and flies towards the player with increased. It is a 2 person game so whenever the bullet of one player hits the shield of another, it deletes itself and creates the other player's bullet.
I currently have it where the bullet collides and makes a 180, however I am unable to get it to go towards the player. Does anyone know how to solve this?
//Turns around normally
Instantiate(bulletPrefaby, transform.position, Quaternion.Euler(0,0,180));
bulletPrefaby.GetComponent<Bullet>().damage = damage + increaseDamage;
Destroy(gameObject);
//Instantiates with incorrect angle
float playerPosition = playery.transform.position.z - transform.position.z;
Instantiate(bulletPrefaby, transform.position, Quaternion.Euler(0,0, playerPosition));
bulletPrefaby.GetComponent<Bullet>().damage = damage + increaseDamage;
Destroy(gameObject);
Thank you!
Answer by Glurth · Nov 02, 2017 at 11:40 PM
Quaternion.Euler takes ANGLES as the parameters, not a positional offset.
Try using this one instead: https://docs.unity3d.com/ScriptReference/Quaternion.LookRotation.html
The below is untested & uncompiled, but should give you a good sample:
Vector3 offset = playery.transform.position - transform.position;
Quaternion facingPlayerX = Quaternion.LookRotatation(offset,Vector3.up);
Instantiate(bulletPrefaby, transform.position, facingPlayerX);
This does make a new bullet, however they don't seem to work like they do with Quaternion.Euler. For some reason they don't move and create a smaller version of the prefab although it is the same exact prefab as the normal bullets. They don't create a collider either and I get an error saying the collider "did not create any as they all failed verification. This could be because they were deemed too small or the vertices were too close". Any ideas?
too close: this could be a problem- passing a vector that is == to Vector3.zero, into the LookRotation could give you problems. But that would mean playery.transform.position == transform.position
: the bullet and player are in the same place- automatic hit?
Other than that, the ONLY thing that should have changed is the rotation you are passing to the Instantiate function.
Answer by Xysch · Nov 10, 2017 at 08:52 AM
I did not end up finding a solution but I instead chose to change the rotation after the bullet was created.
public bool reflectedBullet = false;
void Start()
{
//If bullet clone is reflected
if(reflectedBullet == true)
{
Vector3 FacePlayer = player1.transform.position - transform.position;
float angle = Mathf.Atan2(FacePlayer.y, FacePlayer.x) * Mathf.Rad2Deg;
Quaternion q = Quaternion.AngleAxis(angle, Vector3.forward);
transform.rotation = Quaternion.Slerp(transform.rotation, q, Time.deltaTime * 1000000);
}
}
private void OnTriggerExit2D(Collider2D collision)
{
if (collision.gameObject.tag == "SheildPlayer2" && player1Bullets == true && playerMissile == false)
{
//Create transform in order to only edit the clone and not prefrab
Transform reflectedBullet = Instantiate(player2BulletPrefab, transform.position, transform.rotation * Quaternion.Euler(0, 0, 180));
reflectedBullet.GetComponent<Bullet>().damage = damage + increaseDamage;
reflectedBullet.GetComponent<Bullet>().reflectedBullet = true;
Destroy(gameObject);
}
}