- Home /
Projectile Speeds up when target is farther away
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);
}
}
Answer by Loius · Aug 02, 2012 at 04:03 AM
This sounds like the issue you get if you forget the .normalized in (targetPosition - sourcePosition).normalized. It looks like perhaps LookAt is dirtying the transform's vectors, perhaps setting them to non-normalized values somehow (I wouldn't expect this behaviour, this just seems to be what's happening). Try doing something like (transform.forward = transform.forward.normalized) between LookAt and Translate.
If that doesn't do anything, you need to check out your movement code and see if you can find a "target vector" calculation that calculates a non-normalized vector.
EDIT: I ended up finding that the fact that the player (camera) position had a -1000 Z it was throwing the speed off. I could not find a way to correct this out effectively so I ended up going a different route. Thank you though!