Hey, I’m new to unity and i’ve been having some issues trying to instantiate a projectile where the player is currently looking that is rotated depending on the player’s “cameraHead” rotation (which is an gameobject inside the player that takes on the role of a camera.) I was following a tutorail and no one else seemed to have this issue and I don’t know enough of C# to be able to code effectively in it.
I heard fourms were a great place to find advice and solutions to this please take a look and tell me what i’ve done wrong or what an easier way of going about it is.
using UnityEngine;
using System.Collections;
/// <summary>
/// This script allows the player to fire the "Blaster" projectile
/// </summary>
public class FireBlaster : MonoBehaviour {
// Variables
// The blaster projectile is attached in the inspector
public GameObject Blaster;
// Refences
private Transform myTransform;
private Transform cameraHeadTransform;
// Postion where the projectile will be sqawned
private Vector3 lauchPostion = new Vector3();
//Variables
// Use this for initialization
void Start ()
{
myTransform = transform;
cameraHeadTransform = myTransform.FindChild("CameraHead");
}
// Update is called once per frame
void Update ()
{
if (Input.GetButton("Fire Weapon"))
{
//This is where the projectile spawns which is just infront of the cameraHead.
lauchPostion = cameraHeadTransform.TransformPoint(0 , 0 , 0.2f);
//Create blaster at launch postion and tilt its angle so it is horizontal
Instantiate(Blaster, lauchPostion,Quaternion.Euler(myTransform.eulerAngles.x + 90,
myTransform.eulerAngles.y, 0));
}
}
}
Oh yeah, and the reason the “blaster” (the projectile) is spawned and then rotated 90 is because its “point” is facing upwards by default.