Hi guys
when my ship is destroyed it respawns and then rotates, like its taking the previous rotation into account.
i have tried using
rb.velocity = Vector3.zero;
rb.position = Vector3.zero;
but it just doesnt seem to be effecting my RigidBody2D
everything else is working great except this.
any hints appreciated
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PlayerShipConrols : MonoBehaviour
{
public Animator hyperspaceAnimator;
public Rigidbody2D rb;
public GameObject weaponSpawnPoint;
public SpriteRenderer spriteRenderer;
public Collider2D collider;
public ParticleSystem engineParticleSystem;
public GameObject playerShipExplodes;
public ParticleSystem hyperspaceLightning;
public Text scoreText;
public Text livesText;
public Color inColor;
public Color normalColor;
public float thrust;
public float turnThrust;
private bool hyperSpace = false;
private bool canHyperspace = true;
private bool isAlive = true;
private float thrustInput;
private float turnInput;
/*public float screenTop;
public float screenBottom;
public float screenLeeft;
public float screenRight;*/
public float deathForce;
public int shipCount;
public float laserRedSpeed;
public GameObject laserRed;
public int score;
// Start is called before the first frame update
void Start()
{
score = 0;
scoreText.text = "Score " + score;
livesText.text = "Ships left " + shipCount;
}
// Update is called once per frame
void Update()
{
AllPlayerControls();
//ScreenWrapping();
}
public void AllPlayerControls()
{
thrustInput = SimpleInput.GetAxis("Vertical");
turnInput = SimpleInput.GetAxis("Horizontal");
}
private void FixedUpdate()
{
if (thrustInput > 0 && !hyperSpace && isAlive)
{
FindObjectOfType<AudioManager>().Play("PlayerThrust");
rb.AddRelativeForce(Vector2.up * thrustInput);
engineParticleSystem.Emit(1);
} else
{
FindObjectOfType<AudioManager>().Stop("PlayerThrust");
engineParticleSystem.Emit(0);
}
/* if (rb.angularVelocity < maxAngularVelocity && rb.angularVelocity > -maxAngularVelocity)
{
//rb.AddTorque(-turnInput);
RotateShip();
}*/
transform.Rotate(Vector3.forward * turnInput * Time.deltaTime * -turnThrust);
}
public void FireLaser()
{ if (!hyperSpace && isAlive)
{
FindObjectOfType<AudioManager>().Play("PlayerLaserRed");
GameObject newLaserRed = Instantiate(laserRed, weaponSpawnPoint.transform.position, transform.rotation);
newLaserRed.GetComponent<Rigidbody2D>().AddRelativeForce(Vector2.up * laserRedSpeed);
Destroy(newLaserRed, 2F);
}
}
public void Hyperspace()
{
if (canHyperspace && isAlive) {
FindObjectOfType<AudioManager>().Play("HyperspaceEnter");
hyperSpace = true;
collider.enabled = false;
hyperspaceAnimator.Play("ToHyperspace");
Invoke("ReturnFromHyperspace", 3);
}
}
public void ReturnFromHyperspace()
{
if (canHyperspace) {
rb.velocity = Vector2.zero;
FindObjectOfType<AudioManager>().Play("HyperspaceExit");
hyperspaceAnimator.Play("ReturnHyperspace");
Vector2 newPosition = new Vector2(Random.Range(-18f, 18f), Random.Range(-13f, 13f));
//stop speed after hyperspace???
transform.position = newPosition;
hyperspaceLightning.Emit(1);
hyperSpace = false;
canHyperspace = false;
StartCoroutine(HyperspaceTimer());
}
}
IEnumerator HyperspaceTimer()
{
int waiting = 7;
yield return new WaitForSeconds(waiting);
canHyperspace = true;
}
/*private void ScreenWrapping()
{
Vector2 newPosition = transform.position;
if (transform.position.y > screenTop)
{
newPosition.y = screenBottom;
}
if (transform.position.y < screenBottom)
{
newPosition.y = screenTop;
}
if (transform.position.x > screenRight)
{
newPosition.x = screenLeeft;
}
if (transform.position.x < screenLeeft)
{
newPosition.x = screenRight;
}
transform.position = newPosition;
}*/
void ScorePoints(int pointsToAdd)
{
score += pointsToAdd;
scoreText.text = "Score " + score;
}
private void OnCollisionEnter2D(Collision2D col)
{
if (col.relativeVelocity.magnitude > deathForce)
{
isAlive = false;
Instantiate(playerShipExplodes, transform.position, transform.rotation);
spriteRenderer.enabled = false;
collider.enabled = false;
FindObjectOfType<AudioManager>().Play("PlayerShipExplodes");
shipCount--;
livesText.text = "Ships Left " + shipCount;
//respawn new ship
Invoke("Respawn", 5);
if (shipCount <= 0)
{
//game over
}
}
}
void Respawn()
{
rb.velocity = Vector3.zero;
rb.position = Vector3.zero;
spriteRenderer.enabled = true;
spriteRenderer.color = inColor;
Invoke("Invulnerable", 3);
isAlive = true;
}
void Invulnerable()
{
collider.enabled = true;
spriteRenderer.color = normalColor;
}
}