- Home /
Question by
wizardomri · Mar 12, 2017 at 05:49 PM ·
2d gameprojectile
My code wont work for a projectile following a target
the vector3 wont update and I cant figure out how to make it work. we have a projectile that spins around the map but the target's position won't change this is the code: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class CanonBallMoveTo : MonoBehaviour {
public Transform target;
public float speed;
Vector3 targetVec = Input.mousePosition;
// Use this for initialization
void Start () {
Vector3 targetVec = Input.mousePosition;
speed = 10f;
//target = GetComponent<Transform> ();
targetVec = target.position;
}
// Update is called once per frame
void Update () {
Debug.Log (targetVec);
StartCoroutine(moveTo(targetVec));
//StartCoroutine(moveTo(new Vector3(-103.5713f, 4.707786f, 0)));
}
IEnumerator moveTo(Vector3 pos) {
transform.position = Vector3.MoveTowards (transform.position, pos , speed * Time.deltaTime);
yield return null;
}
}
Comment