Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

why do projectiles always move to the same spot?

Discussion in 'Scripting' started by Sporech, Oct 20, 2015.

  1. Sporech

    Sporech

    Joined:
    Sep 14, 2015
    Posts:
    26
    Hi, i have a simple first person shooter, and i have a gun which creates projectiles. the projectiles should be moving to an empty in front of it, but instead are moving to a single spot. Can anyone see anything wrong:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class WeaponHold : MonoBehaviour {
    5.  
    6.     public GameObject lefthand;
    7.     public GameObject righthand;
    8.     public GameObject parent;
    9.  
    10.     public GameObject pump;
    11.     public GameObject handle;
    12.  
    13.     public GameObject projectile;
    14.     public GameObject lookdirection;
    15.  
    16.     public float pumpspeed;
    17.     private bool isPumping;
    18.     private bool isReverse;
    19.     private float progress;
    20.    
    21.     void Start () {
    22.         isPumping = false;
    23.         isReverse = false;
    24.     }
    25.  
    26.     void Update () {
    27.         if (Input.GetKeyDown (KeyCode.Mouse0) && !isPumping) {
    28.             int i = 0;
    29.             while (i < 10){
    30.                 Instantiate(projectile);
    31.                 projectile.transform.position = this.gameObject.transform.position;
    32.                 i++;
    33.             }
    34.             isPumping = true;
    35.             progress = 1;
    36.         }
    37.         lefthand.transform.position = pump.gameObject.transform.position;
    38.         righthand.transform.position = handle.gameObject.transform.position;
    39.         lefthand.transform.LookAt (pump.transform.position);
    40.         righthand.transform.LookAt (handle.transform.position);
    41.         if (isPumping) {
    42.             if (!isReverse) {
    43.                 pump.transform.Translate (0,(float) -0.1, 0);
    44.                 progress = progress - (float) 0.1;
    45.                 if (progress < 0) {
    46.                     isReverse = true;
    47.                     progress = 1;
    48.                 }
    49.             }
    50.             if (isReverse) {
    51.                 pump.transform.Translate (0,(float) 0.1, 0);
    52.                 progress = progress - (float) 0.1;
    53.                 if (progress < 0) {
    54.                     isReverse = false;
    55.                     isPumping = false;
    56.                 }
    57.             }
    58.            
    59.         }
    60.     }
    61. }
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ProjectileScript : MonoBehaviour {
    5.  
    6.     public GameObject lookdirection;
    7.  
    8.     void Start () {
    9.         this.gameObject.transform.LookAt (lookdirection.transform.position);
    10.         this.gameObject.transform.Rotate (0, 90, -270);
    11.     }
    12.    
    13.  
    14.     void Update () {
    15.         this.gameObject.transform.Translate (0,(float) 0.1, 0);
    16.     }
    17. }
    18.  
    Here's a screenshot of the error:
    I have selected the transform where it should be travelling to
     

    Attached Files:

  2. Glurth

    Glurth

    Joined:
    Dec 29, 2014
    Posts:
    109
    I see one possible cause: this line-
    Code (CSharp):
    1. this.gameObject.transform.LookAt(lookdirection.transform.position);
    "LookAt" is going to use the current position of the object when computing the new rotation. But you don't set the position of the projectile until AFTER you have called this.
     
  3. Sporech

    Sporech

    Joined:
    Sep 14, 2015
    Posts:
    26
    So, where should this be?
     
  4. vintar

    vintar

    Joined:
    Sep 18, 2014
    Posts:
    90
    So I`ve fallen into this trap many times :

    1. Instantiate(projectile);
    2. projectile.transform.position = this.gameObject.transform.position;
    Here you are changing the position of a prefab and NOT the instantiated object.
    Instead do this :
    Code (csharp):
    1.  
    2. GameObject obj = Instantiate(projectile);
    3. obj.transform.position = transform.position;
     
    Sporech and McMayhem like this.
  5. Sporech

    Sporech

    Joined:
    Sep 14, 2015
    Posts:
    26
    That looks better, and i'm sure that it would have caused a problem if u hadn't told me (thanks :) ), but it still isn't working >.< it won't point towards my point.
     
  6. vintar

    vintar

    Joined:
    Sep 18, 2014
    Posts:
    90
    I also agree that you should not be calling LookAt in the start function, or rotating the obj for that matter. Its better to have your obj as a child of a blank object. Set your childed objects rotation in he prefab, and make sure the parents Z axis is point in the world Z direction. then when you spawn the object, you can do something like this :
    Code (csharp):
    1.  
    2. //in projectile script
    3. Vector3 targetPosition;
    4. float speed = 1;
    5.  
    6. //target position sent from WeaponHold  script
    7. public void Initialize(Vector3 targetPos)
    8. {
    9.     targtePosition = targetPos;
    10.     Vector3 direction = targetPos - transform.position;
    11.     Quaternion rot = Quaternion.LookRotation(direction);
    12.     transform.rotation = rot;
    13. }
    14.  
    15. void Update()
    16. {
    17.     //this is straight from the manual
    18.     float step = speed * Time.deltaTime;
    19.      transform.position = Vector3.MoveTowards(transform.position, targetPosition, step);
    20. }
    21.