why do projectiles always move to the same spot?

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:

using UnityEngine;
using System.Collections;

public class WeaponHold : MonoBehaviour {

    public GameObject lefthand;
    public GameObject righthand;
    public GameObject parent;

    public GameObject pump;
    public GameObject handle;

    public GameObject projectile;
    public GameObject lookdirection;

    public float pumpspeed;
    private bool isPumping;
    private bool isReverse;
    private float progress;
   
    void Start () {
        isPumping = false;
        isReverse = false;
    }

    void Update () {
        if (Input.GetKeyDown (KeyCode.Mouse0) && !isPumping) {
            int i = 0;
            while (i < 10){
                Instantiate(projectile);
                projectile.transform.position = this.gameObject.transform.position;
                i++;
            }
            isPumping = true;
            progress = 1;
        }
        lefthand.transform.position = pump.gameObject.transform.position;
        righthand.transform.position = handle.gameObject.transform.position;
        lefthand.transform.LookAt (pump.transform.position);
        righthand.transform.LookAt (handle.transform.position);
        if (isPumping) {
            if (!isReverse) {
                pump.transform.Translate (0,(float) -0.1, 0);
                progress = progress - (float) 0.1;
                if (progress < 0) {
                    isReverse = true;
                    progress = 1;
                }
            }
            if (isReverse) {
                pump.transform.Translate (0,(float) 0.1, 0);
                progress = progress - (float) 0.1;
                if (progress < 0) {
                    isReverse = false;
                    isPumping = false;
                }
            }
           
        }
    }
}
using UnityEngine;
using System.Collections;

public class ProjectileScript : MonoBehaviour {

    public GameObject lookdirection;

    void Start () {
        this.gameObject.transform.LookAt (lookdirection.transform.position);
        this.gameObject.transform.Rotate (0, 90, -270);
    }
   

    void Update () {
        this.gameObject.transform.Translate (0,(float) 0.1, 0);
    }
}

Here’s a screenshot of the error:
I have selected the transform where it should be travelling to

I see one possible cause: this line-

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.

So, where should this be?

So I`ve fallen into this trap many times :

Here you are changing the position of a prefab and NOT the instantiated object.
Instead do this :

GameObject obj = Instantiate(projectile);
obj.transform.position = transform.position;
2 Likes

That looks better, and i’m sure that it would have caused a problem if u hadn’t told me (thanks :slight_smile: ), but it still isn’t working >.< it won’t point towards my point.

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 :

//in projectile script
Vector3 targetPosition;
float speed = 1;

//target position sent from WeaponHold  script
public void Initialize(Vector3 targetPos)
{
    targtePosition = targetPos;
    Vector3 direction = targetPos - transform.position;
    Quaternion rot = Quaternion.LookRotation(direction);
    transform.rotation = rot;
}

void Update()
{
    //this is straight from the manual
    float step = speed * Time.deltaTime;
     transform.position = Vector3.MoveTowards(transform.position, targetPosition, step);
}