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 game_crusher · Jun 25, 2015 at 12:36 PM · unity 5unity 2d

After resuming the game - wrong position of player

Hi,i have a bug in my infinite runner/platformer 2d game.I have a code which moves the camera to the right,this is forcing the player to stay within the bounds of the camera.Here is the script for it:

 public class CameraRunnerScript : MonoBehaviour {
 
     public Transform player;
 
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
         transform.position = new Vector3 (transform.position.x + 0.1f, 0, -10);


Now the problem is after pausing and resuming the game.Here is the code for that in another script:

 public class PauseGameMenu : MonoBehaviour {
 
     public bool isPaused;
     public GameObject pauseMenuCanvas;
 
 
     // Update is called once per frame
     public void Update () {
         if(isPaused)
         {
             pauseMenuCanvas.SetActive(true);
             Time.timeScale = 0f;
             //transform.position =GameObject.FindGameObjectWithTag("MainCamera").transform.position(0,0,0) ;
         }
         else
         {
             pauseMenuCanvas.SetActive(false);
             Time.timeScale = 1f;
 
         }
         if (Input.GetKeyDown (KeyCode.Escape)) 
         {
             isPaused = !isPaused;
         }
     
     }
 
     public void Resume ()
     {
         isPaused = false;
         pauseMenuCanvas.SetActive(false);
         Time.timeScale = 1f;
 
     }
 

I commented in this script my unsuccessful attempt to get the position of the main camera and reseted after resuming the game(actually i want the game to resume with the exact same position of the player and the platforms like before pausing the game).Of course there was some error like:

'UnityEngine.Transform' does not contain definition for position.

After that i tried to add the next lines in the first code(CameraRunnerScript) in Update():

 if(Time.timeScale == 0) 
         {
             transform.position = new Vector3 (0 , 0, -10);
         }

After resuming the game the player appears on some platform from the right side of the screen(outside of camera view) after a few seconds.I tried replacing the first 0(x position) with "transform.position.x" but then the while pausing, the player and the platforms kept "moving" to the left(actually the camera kept moving to the right as in playmode).So how do i resume the game from the same position as before pausing it?

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

1 Reply

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

Answer by game_crusher · Jun 25, 2015 at 10:29 PM

OK, i figured this one on my own.I added one line of code in the Update() function of the CameraRunnerScript.I counteracted the effect of moving the camera to the right in the playmode which is done with this line of code:

 transform.position = new Vector3 (transform.position.x + 0.15f, 0, -10);

with its reverse(moving the camera to the left with the same speed) in the pause mode - all done in Update():

  if(Time.timeScale == 0) 
         {
             transform.position = new Vector3 (transform.position.x-0.15f , 0, -10);
         }

I doubt this is the best solution for the CPU,so if anybody has a better solution i'm opened to suggestions

Comment
Add comment · 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

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

2 People are following this question.

avatar image avatar image

Related Questions

Vector3.MoveTowards doesnt work 1 Answer

Instantiated object not showing in scene or hierarchy 2 Answers

re-entering on the opposite side? 1 Answer

Need my character to move with platform,I need to make it so when i touch anything with the tag mov platform my character moves with it 0 Answers

Unity at fedora linux,Unity at fedora. 0 Answers


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