- Home /
Bullet not moving towards player
Im making a space shooter game, and I have enemys that shoot at the player, and the bullets are a bullet supposed to move towards the player. It moves NEAR the player, but not actually TOWARDS it. No matter what part of the screen the player is at (think star fox style), the enemy bullets are never actually hitting the player if hes standing still. heres my code using System.Collections; using System.Collections.Generic; using UnityEngine;
public class enemyBullet : MonoBehaviour {
public float speed = 30;
public GameObject player;
Vector3 targetLocation;
// Use this for initialization
void Start () {
player = GameObject.FindWithTag("shootPoint");
Debug.Log(player.gameObject.name);
targetLocation = player.transform.position;
setRotation();
}
// Update is called once per frame
void Update () {
GetComponent<Rigidbody>().velocity = transform.forward * speed;
}
void setRotation()
{
targetLocation = new Vector3(targetLocation.x, targetLocation.y, player.transform.position.z - 2);
transform.LookAt(targetLocation);
}
IEnumerator setRotationAgain()
{
yield return new WaitForSeconds(1f);
setRotation();
}
}
Answer by Atiyeh123 · Sep 04, 2018 at 11:24 AM
Hi, 1.There is no need for GetPlayerWithTag when its public , you can attach gameobject in editor. 2.I can not see any example when placing GetComponent in update is unavoidable , aasign it on start or Awake. Cache it once, or as little as possible. It is a costly venture to the engine. 3. your problem is with this line:
targetLocation = new Vector3(targetLocation.x, targetLocation.y, player.transform.position.z - 2);//why -2 ??
or maybe int your enemy rigidbody you freeze the x , y position! I've Corrected the code :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class OnCollide : MonoBehaviour {
public float speed = 2;
public GameObject player;
Vector3 targetLocation;
Rigidbody rb;
// Use this for initialization
void Start()
{
rb = GetComponent<Rigidbody>();
targetLocation = player.transform.position;
transform.LookAt(player.transform);
rb.velocity = transform.forward * speed;//Velocity Dont need to be in update
}
void Update()
{
//alternative ways
// 1. transform.position += transform.forward * speed * Time.deltaTime;
// 2. rb.MovePosition(rb.position + transform.forward * Time.deltaTime);
//translate ,movetowards ,....
}
void setRotation()
{
targetLocation = new Vector3(targetLocation.x, targetLocation.y, player.transform.position.z - 2);//why -2 ??
transform.LookAt(targetLocation);
}
IEnumerator setRotationAgain()
{
yield return new WaitForSeconds(1f);
setRotation();
}
}
Also i've attached the test project below link text
thanks for the reply, a couple of things: 1. I used GetPlayerWithTag because this is a bullet that gets spawned in the game, not something thats already in the scene. I know theres several other ways of doing it but that doesnt really matter 2. I know it can be a variable, I was just lazy 3. I believe the reason for the -2 in the targetLocation line is because the player is contantly moving forward, and the enemies stay a certain distance away from the player so they are constantly moving backwards as well. I probably should have mentioned that part in the description and I think its the reason why the bullets keep going behind him, Originally we had the target location being set in update but that made the bullets follow and become unavoidable, We used Start() because we want them to shoot at where the player is, but theyll have enough time to move out of the way. The issue here is that when the player is completely still, the bullets arent moving to the players location, they are either above/below/right/left of where he is.
1.you can just make bullet prefab and attach bullet prefab to public variable(it's more performant).i know it's not the problem , it's just a suggestion.
your problem:
So if you want 2 points distance from enemy you cant put rb.velocity int start. i correct my previous code.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class OnCollide : $$anonymous$$onoBehaviour
{
public float speed = 2;
public GameObject player;
Vector3 targetLocation;
Rigidbody rb;
// Use this for initialization
void Start()
{
rb = GetComponent<Rigidbody>();
targetLocation = player.transform.position;
setRotation();
}
void Update()
{
setRotation();
transform.position += transform.forward * speed * Time.deltaTime;
}
void setRotation()
{
targetLocation = new Vector3(player.transform.position.x - 2, targetLocation.y, player.transform.position.z - 2);
transform.LookAt(targetLocation);
}
IEnumerator setRotationAgain()
{
yield return new WaitForSeconds(1f);
setRotation();
}
}
but its not a good way to chasing player.if enemy always keep a constant distance from player you can set enemy child of player . you can also raycast from enemy to player too.(just search chasing player simple ai , there are good tutorials availables on youtube about it)
Answer by madks13 · Sep 06, 2018 at 08:17 AM
Umm, if it's like star fox, i'd suggest you don't move the player forward at all, unless it can be done with input. Instead, add the environment as children to an empty object that will be moved around the player. As for the enemies, in this kind of game, the enemies are usually scripted. Meaning they don't do random things, so you can plot out their movement around the player's position, rather than constantly keeping them at a distance of the player. This way, you won't have to compensate for player movement much either, only the horizontal and vertical movement, but no forward/backward movement.
Answer by Vice_Versa · Sep 07, 2018 at 04:22 PM
in case anyones wondering, I tried both versions of the code posted above, neither worked. here is the current code that Im trying to use using System.Collections; using System.Collections.Generic; using UnityEngine; public class enemyBullet : MonoBehaviour { public float speed = 2; public GameObject player; Vector3 targetLocation; // Use this for initialization void Start () { player = GameObject.FindWithTag("shootPoint"); targetLocation = player.transform.position; setRotation(); GetComponent<Rigidbody>().AddForce(transform.forward * speed, ForceMode.Impulse); } void setRotation() { targetLocation = new Vector3(targetLocation.x, targetLocation.y, targetLocation.z); transform.LookAt(targetLocation); } }
when I pause the game I can clearly see that the forward Vector is pointed directly at the player, but the bullet is not moving that way. This truely makes no sense to me, This bug has been persistant for weeks for a very very basic script.
I've Tested both posted codes and they work fine.I don't know why you don't search (chasing player youtube) to see the tutorials! This link covers your question link text
Err, this is clearly weird. The code is too simple to fail doing what it's supposed to do. So please check that you don't have other scripts affecting your bullets in some weird way. That is the only possibility i see.