Good day/night! I’m making a 2D shooter and I’ve come across a strange error in the game that appeared yesterday. The player movement was working before, but then it suddenly stopped. I redid the movement code and that didn’t work. I used a few debug statements to see if Update() was reaching the code and it was. After a few hours of testing new things, I realized something. The player isn’t moving, but something is.
I have a segment of code that makes the enemy shoot a bullet at the player. No matter the position of the player, the bullet will head towards it. When I hit the arrow keys, the player was still, but the enemy’s bullets would move in the direction I “moved” to. Nothing else has the player movement script.
Here is my current player movement class. If you see that I have two areas that deal with movement, that was me trying to fix the issue. One is commented out, feel free to test the other one since neither of them work:
I commented out a few variables since to see if they were part of the problem. Disregard them.
public class PlayerMovement : MonoBehaviour
{
public GameObject bullets; //bullet prefab
public GameObject bulletPos1;
public float speed;
public int playerLives;
//private CameraShake shake;
// Start is called before the first frame update
void Start()
{
playerLives = 3;
// shake = GameObject.FindGameObjectWithTag("Screenshake").GetComponent<CameraShake>();
//hitByEnemy = false;
}
// Update is called once per frame
void Update()
{
/* 2nd movement code, not working
if (Input.GetKey(KeyCode.LeftArrow))
{
Debug.Log("Pressing down");
Vector2 pos = transform.position;
pos.x-=speed;
transform.position = pos;
}
if (Input.GetKey(KeyCode.RightArrow))
{
Debug.Log("Pressing down2");
Vector2 pos = transform.position;
pos.x += speed;
transform.position = pos;
}
if (Input.GetKey(KeyCode.UpArrow))
{
Debug.Log("Pressing down3");
Vector2 pos = transform.position;
pos.y += speed;
transform.position = pos;
}
if (Input.GetKey(KeyCode.DownArrow))
{
Debug.Log("Pressing down4");
Vector2 pos = transform.position;
pos.y -= speed;
transform.position = pos;
} */
//fire bullets when spacebar is hit, works
if (Input.GetKeyDown("space"))
{
GameObject bullet1 = (GameObject)Instantiate(bullets);
bullet1.transform.position = bulletPos1.transform.position;
}
//For the first movement code
float x = Input.GetAxisRaw("Horizontal");
float y = Input.GetAxisRaw("Vertical");
Vector2 direction = new Vector2(x, y).normalized;
Move(direction);
}
//first movement code
void Move(Vector2 direction)
{
Debug.Log("In the move func");
Vector2 min = Camera.main.ViewportToWorldPoint(new Vector2(0, 0)); //bottom left of cam
Vector2 max = Camera.main.ViewportToWorldPoint(new Vector2(1, 1)); //top right of cam
max.x = max.x - 0.225f;
min.x = min.x + 0.225f;
max.y = max.y - 0.285f;
max.y = max.y + 0.285f;
Vector2 pos = transform.position;
pos += direction * speed * Time.deltaTime;
pos.x = Mathf.Clamp(pos.x, min.x, max.x);
pos.y = Mathf.Clamp(pos.y, min.y, max.y);
transform.position = pos;
}
//works
void OnTriggerEnter2D(Collider2D col)
{
Debug.Log("Lives: " + playerLives);
if ((col.tag == "EnemyShip") || (col.tag == "EnemyBullet"))
{
if (playerLives > 0)
{
//shake.CamShake();
playerLives -= 1;
}
else
{
// shake.CamShake();
Destroy(gameObject);
}
}
}
}
All possible solutions are appreciated. If you need me to upload anything else, just ask. Thank you!