fbpx

 

Ultimate guide to g...
 

Ultimate guide to game dev - build problem with player  

   RSS

0

I have just finished the session 128 PC, MA and Linux Builds and ran into an issue with the player.

In Unity preveiw it works fine but whn I send it to build the player falls off the screen on the -X axis without any input from me. If I hold the right key down it starts to move to the right. I also can't move along the y axis. Apart from this issue, everything else works. What have I done wrong? I have done a google search but haven't found anything of help

Video of the issue

 

My player script

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : MonoBehaviour
{
[SerializeField]
private float _speed = 3.5f;
[SerializeField]
private float _fireRate = 0.5f;

[SerializeField]
private GameObject _laserPrefab;
[SerializeField]
private GameObject _tripleShotPrefab;
[SerializeField]
private GameObject _shield;
[SerializeField]
private GameObject _rtEngine;
[SerializeField]
private GameObject _ltEngine;

[SerializeField]
private int _lives = 3;
[SerializeField]
private int _score;

[SerializeField]
private AudioSource _laserAudio;

private float _speedMultiplier = 2;
private float _canFire = -1f;

private bool _isTripleShotActive = false;
private bool _isSpeedBoostActive = false;
private bool _isShieldBoostActive = false;

private UIManager _uimanager;
private SpawnManager _spawnManager;

 

// Start is called before the first frame update
void Start()
{
transform.position = new Vector3(0, 0, 0);

_spawnManager = GameObject.Find("Spawn_Manager").GetComponent<SpawnManager>();
_uimanager = GameObject.Find("Canvas").GetComponent<UIManager>();

if (_spawnManager == null)
{
Debug.LogError("The Spawn Manager is NULL.");
}

if(_uimanager == null)
{
Debug.LogError("The UI Manager is Null");
}

}

// Update is called once per frame
void Update()
{
CalculateMovement();

if (Input.GetKeyDown(KeyCode.Space) && Time.time > _canFire)
{
FireLaser();
}
}

void CalculateMovement()
{
float horizontalInput = Input.GetAxis("Horizontal");
float verticalInput = Input.GetAxis("Vertical");

Vector3 direction = new Vector3(horizontalInput, verticalInput, 0);

transform.Translate(direction * _speed * Time.deltaTime);

transform.position = new Vector3(transform.position.x, Mathf.Clamp(transform.position.y, -3.8f, 0));

if (transform.position.x >= 11.3f)
{
transform.position = new Vector3(-11.3f, transform.position.y, 0);
}
else if (transform.position.x <= -11.3f)
{
transform.position = new Vector3(11.3f, transform.position.y, 0);
}
}

void FireLaser()
{
_canFire = Time.time + _fireRate;

if (_isTripleShotActive == true)
{
Instantiate(_tripleShotPrefab, transform.position, Quaternion.identity);
}
else
{
Instantiate(_laserPrefab, transform.position + new Vector3(0, 1.05f, 0), Quaternion.identity);
}

_laserAudio.Play();

}

public void Damage()
{
if (_isShieldBoostActive == true)
{
_isShieldBoostActive = false;
_shield.SetActive(false);

return;
}

_lives -= 1;

_uimanager.UpdateLives(_lives);

if (_lives == 2)
{
_ltEngine.SetActive(true);
}

else if (_lives == 1)
{
_rtEngine.SetActive(true);
}

if (_lives < 1)
{
_spawnManager.onPlayerDeath();
Destroy(this.gameObject);
_uimanager.GameOver();

}
}

public void TripleShotActive()
{
_isTripleShotActive = true;

StartCoroutine(TripleShotPowerDown());

}

IEnumerator TripleShotPowerDown()
{
yield return new WaitForSeconds(5.0f);
_isTripleShotActive = false;

}

public void SpeedBoostActive()
{
_isSpeedBoostActive = true;
_speed *= _speedMultiplier;
StartCoroutine(SpeedBoostPowerDown());
}

IEnumerator SpeedBoostPowerDown()
{

yield return new WaitForSeconds(5.0f);
_isSpeedBoostActive = false;
_speed /= _speedMultiplier;
}

public void ShieldBoostActive()
{
_isShieldBoostActive = true;
_shield.SetActive(true);
}

public void AddScore(int points)
{
_score += points;
_uimanager.UpdateScore(_score);
}

}

 

TIA

@crazycgchick

I don't see anything wrong with your code.

I would check the horizontal input settings under project settings > axes. Then try to make another build.

@Thom Foxx

Thanks for the reply. I did try to change the horizontal inputs but no difference. Plus I build the game for WebGX and it was fine no issues what so ever.

Share:

 

 

Scroll to Top

Please Login or Register