Player spawn in the center of the world
I want to have my player re-spawn at the start location after it touches an enemy but instead it spawns at the center of the world. Here is more code. Can you help me?
using UnityEngine; using System.Collections;
public class PlayerMovement : MonoBehaviour {
public float moveSpeed;
private Vector3 input;
Rigidbody cubeMovement;
private float maxSpeed = 5f;
public GameObject deathParticles;
private Vector3 movement;
private Vector3 spawn;
void Start () {
cubeMovement = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update () {
input = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical"));
if(cubeMovement.velocity.magnitude < maxSpeed)
{
cubeMovement.AddForce(input * moveSpeed);
}
if(Input.GetKey(KeyCode.A))
{
cubeMovement.AddForce(input * moveSpeed);
}
if(Input.GetKey(KeyCode.D))
{
cubeMovement.AddForce(input * moveSpeed);
}
if(Input.GetKey(KeyCode.W))
{
cubeMovement.AddForce(input * moveSpeed);
}
if(Input.GetKey(KeyCode.S))
{
cubeMovement.AddForce(input * moveSpeed);
}
}
void OnCollisionEnter(Collision other)
{
if (other.transform.tag == "Enemy")
{
Die ();
}
}
void OnTriggerEnter(Collider other)
{
if (other.transform.tag == "Goal")
{
GameManager.CompleteLevel();
}
}
void Die ()
{
Instantiate (deathParticles, transform.position, Quaternion.identity);
transform.position = spawn;
}
}
Answer by haiderInUnity · Jan 30, 2016 at 12:50 PM
Hi, The problem here is that the vector3 spawn is not initialized to anything and thus by default i believe it is vector.zero which is (0,0,0) thus the center of the world. When you call Die() it sets the transform.position of the object this. The solution is that on the start() you initialize spawn to whatever the start position you want like this spawn = new vector3(1,2,3);
@haiderInUnity Thanks for your answer, it works fine now but how do I write my code so that my player spawns at the location that I had put him originally so that I don't have to give the spawn location on every level/map/scene.
@$$anonymous$$ajorkowski You can store the player position in a new vector3 variable in start() like this Vector3 playerPos = transform.position; This will store the position of the player which you originally set in playerPos and in Die() you can then set transform.position = playerPos;
Answer by Majorkowski · Jan 30, 2016 at 08:44 PM
@haiderInUnity Thanks for your answer, it works fine now but how do I write my code so that my player spawns at the location that I had put him originally so that I don't have to give the spawn location on every level/map/scene.
Tip: Click the "Add comment" link under an answer to respond to that answer, ins$$anonymous$$d of creating a new answer to your own question.
Your answer
![](https://koobas.hobune.stream/wayback/20220612062053im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to properly position gameObjects one after the other (C#) 1 Answer
Doing a multiplayer location based game 0 Answers
How Do I Get An Object At Given Position? 0 Answers
Script component seems to be missing when spawning prefab? 2 Answers
Make a collision check when spawning with Instantiate 1 Answer