The bullet shoots forward with an angle based on the mouse position. The farther away the mouse is the faster the bullet travels. Can’t figure out why. I am sure it is something simple. Thanks in advance!
EDIT: I realized that the same issue is occurring with the AI for my enemies. The farther away from me they are the faster they move. It is impossible to drop aggro!
using UnityEngine;
using System.Collections;
public class Bullet : MonoBehaviour {
// Variable Init:
private int damage;
private float speed;
private int range;
public int timetolive;
public int timesincespawn;
private float spawnX;
private float spawnY;
private float spawnZ;
private Vector3 target;
private Vector3 mousePositionInWorld;
// Use this for initialization
void Start () {
this.timetolive = 100;
this.timesincespawn = 0;
this.mousePositionInWorld = Camera.main.ScreenToWorldPoint(Input.mousePosition);
this.speed = 2000.0f;
}
// Update is called once per frame
void Update () {
if(timesincespawn >= timetolive) {
Destroy(gameObject);
}
}
void FixedUpdate () {
timesincespawn += 1;
this.transform.LookAt(mousePositionInWorld);
this.transform.Translate(Vector3.forward * speed * Time.deltaTime);
}
}