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 /
avatar image
0
Question by Poi7ioN · Apr 23, 2019 at 02:06 PM · instantiatevelocitytransform.positiondontdestroyonloadscene-change

Need some help with transform.position and velocity of object.

Here I'm developing a basic arkanoid game where ball bounces off a paddle and gamestatus.cs script keeps track of all score and powerups so i kept it as a dontdestroyonload but because of this as the scene changes the paddle references are missing and so does the balls so i made the paddle as dontdestory but kept the ball as it is. On the ball script there are two functions LockonPaddle and OnLaunchClick.

  public void LockOnPaddle()
     {
         Vector2 PaddlePos  = new Vector2(paddle1.transform.position.x, paddle1.transform.position.y);
         transform.position = PaddlePos + MouseToPaddlePos;
     }
 
     public void LaunchOnClick()
     {
         if(Input.GetMouseButtonDown(0))
         {
             HasStarted             = true;
             myRigidBody2D.velocity = new Vector2(LaunchX,LaunchY);
         }
     }

But when the scene changes the ball references are missing as it gets destroyed in the previous scene. and if i kept the ball as a dontdestroy it doesn't gets lockonpaddle on the next scene, it starts from its last position on previous scene. So i need some help regarding when the scene changes here

  public void DestroyBlocks()
         {
             BreakableBlocks--;
     
             if(BreakableBlocks <= 0)
             {
                 sceneLoader.LoadNextScene();
                 FindObjectOfType<Ball>().GetComponent<Rigidbody2D>().velocity = Vector2.zero;
                 FindObjectOfType<Ball>().LockOnPaddle();
                 FindObjectOfType<Ball>().LaunchOnClick();
             }
         }

i want to make the ball again stick to the paddle and launch on click . and the below is the lose on collider script where the ball collides the object and new ball is instantiated.

  public void OnTriggerEnter2D(Collider2D collision)
         {
             
             if (collision.gameObject.tag.Equals("NormalBall") == true)
             {
                 if(lifePower == 0)
                 {
                     collision.GetComponent<Collider2D>().isTrigger = true;
                     gameStatus                                     = FindObjectOfType<GameStatus>();
                     gameStatus.ChangePosition();
                     DestroyLevel();
                 }
                 else
                 {
                     for (var i= lifePower-1; i < lifePower; i++)
                     {
                         Instantiate(ball,new Vector3(paddle.transform.position.x, 0.799f, 0), Quaternion.identity);// Here Instead of Instantiating is it possible to transform the ball position and get it back on paddle.
                     }
                     GameStatus.countLives--;
                 }
             }
             else
             {
                 collision.GetComponent<Collider2D>().isTrigger = false;
             }
         }

A little help would be much appreciated. :)

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 Poi7ioN · Apr 24, 2019 at 10:46 AM

I've solved the problem , it was a silly mistake :) here is what i did ... created a function and made the bool hasstarted static and used in the sceneloader.cs script

 NextSceneBall()
 {
 HasStarted = false;
 }

so in the update function of ball when onclick , ball launches and after collision it reset itself.

 void Update()
     {
         if (!HasStarted)
         {
             LockOnPaddle();
             LaunchOnClick();
         }
         AddGravity();
         var speed = myRigidBody2D.velocity.magnitude;
         if(speed > 15f)
         {
             particleSystem.Play();
         }
     }


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

129 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

Related Questions

I need to move a clone of a prefab to a specific position after it instantiates 1 Answer

transform of instantiated bullet via ui button press not following the player 0 Answers

prefab always instantiates at 0.0.0 1 Answer

I want to make a gun with 2 parts and follow their positions and scales 2 Answers

When cloning, how do you make your clones keep the original properties? 2 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