Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by stephen_mmm · Apr 21, 2021 at 11:10 AM · positionpositioningscene loadstarting

Seemingly random bug when positioning player on scene start

Hi,

I'm having trouble with a script I've put together that's meant to position the player on the start of a new scene, depending on the previous scene they were in. The name of the previous scene is stored in a global variable, and this script checks for it and if found moves the player to the position of the script object. The "Player" in this case is an empty gameobject prefab that's parent to both a Character Controller and a Camera.

If I test this in a level, it mostly works fine and moves the entire prefab to the new location as intended. However - every once in a while, and seemingly at random, it only moves the Prefab and Camera and leaves the Character Controller in the original scene position.

I'm trying out different workarounds but would really appreciate being able to find out just what's going on, for my own knowledge if nothing else. The script I'm using is below - "gPrevLevel" is the previous level string. myLastLevelName is the level name being checked against.

 using UnityEngine;
 using System.Collections;
 
 public class LevelStartPosition : MonoBehaviour
 {
 
     public string myLastLevelName;
     public GameObject myPlayer;
     public bool myRotationFlag = true;
 
     private void Awake()
     {
         if (GlobalVar.gPrevLevel == myLastLevelName)
         {
             myPlayer.transform.position = transform.position;
             if (myRotationFlag == true)
             {
                 myPlayer.transform.rotation = transform.rotation;
             }
         }
     }
 
 
 
 }
Comment
Add comment · Show 4
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 gigos22 · Apr 21, 2021 at 04:49 PM 0
Share

I don't understand what are you saying when you wrote:

The "Player" in this case is an empty gameobject prefab that's parent to both a Character Controller and a Camera.

  1. Any gameObject can have only one parent.

  2. Character Controller is a component. Which means it should sit on this empty gameObject and not on another gameObject.

  3. In general, any fps or 3rd person character should be as such:

EmptyGameObject with CharacterController component,

MainCamera as a child of this empty,

A 3d model as a child of that camera

avatar image stephen_mmm · Apr 21, 2021 at 05:18 PM 0
Share

Sorry for confusion - in this case it means an empty (i.e. no components) gameobject which contains two different child gameobjects. One of these is the Character Controller and the other is the Main Camera. They're both being kept inside the one empty object for the sake of convenience when storing as a prefab.

avatar image gigos22 stephen_mmm · Apr 22, 2021 at 07:11 AM 0
Share

But then the whole logic should be wrong.. Or at least a big chunk of it..

How do you move the player? Because if it's not on the empty gameobject than it would literally move without it's parent.

I honestly believe that if you re-order your Player structure to be as I wrote above, it should solve this issue.

avatar image stephen_mmm gigos22 · Apr 22, 2021 at 08:08 AM 0
Share

No, it works fine in game, I think maybe too much emphasis is being put on me calling the container prefab the "Player" in the post. The Character Controller object it contains is also what has the mesh and animations attached to it and what actually moves around. The Camera object it also contains has a script to automatically look at and follow the Controller. They don't have any script dependencies on the parent container object at all. That object is only used to be able to move them both around at the same time and to keep them in a certain position relative to each other when adding the player to a level.

0 Replies

· Add your reply
  • Sort: 

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

141 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 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 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 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

Player position with script and screen sizes 0 Answers

Move object with Translate on X axis (with mouse) 0 Answers

Offset position to a target 3 Answers

My Position suddenly changes 0 Answers

Is there an elegant way of limiting camera's position? 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