Search Unity

Instaited projectile Ignores the player's rotation

Discussion in 'Scripting' started by SirHootsALot, Apr 29, 2015.

  1. SirHootsALot

    SirHootsALot

    Joined:
    Apr 29, 2015
    Posts:
    4
    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.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. /// <summary>
    4. /// This script allows the player to fire the "Blaster" projectile
    5. /// </summary>
    6.  
    7.  
    8. public class FireBlaster : MonoBehaviour {
    9.    
    10.     // Variables
    11.     // The blaster projectile is attached in the inspector
    12.    
    13.     public GameObject Blaster;
    14.    
    15.     // Refences
    16.    
    17.     private Transform myTransform;
    18.    
    19.     private Transform cameraHeadTransform;
    20.    
    21.    
    22.     // Postion where the projectile will be sqawned
    23.    
    24.     private Vector3 lauchPostion = new Vector3();
    25.    
    26.     //Variables
    27.    
    28.    
    29.    
    30.    
    31.     // Use this for initialization
    32.     void Start ()
    33.     {
    34.  
    35.         myTransform = transform;
    36.         cameraHeadTransform = myTransform.FindChild("CameraHead");
    37.            
    38.     }
    39.        
    40.    
    41.     // Update is called once per frame
    42.     void Update ()
    43.         {
    44.    
    45.     if (Input.GetButton("Fire Weapon"))
    46.         {
    47.             //This is where the projectile spawns which is just infront of the cameraHead.
    48.             lauchPostion = cameraHeadTransform.TransformPoint(0 , 0 , 0.2f);
    49.            
    50.            
    51.             //Create blaster at launch postion and tilt its angle so it is horizontal
    52.             Instantiate(Blaster, lauchPostion,Quaternion.Euler(myTransform.eulerAngles.x + 90,
    53.                                                                myTransform.eulerAngles.y, 0));
    54.         }
    55.     }
    56. }
    57.  
    Oh yeah, and the reason the "blaster" (the projectile) is spawned and then rotated 90 is because its "point" is facing upwards by default.
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,411
    You can take cameraHeadTransform.forward to get its pointing direction
    (or depending what direction is forward for your object, try cameraHeadTransform.right, cameraHeadTransform.up etc)
    http://docs.unity3d.com/ScriptReference/Transform-forward.html

    So to have little offset could work as (not tested)
    lauchPostion = cameraHeadTransform.position+cameraHeadTransform.forward*0.2f;

    Other option would be to add empty launchPoint gameobject as a child to "cameraHeadTransform" at good position,
    and instantiate to there. (it would then follow the cameraHeadTransform rotation)
     
  3. SirHootsALot

    SirHootsALot

    Joined:
    Apr 29, 2015
    Posts:
    4
    Thanks for replying, those look they will work!
    1 Question my projectile modifies its transform according to time.delta in order to move.
    Why would I use the "rigibody" addforce when they both seem to do the same thing.
     
  4. steego

    steego

    Joined:
    Jul 15, 2010
    Posts:
    969
    If your object has a rigidbody and is controlled by physics (not kinematic) you should always move it with AddForce or MovePosition or similar, never by moving its transform.

    This is just due to how the physics system works.