- Home /
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. :)
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();
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612200045im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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