- Home /
Enemy shooting at spawn point not player
The enemy that spawn into my game from the Prefab folder does not shoot at the player, but where the player spawns.
//enemy shooting script
var projectile: Rigidbody;
var speed = 25;
var player : Transform;
function Start() {
var rendum = Random.Range(1F,3F);
InvokeRepeating("Shuut", 2, rendum);
}
function Update() {
transform.LookAt(player);
}
function Shuut () {
clone = Instantiate(projectile, transform.position, transform.rotation);
clone.velocity = transform.TransformDirection( Vector3 (0, 0, speed));
Destroy (clone.gameObject, 50);
}
//enemy movement script
var startMarker: Transform;
var endMarker: Transform;
// Movement speed in units/sec.
var speed = 10.0;
// Time when the movement started.
private var startTime: float;
// Total distance between the markers.
private var journeyLength: float;
var target : Transform;
var smooth = 5.0;
function Start() {
// Keep a note of the time the movement started.
startTime = Time.time;
// Calculate the journey length.
journeyLength = Vector3.Distance(startMarker.position, endMarker.position);
}
// Follows the target position like with a spring
function Update () {
// Distance moved = time * speed.
var distCovered = (Time.time - startTime) * speed;
// Fraction of journey completed = current distance divided by total distance.
var fracJourney = distCovered / journeyLength;
transform.position=Vector3.Lerp(startMarker.position, endMarker.position, Mathf.PingPong(Time.time*speed, 1.0f));
}
Answer by Socrates · Mar 15, 2015 at 08:20 PM
Looking at your shooting script, you are turning your enemy toward the "player" target. Have you confirmed that you put the actual player transform into that public variable and not the player's spawn point?
For further testing, use the Project View to watch the enemy after you have moved the player away from the starting position. Is the enemy actually rotating to face the player position as that enemy moves between the two ends of its movement path?
Yes I have put the player in the variable but the player is a gameobject not transform, could that be affecting the enemy. The enemy is facing the player spawn point as it moves just not at the player.
When you drop a GameObject into a Transform public variable in the Inspector, it automatically gets the Transform component. So, that part should be fine.
However, if the enemy is shooting at the player's spawn location and continually rotating to face that location, it still sounds like the "player" variable is not set correctly. How are you assigning that variable? If it is assigned in the Inspector, try removing what you currently have set and then make sure you assign the same GameObject that is being moved by whatever script is moving your player.
Also make sure you are setting that GameObject in the enemy shooting script as "player" and not in the enemy movement script as "target". You don't seem to use "target" anywhere in the enemy movement script.
If trying to set the "player" object from scratch does not solve it, perhaps posting a screenshot or link to one showing your script settings here would be provide more information.
Your answer
Follow this Question
Related Questions
Enemys jittering when moving 1 Answer
Enemy turn AI 2 Answers
Enemy behavior help 1 Answer
2D Top Down 4-directional RPG: Enemy Animation / Follow (beginner) 2 Answers
Spaceship AI for 3d game 1 Answer