Prefab Direction - shooting
Hey! For some days I had a problem regarding one prefab direction, basically I'm building a 2D shooter game where the gun follows the mouse cursor.
My current problem is, when I'm facing right the bulletPrefab goes in the direction I want, but when I flip the character the bulletPrefab goes in the opposite direction... I tried to change it manually and it seems to be a problem with the rotation but I don't know how to solve it. Can you guys help? What I have:
void Shoot() {
// Obter posição do rato no "mundo"
Vector2 mousePostion = new Vector2 (Camera.main.ScreenToWorldPoint(Input.mousePosition).x,Camera.main.ScreenToWorldPoint(Input.mousePosition).y);
Vector2 firePointPosition = new Vector2 (firePoint.position.x, firePoint.position.y);
RaycastHit2D hit = Physics2D.Raycast (firePointPosition, mousePostion-firePointPosition, 100, whatToHit);
if (Time.time >= timeToSpawnEffect)
{
Effect();
timeToSpawnEffect = Time.time + 1/effectSpawnRate;
}
Debug.DrawLine (firePointPosition, (mousePostion-firePointPosition)*100);
if (hit.collider != null) {
Debug.DrawLine (firePointPosition, hit.point, Color.red);
Debug.Log ("Hit:" + hit.collider.name);
}
}
void Effect()
{
Transform playerTransform = GameObject.FindGameObjectWithTag("Player").transform;
Vector2 playerScale = playerTransform.localScale;
if (playerScale.x > 0)
{
Instantiate(BulletTrailPrefab, firePoint.position, firePoint.rotation);
}
else
{
Instantiate(BulletTrailPrefab, firePoint.position, Quaternion.Euler(firePoint.rotation.x, firePoint.rotation.y, -firePoint.rotation.z));
}
}
// firePoint is a child object
Try and not use the firePoint.positions and rotations, just use transform.position and the same for the rotations
I don't think you need to use Quaternion.Euler
seeing as it's going to spawn the bullet at the fire point rotation anyways. Can you attach a picture of the problem?
Yes. This is what happens when I don't use Quaternion.Euler and only use the Instantiate(BulletTrailPrefab, firePoint.position, firePoint.rotation); https://i.gyazo.com/0251d41d89c556da55270ee04f901b7e.gif
Answer by sxnorthrop · Nov 02, 2016 at 12:13 AM
In the else statement for playerScale.x
try Quaternion.LookRotation(-firePoint.forward)
for the rotation.
Hello again and thank you for answering! I tried it and the bullet always go forward now...
You need to make sure that the firePoint is actually rotating with the gun, meaning the z axis of the firepoint should always be looking in the direction in front of the gun itself.
Your answer
Follow this Question
Related Questions
I want to spawn objects probabilistically. 0 Answers
UI inputfield not shows mobile keyboard on click 0 Answers
How do i fix my problem with minimizing/maximizing and just opening unity editor windows? 0 Answers
Slider.value doesn't show the value in real-time but after the execution of the script 1 Answer