Respawned playerprefab unable to control ,Player death and respawn, but playerprefab unable to control or move
Hi...I have followed "The Life of Pottsy" video and steps to respawn player after the player has die. However, after respawned the player cannot control (i.e. no movement (left, right, up or down) even I try to control using the keyboard). How do I resolved this.
Below is the script for the player
public class SqScript : MonoBehaviour { public float bodyforce; public float bobspeed; public float jumpspeed;
// Update is called once per frame
void Update()
{
// Key down press right & left movement
if (Input.GetKeyDown(KeyCode.D))
{
transform.GetComponent<Rigidbody2D>().AddForce(Vector3.right * bodyforce);
transform.Translate(Vector3.right * bobspeed * Time.deltaTime);
}
else if (Input.GetKeyDown(KeyCode.A))
{
transform.GetComponent<Rigidbody2D>().AddForce(Vector3.left * bodyforce);
transform.Translate(Vector3.left * bobspeed * Time.deltaTime);
}
// Key down press jump movement, up and down
if (Input.GetKeyDown(KeyCode.W))
{
transform.GetComponent<Rigidbody2D>().AddRelativeForce(Vector3.up * bodyforce);
transform.Translate(Vector3.up * jumpspeed * Time.deltaTime);
}
else if (Input.GetKeyDown(KeyCode.S))
{
transform.GetComponent<Rigidbody2D>().AddForce(Vector3.down * bodyforce);
transform.Translate(Vector3.down * bobspeed * Time.deltaTime);
}
}
}
Below is the Player Death script public class PlayerDeath : MonoBehaviour {
void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("enemywhite"))
{
Destroy(gameObject);
print("Player Killed");
// Heartcounter.health -= 1;
print("Collided");
LevelManager.instance.Respawn();
}
}
}
Below is the LevelManager script
public class LevelManager : MonoBehaviour {
public static LevelManager instance;
public Transform respawnPoint; public GameObject playerPrefab;
private void Awake() { instance = this;
}
public void Respawn() { Instantiate(playerPrefab, respawnPoint.position, Quaternion.identity); }
}
Please help...thanks.
,I have followed "The Life of Pottsy" video and copy his code when the player is killed, it will respawn. The code went well and respawned. But the respawn playerprefab is unable control or move. How do I resolved this.
Below is the code
PLAYER CODE Below
public class SqScript : MonoBehaviour { public float bodyforce; public float bobspeed; public float jumpspeed;
// Update is called once per frame
void Update()
{
// Key down press right & left movement
if (Input.GetKeyDown(KeyCode.D))
{
transform.GetComponent<Rigidbody2D>().AddForce(Vector3.right * bodyforce);
transform.Translate(Vector3.right * bobspeed * Time.deltaTime);
}
else if (Input.GetKeyDown(KeyCode.A))
{
transform.GetComponent<Rigidbody2D>().AddForce(Vector3.left * bodyforce);
transform.Translate(Vector3.left * bobspeed * Time.deltaTime);
}
// Key down press jump movement, up and down
if (Input.GetKeyDown(KeyCode.W))
{
transform.GetComponent<Rigidbody2D>().AddRelativeForce(Vector3.up * bodyforce);
transform.Translate(Vector3.up * jumpspeed * Time.deltaTime);
}
else if (Input.GetKeyDown(KeyCode.S))
{
transform.GetComponent<Rigidbody2D>().AddForce(Vector3.down * bodyforce);
transform.Translate(Vector3.down * bobspeed * Time.deltaTime);
}
}
}
PLAYER DEATH CODE Below
public class PlayerDeath : MonoBehaviour {
void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("enemywhite"))
{
Destroy(gameObject);
print("Player Killed");
// Heartcounter.health -= 1;
print("Collided");
LevelManager.instance.Respawn();
}
}
}
LevelManager CODE below
public class LevelManager : MonoBehaviour {
public static LevelManager instance;
public Transform respawnPoint; public GameObject playerPrefab;
private void Awake() { instance = this;
}
public void Respawn() { Instantiate(playerPrefab, respawnPoint.position, Quaternion.identity); }
}
Your answer
Follow this Question
Related Questions
When touching ResSpawn C# Script 0 Answers
Problem with UNET calling methods 0 Answers
Why does my character keep respawning in the same place it dies? 0 Answers
Respawn point 1 Answer
cant stand in front of the platform after respawning 1 Answer