Skip to main content
added 59 characters in body
Source Link
Big T Larrity
  • 1.4k
  • 4
  • 31
  • 61
public class Player : MonoBehaviour {
float rotationSpeed = 360f;
float currentThrust = 0f;
float thrustDecay = 0.02f;
float accel = 0.25f;
float maxThrust = 150f;

float thrustAngle, prevThrustAngle;
float maxTurningThrust;
public GameObject laserStartPoint;
float laserFireRate = 0.5f;
private float laserFireTimer = 5f; // the 5 is a random amount just to make sure no delay at the beginning

public GameObject laserPrefab;

private List<GameObject> lasers = new List<GameObject>();

private Collider gameArea;

private GameObject afterburner;
public GameObject explosionPrefab;

AudioSource audioSource;
public AudioClip laserSound, deathSound, deathExplosionSound, thrusterSound;
float deathWaitTimeBeforeDestroy = 2f;
bool isDying;
GameObject shipModel;

private bool isLeftToggled = false, isRightToggled;

void Start () {
    isDying = false;
    gameArea = GameObject.FindGameObjectWithTag("GameArea").GetComponent<Collider>();
    maxTurningThrust = maxThrust * 0.012f;
    afterburner = GameObject.FindGameObjectWithTag("Afterburner");
    afterburner.SetActive(false);
    audioSource = GetComponent<AudioSource>();
    shipModel = GameObject.FindGameObjectWithTag("ShipModel");
}

void Update () {
    if (isDying)
    {
        shipModel.SetActive(false);
        deathWaitTimeBeforeDestroy -= Time.deltaTime;
        if (deathWaitTimeBeforeDestroy <= 0)
        {
            Destroy(this.gameObject);
            GameController.instanceOf.PlayerLoseLife ();
        }
    }
    else
    {
        HandleInput();
        CheckScreenBounds();

        laserFireTimer += Time.deltaTime;
    }

    Debug.Log("is left toggled: " + isLeftToggled);
}

void HandleInput(){
    // activate weapon
    if (Input.GetKey(KeyCode.Space))
    {
        FireWeapon();
    }
    // Rotate left
    if (Input.GetKey(KeyCode.A) || isLeftToggled)
    {
        
        transform.Rotate(0, -rotationSpeed * Time.deltaTime, 0);
    }
    // Rotate right
    if (Input.GetKey(KeyCode.D))
    {
        transform.Rotate(0, rotationSpeed * Time.deltaTime, 0);
    }
    // activate thrusters
    if (Input.GetKey(KeyCode.W))
    {
        //audioSource.clip = thrusterSound;
        audioSource.PlayOneShot(thrusterSound, 0.25f);
        afterburner.SetActive(true);
        prevThrustAngle = thrustAngle;
        thrustAngle = transform.eulerAngles.y;
        if (currentThrust < maxThrust)
        {
            if (prevThrustAngle != thrustAngle)
            {
                if (currentThrust > maxTurningThrust) {
                    currentThrust = maxTurningThrust;
                }
            }
            currentThrust += accel;
        }
    }
    else
    {
        afterburner.SetActive(false);
        if (currentThrust > 0f)
        {
            currentThrust -= thrustDecay;
        }
    }


    // Move the ship in the direction it was facing at point when thruster button was last pressed
    transform.position += new Vector3(Mathf.Sin(thrustAngle * Mathf.Deg2Rad), 0, Mathf.Cos(thrustAngle * Mathf.Deg2Rad)) * currentThrust * Time.deltaTime;

    if (currentThrust < 0f)
    {
        currentThrust = 0f;
    }

}

void CheckScreenBounds()
{
    if (transform.position.x - (GetComponent<Collider>().bounds.size.x / 2) > gameArea.bounds.size.x / 2)
    {
        transform.position = new Vector3(-gameArea.bounds.size.x / 2, transform.position.y, transform.position.z);
    }
    if (transform.position.x + (GetComponent<Collider>().bounds.size.x / 2) < -gameArea.bounds.size.x / 2)
    {
        transform.position = new Vector3(gameArea.bounds.size.x / 2, transform.position.y, transform.position.z);
    }
    if (transform.position.z - (GetComponent<Collider>().bounds.size.z / 2) > gameArea.bounds.size.z / 2)
    {
        transform.position = new Vector3(transform.position.x, transform.position.y, -gameArea.bounds.size.z / 2);
    }
    if (transform.position.z + (GetComponent<Collider>().bounds.size.z / 2) < -gameArea.bounds.size.z / 2)
    {
        transform.position = new Vector3(transform.position.x, transform.position.y, gameArea.bounds.size.z / 2);
    }
}

void FireWeapon()
{
    if (laserFireTimer >= laserFireRate)
    {
        GameObject tempLaser = laserPrefab;

        tempLaser.transform.position = laserStartPoint.transform.position;
        tempLaser.transform.rotation = laserStartPoint.transform.rotation;
        Instantiate(tempLaser);

        lasers.Add(tempLaser);

        laserFireTimer = 0f;
        tempLaser = null;

        
        audioSource.PlayOneShot(laserSound);
    }
    
}

public void Die()
{
    GameObject explosion = GameObject.Instantiate(explosionPrefab);
    explosion.transform.position = transform.position;
    audioSource.PlayOneShot(deathSound);
    audioSource.PlayOneShot(deathExplosionSound);
    isDying = true;
}

private void OnTriggerEnter(Collider other)
{
    if (other.tag == "Asteroid" || other.tag == "Saucer" || other.tag == "LaserEnemy")
    {
        Die();         
    }
}


public void ToggleButtonLeft(){
    Debug.Log ("LEFT TOGGLED" + isLeftToggled);

    if (isLeftToggled)
    {
        isLeftToggled = false;
    }
    else 
    {
        isLeftToggled = true;
    }
}
public void ToggleButtonRight()
{
    Debug.Log("RIGHT TOGGLED");

    if (!isLeftToggled)
        isRightToggled = true;
    else
        isRightToggled = false;
}
public class Player : MonoBehaviour {
float rotationSpeed = 360f;
float currentThrust = 0f;
float thrustDecay = 0.02f;
float accel = 0.25f;
float maxThrust = 150f;

float thrustAngle, prevThrustAngle;
float maxTurningThrust;
public GameObject laserStartPoint;
float laserFireRate = 0.5f;
private float laserFireTimer = 5f; // the 5 is a random amount just to make sure no delay at the beginning

public GameObject laserPrefab;

private List<GameObject> lasers = new List<GameObject>();

private Collider gameArea;

private GameObject afterburner;
public GameObject explosionPrefab;

AudioSource audioSource;
public AudioClip laserSound, deathSound, deathExplosionSound, thrusterSound;
float deathWaitTimeBeforeDestroy = 2f;
bool isDying;
GameObject shipModel;

private bool isLeftToggled = false, isRightToggled;

void Start () {
    isDying = false;
    gameArea = GameObject.FindGameObjectWithTag("GameArea").GetComponent<Collider>();
    maxTurningThrust = maxThrust * 0.012f;
    afterburner = GameObject.FindGameObjectWithTag("Afterburner");
    afterburner.SetActive(false);
    audioSource = GetComponent<AudioSource>();
    shipModel = GameObject.FindGameObjectWithTag("ShipModel");
}

void Update () {
    if (isDying)
    {
        shipModel.SetActive(false);
        deathWaitTimeBeforeDestroy -= Time.deltaTime;
        if (deathWaitTimeBeforeDestroy <= 0)
        {
            Destroy(this.gameObject);
            GameController.instanceOf.PlayerLoseLife ();
        }
    }
    else
    {
        HandleInput();
        CheckScreenBounds();

        laserFireTimer += Time.deltaTime;
    }
}

void HandleInput(){
    // activate weapon
    if (Input.GetKey(KeyCode.Space))
    {
        FireWeapon();
    }
    // Rotate left
    if (Input.GetKey(KeyCode.A) || isLeftToggled)
    {
        
        transform.Rotate(0, -rotationSpeed * Time.deltaTime, 0);
    }
    // Rotate right
    if (Input.GetKey(KeyCode.D))
    {
        transform.Rotate(0, rotationSpeed * Time.deltaTime, 0);
    }
    // activate thrusters
    if (Input.GetKey(KeyCode.W))
    {
        //audioSource.clip = thrusterSound;
        audioSource.PlayOneShot(thrusterSound, 0.25f);
        afterburner.SetActive(true);
        prevThrustAngle = thrustAngle;
        thrustAngle = transform.eulerAngles.y;
        if (currentThrust < maxThrust)
        {
            if (prevThrustAngle != thrustAngle)
            {
                if (currentThrust > maxTurningThrust) {
                    currentThrust = maxTurningThrust;
                }
            }
            currentThrust += accel;
        }
    }
    else
    {
        afterburner.SetActive(false);
        if (currentThrust > 0f)
        {
            currentThrust -= thrustDecay;
        }
    }


    // Move the ship in the direction it was facing at point when thruster button was last pressed
    transform.position += new Vector3(Mathf.Sin(thrustAngle * Mathf.Deg2Rad), 0, Mathf.Cos(thrustAngle * Mathf.Deg2Rad)) * currentThrust * Time.deltaTime;

    if (currentThrust < 0f)
    {
        currentThrust = 0f;
    }

}

void CheckScreenBounds()
{
    if (transform.position.x - (GetComponent<Collider>().bounds.size.x / 2) > gameArea.bounds.size.x / 2)
    {
        transform.position = new Vector3(-gameArea.bounds.size.x / 2, transform.position.y, transform.position.z);
    }
    if (transform.position.x + (GetComponent<Collider>().bounds.size.x / 2) < -gameArea.bounds.size.x / 2)
    {
        transform.position = new Vector3(gameArea.bounds.size.x / 2, transform.position.y, transform.position.z);
    }
    if (transform.position.z - (GetComponent<Collider>().bounds.size.z / 2) > gameArea.bounds.size.z / 2)
    {
        transform.position = new Vector3(transform.position.x, transform.position.y, -gameArea.bounds.size.z / 2);
    }
    if (transform.position.z + (GetComponent<Collider>().bounds.size.z / 2) < -gameArea.bounds.size.z / 2)
    {
        transform.position = new Vector3(transform.position.x, transform.position.y, gameArea.bounds.size.z / 2);
    }
}

void FireWeapon()
{
    if (laserFireTimer >= laserFireRate)
    {
        GameObject tempLaser = laserPrefab;

        tempLaser.transform.position = laserStartPoint.transform.position;
        tempLaser.transform.rotation = laserStartPoint.transform.rotation;
        Instantiate(tempLaser);

        lasers.Add(tempLaser);

        laserFireTimer = 0f;
        tempLaser = null;

        
        audioSource.PlayOneShot(laserSound);
    }
    
}

public void Die()
{
    GameObject explosion = GameObject.Instantiate(explosionPrefab);
    explosion.transform.position = transform.position;
    audioSource.PlayOneShot(deathSound);
    audioSource.PlayOneShot(deathExplosionSound);
    isDying = true;
}

private void OnTriggerEnter(Collider other)
{
    if (other.tag == "Asteroid" || other.tag == "Saucer" || other.tag == "LaserEnemy")
    {
        Die();         
    }
}


public void ToggleButtonLeft(){
    Debug.Log ("LEFT TOGGLED" + isLeftToggled);

    if (isLeftToggled)
    {
        isLeftToggled = false;
    }
    else 
    {
        isLeftToggled = true;
    }
}
public void ToggleButtonRight()
{
    Debug.Log("RIGHT TOGGLED");

    if (!isLeftToggled)
        isRightToggled = true;
    else
        isRightToggled = false;
}
public class Player : MonoBehaviour {
float rotationSpeed = 360f;
float currentThrust = 0f;
float thrustDecay = 0.02f;
float accel = 0.25f;
float maxThrust = 150f;

float thrustAngle, prevThrustAngle;
float maxTurningThrust;
public GameObject laserStartPoint;
float laserFireRate = 0.5f;
private float laserFireTimer = 5f; // the 5 is a random amount just to make sure no delay at the beginning

public GameObject laserPrefab;

private List<GameObject> lasers = new List<GameObject>();

private Collider gameArea;

private GameObject afterburner;
public GameObject explosionPrefab;

AudioSource audioSource;
public AudioClip laserSound, deathSound, deathExplosionSound, thrusterSound;
float deathWaitTimeBeforeDestroy = 2f;
bool isDying;
GameObject shipModel;

private bool isLeftToggled = false, isRightToggled;

void Start () {
    isDying = false;
    gameArea = GameObject.FindGameObjectWithTag("GameArea").GetComponent<Collider>();
    maxTurningThrust = maxThrust * 0.012f;
    afterburner = GameObject.FindGameObjectWithTag("Afterburner");
    afterburner.SetActive(false);
    audioSource = GetComponent<AudioSource>();
    shipModel = GameObject.FindGameObjectWithTag("ShipModel");
}

void Update () {
    if (isDying)
    {
        shipModel.SetActive(false);
        deathWaitTimeBeforeDestroy -= Time.deltaTime;
        if (deathWaitTimeBeforeDestroy <= 0)
        {
            Destroy(this.gameObject);
            GameController.instanceOf.PlayerLoseLife ();
        }
    }
    else
    {
        HandleInput();
        CheckScreenBounds();

        laserFireTimer += Time.deltaTime;
    }

    Debug.Log("is left toggled: " + isLeftToggled);
}

void HandleInput(){
    // activate weapon
    if (Input.GetKey(KeyCode.Space))
    {
        FireWeapon();
    }
    // Rotate left
    if (Input.GetKey(KeyCode.A) || isLeftToggled)
    {
        
        transform.Rotate(0, -rotationSpeed * Time.deltaTime, 0);
    }
    // Rotate right
    if (Input.GetKey(KeyCode.D))
    {
        transform.Rotate(0, rotationSpeed * Time.deltaTime, 0);
    }
    // activate thrusters
    if (Input.GetKey(KeyCode.W))
    {
        //audioSource.clip = thrusterSound;
        audioSource.PlayOneShot(thrusterSound, 0.25f);
        afterburner.SetActive(true);
        prevThrustAngle = thrustAngle;
        thrustAngle = transform.eulerAngles.y;
        if (currentThrust < maxThrust)
        {
            if (prevThrustAngle != thrustAngle)
            {
                if (currentThrust > maxTurningThrust) {
                    currentThrust = maxTurningThrust;
                }
            }
            currentThrust += accel;
        }
    }
    else
    {
        afterburner.SetActive(false);
        if (currentThrust > 0f)
        {
            currentThrust -= thrustDecay;
        }
    }


    // Move the ship in the direction it was facing at point when thruster button was last pressed
    transform.position += new Vector3(Mathf.Sin(thrustAngle * Mathf.Deg2Rad), 0, Mathf.Cos(thrustAngle * Mathf.Deg2Rad)) * currentThrust * Time.deltaTime;

    if (currentThrust < 0f)
    {
        currentThrust = 0f;
    }

}

void CheckScreenBounds()
{
    if (transform.position.x - (GetComponent<Collider>().bounds.size.x / 2) > gameArea.bounds.size.x / 2)
    {
        transform.position = new Vector3(-gameArea.bounds.size.x / 2, transform.position.y, transform.position.z);
    }
    if (transform.position.x + (GetComponent<Collider>().bounds.size.x / 2) < -gameArea.bounds.size.x / 2)
    {
        transform.position = new Vector3(gameArea.bounds.size.x / 2, transform.position.y, transform.position.z);
    }
    if (transform.position.z - (GetComponent<Collider>().bounds.size.z / 2) > gameArea.bounds.size.z / 2)
    {
        transform.position = new Vector3(transform.position.x, transform.position.y, -gameArea.bounds.size.z / 2);
    }
    if (transform.position.z + (GetComponent<Collider>().bounds.size.z / 2) < -gameArea.bounds.size.z / 2)
    {
        transform.position = new Vector3(transform.position.x, transform.position.y, gameArea.bounds.size.z / 2);
    }
}

void FireWeapon()
{
    if (laserFireTimer >= laserFireRate)
    {
        GameObject tempLaser = laserPrefab;

        tempLaser.transform.position = laserStartPoint.transform.position;
        tempLaser.transform.rotation = laserStartPoint.transform.rotation;
        Instantiate(tempLaser);

        lasers.Add(tempLaser);

        laserFireTimer = 0f;
        tempLaser = null;

        
        audioSource.PlayOneShot(laserSound);
    }
    
}

public void Die()
{
    GameObject explosion = GameObject.Instantiate(explosionPrefab);
    explosion.transform.position = transform.position;
    audioSource.PlayOneShot(deathSound);
    audioSource.PlayOneShot(deathExplosionSound);
    isDying = true;
}

private void OnTriggerEnter(Collider other)
{
    if (other.tag == "Asteroid" || other.tag == "Saucer" || other.tag == "LaserEnemy")
    {
        Die();         
    }
}


public void ToggleButtonLeft(){
    Debug.Log ("LEFT TOGGLED" + isLeftToggled);

    if (isLeftToggled)
    {
        isLeftToggled = false;
    }
    else 
    {
        isLeftToggled = true;
    }
}
public void ToggleButtonRight()
{
    Debug.Log("RIGHT TOGGLED");

    if (!isLeftToggled)
        isRightToggled = true;
    else
        isRightToggled = false;
}
added screenshot
Source Link
Big T Larrity
  • 1.4k
  • 4
  • 31
  • 61

here is screenshot of unity. I think it sums up the problem quite well, the Event Trigger on the right shows its calling the method in the class I posted above, the log even shows its getting toggled to True when i push the button, but the ship doesnt rotate, and as you see in the log the bool is 'false' on every frame:

here is screenshot of unity:

here is screenshot of unity. I think it sums up the problem quite well, the Event Trigger on the right shows its calling the method in the class I posted above, the log even shows its getting toggled to True when i push the button, but the ship doesnt rotate, and as you see in the log the bool is 'false' on every frame:

added screenshot
Source Link
Big T Larrity
  • 1.4k
  • 4
  • 31
  • 61

here is screenshot of unity:

unity screen

here is screenshot of unity:

unity screen

added full class code
Source Link
Big T Larrity
  • 1.4k
  • 4
  • 31
  • 61
Loading
Source Link
Big T Larrity
  • 1.4k
  • 4
  • 31
  • 61
Loading