Instantiate position for shooting projectile is way off. Could I get some help?
Hi all. Working my way through the unity learn program and I'm at the point where I should make the game mechanics for my own game. I want to shoot a projectile from a launcher at the front of my little UFO placeholder object (which is the player). I've tried several different scripts to do this, but the closest I came to a working script is one that shoots the projectile in the right direction, but does NOT shoot it from the launcher on the UFO. (it instantiates a bit higher, to the left and too much in front of the barrel).
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ProjectileShoot : MonoBehaviour
{
public GameObject projectilePrefab;
public GameObject bulletEmitter;
public float shootSpeed = 500;
void FixedUpdate()
{
if (Input.GetKeyDown(KeyCode.Mouse0))
{
GameObject bullet;
bullet = Instantiate(projectilePrefab, bulletEmitter.transform.position, bulletEmitter.transform.rotation) as GameObject;
bullet.transform.Rotate(Vector3.left * 180);
Rigidbody temporaryRigidbody;
temporaryRigidbody = bullet.GetComponent<Rigidbody>();
temporaryRigidbody.AddForce(transform.forward * shootSpeed);
Destroy(bullet, 10.0f);
}
}
}
EDIT: heres the links. The images didn't load. https://ibb.co/52wQCX7 https://ibb.co/P9TW6nG
As you can see from the images I've attached the shootscript to the player, but I've also tried the cannon itself. I've got an emitter inside the cannon from which it should fire and this shouldn't cause a problem since the prefab has no box collider for as far as I know.
The line "bullet.transform.Rotate(Vector3.left *180);" only changes the height of which it launches somehow. It shoots from above the ground instead of below the ground because of this line. I've added a screenshot of the direction of the bullet as well. It also shows the canon from which it should fire.
Sorry for the large amount of info, trying to be thorough :). Can anybody see what is going wrong here?
Thanks a lot!
Gideon
Okay, the code was fine apperantly. Out of ideas I just remade the prefab of the bullet as I noticed it wasnt at the 0, 0, 0 spot when i reset its position. This fixed the issue. Sorry to bother you, I figured it out myself!
Your answer
Follow this Question
Related Questions
How do I instantiate an objects direction and speed seperately? 1 Answer
Trying to get Player to shoot in both directions depending on what side he is facing? 0 Answers
Instantiating a GameObject relative to another object's position and rotation 1 Answer
How does one continuously move an object forward without using velocity? 1 Answer
Instantiate GameObject with Velocity 1 Answer