Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

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

Discussion in 'Scripting' started by BIGTIMEMASTER, Jul 6, 2017.

  1. BIGTIMEMASTER

    BIGTIMEMASTER

    Joined:
    Jun 1, 2017
    Posts:
    3,517
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. namespace Chapter1
    6. {
    7. public class ThrowGrenade : MonoBehaviour {
    8.  
    9.         public GameObject grenadePrefab;
    10.         private Transform myTransform;
    11.         public float propulsionForce;
    12.  
    13.         // Use this for initialization
    14.         void Start ()
    15.         {
    16.             SetInitialReferences ();
    17.         }
    18.  
    19.         // Update is called once per frame
    20.         void Update ()
    21.         {
    22.             SpawnGrenade ();
    23.         }
    24.  
    25.  
    26.         void SpawnGrenade()
    27.         {
    28.             if (Input.GetButtonDown ("Fire1"))
    29.             {
    30.                 GameObject grenade = (GameObject)Instantiate(grenadePrefab,
    31.                     myTransform.TransformPoint(0,0,3f),
    32.                     myTransform.rotation);
    33.                 grenade.GetComponent<Rigidbody> ().AddForce (myTransform.forward
    34.                     * propulsionForce, ForceMode.Impulse);
    35.                 Destroy (grenade, 5);
    36.             }
    37.  
    38.         }
    39.  
    40.         void SetInitialReferences()
    41.         {
    42.             myTransform = transform;
    43.         }
    44.  
    45.    
    46.    
    47.    
    48. }
    49. }
    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.
     
    Last edited: Jul 6, 2017
  2. Cynikal

    Cynikal

    Joined:
    Oct 29, 2012
    Posts:
    122
    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)
     
  3. BIGTIMEMASTER

    BIGTIMEMASTER

    Joined:
    Jun 1, 2017
    Posts:
    3,517
    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.
     
  4. radwan92

    radwan92

    Joined:
    Sep 6, 2013
    Posts:
    56
    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
     
  5. katoun

    katoun

    Joined:
    Dec 26, 2012
    Posts:
    87
    You do not need
    Code (CSharp):
    1. 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:
    Code (CSharp):
    1. public class ThrowGrenade : MonoBehaviour
    2.     {
    3.         [SerializeField]
    4.         GameObject grenadePrefab = null;
    5.         [SerializeField]
    6.         float spawnDistance = 1f;
    7.         [SerializeField]
    8.         float destroyTime = 1f;//in seconds
    9.         [SerializeField]
    10.         float propulsionForce = 1f;
    11.  
    12.         void Update()
    13.         {
    14.             if (Input.GetButtonDown("Fire1"))
    15.             {
    16.                 if (grenadePrefab != null)
    17.                 {
    18.                     GameObject grenadeObject = (GameObject)Instantiate(grenadePrefab);
    19.                     grenadeObject.transform.position = transform.position + new Vector3(0, 0, spawnDistance);
    20.                     grenadeObject.transform.rotation = Quaternion.identity;
    21.                     var rigidBody = grenadeObject.GetComponent<Rigidbody>();
    22.                     if (rigidBody != null)
    23.                     {
    24.                         rigidBody.AddForce(transform.forward * propulsionForce, ForceMode.Impulse);
    25.                         Destroy(grenadeObject, destroyTime);
    26.                     }
    27.                 }
    28.             }
    29.         }
    30.     }
     
  6. radwan92

    radwan92

    Joined:
    Sep 6, 2013
    Posts:
    56
    Code (CSharp):
    1. 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
    Code (CSharp):
    1. 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.
     
unityunity