Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Majorkowski · Jan 28, 2016 at 01:24 PM · spawnlocation

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;
 }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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);

@Majorkowski

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Majorkowski · Jan 30, 2016 at 09:04 PM 0
Share

@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.

avatar image haiderInUnity Majorkowski · Jan 31, 2016 at 09:13 AM 1
Share

@$$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;

avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Koaten · Jan 30, 2016 at 08:49 PM 1
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

42 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges