myTransform = transform -- Does this not mean transform of gameobject that script is attached to?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Chapter1
{
public class ThrowGrenade : MonoBehaviour {

        public GameObject grenadePrefab;
        private Transform myTransform;
        public float propulsionForce;

        // Use this for initialization
        void Start ()
        {
            SetInitialReferences ();
        }

        // Update is called once per frame
        void Update ()
        {
            SpawnGrenade ();
        }


        void SpawnGrenade()
        {
            if (Input.GetButtonDown ("Fire1"))
            {
                GameObject grenade = (GameObject)Instantiate(grenadePrefab,
                    myTransform.TransformPoint(0,0,3f),
                    myTransform.rotation);
                grenade.GetComponent<Rigidbody> ().AddForce (myTransform.forward
                    * propulsionForce, ForceMode.Impulse);
                Destroy (grenade, 5);
            }

        }

        void SetInitialReferences()
        {
            myTransform = transform;
        }

   
   
   
}
}

Working through the GTGD tutorial. Everything was fine with the ThrowGrenade script ( a basic instantiate + add force script) until I messed around with the gameobject that I was instantiating the projectile from. Now I can’t get the script to work anymore.

Yes, I can just start over from the beginning, but I am trying to understand what is not working here. I understood that setting myTransform (a private variable) to = transform (in start function) would grab the transform of whatever gameobject I have the script attached to.

So if that is the case, if I give the instantiate argument a Vector 3 of myTransform.forward, or myTransform.TransformPoint (0,0,0.5f), why is my projectile appearing underneath my character collider?

Sorry, I hope this isn’t a stupid question. I’ve been fiddling for the past hour and a half but just can’t make sense of this.

Not knowing the situation in it’s entirety, i am assuming it’s because of the TransformPoint(0,0,0.5f) portion…

Try like, TransformPoint(0,1f,0.5f)

I updated with code.

I messed with various inputs the way you described to no avail. I moved the script from the weapon object to the FPS character object to no avail. I thought maybe the projectile was getting caught on the character collider, but moving it forward in the Z direction should have alleviated that.

I made an empty gameobject and placed it just forward of the end of the weapon. That is what I had working earlier – now I can’t get it to again.

Correct

transform.forward gives you only the “forward” direction of the transform, and doesn’t rely on the position of the object, so it won’t work. It doesn’t matter if the object is at (0, 0, 5) or at (15, 25, 35), as long as its rotation is the same, forward will return the same value.

This should work just fine. I suspect that the local Z axis (blue one) of myTransform is pointing down, and that’s why it appears underneath

You do not need private Transform myTransform; as every MonoBehaviour class has a transform member that gives you the Transform component of the GameObject that your current script is attached to.
Make sure that the game object has the rotation (0,0,0) that try your code written this way:

public class ThrowGrenade : MonoBehaviour
    {
        [SerializeField]
        GameObject grenadePrefab = null;
        [SerializeField]
        float spawnDistance = 1f;
        [SerializeField]
        float destroyTime = 1f;//in seconds
        [SerializeField]
        float propulsionForce = 1f;

        void Update()
        {
            if (Input.GetButtonDown("Fire1"))
            {
                if (grenadePrefab != null)
                {
                    GameObject grenadeObject = (GameObject)Instantiate(grenadePrefab);
                    grenadeObject.transform.position = transform.position + new Vector3(0, 0, spawnDistance);
                    grenadeObject.transform.rotation = Quaternion.identity;
                    var rigidBody = grenadeObject.GetComponent<Rigidbody>();
                    if (rigidBody != null)
                    {
                        rigidBody.AddForce(transform.forward * propulsionForce, ForceMode.Impulse);
                        Destroy(grenadeObject, destroyTime);
                    }
                }
            }
        }
    }
grenadeObject.transform.position = transform.position + new Vector3(0, 0, spawnDistance);

This doesn’t take player’s rotation into account and so the grenade will spawn incorrectly most of the time

It is crucial to set the transform correctly as the next code part also makes use of the forward vector

rigidBody.AddForce(transform.forward * propulsionForce, ForceMode.Impulse);

And so even if we manage to spawn it in front of the player using a workaround, it will be thrown in the wrong direction.