How to do an automatic scroll level in a 2D mobile game like in mario's game ?
Hi i just would like help for my project, I'm want to make an little game for android, I would like to know how to do an automatic scolling level like mario ( https://www.youtube.com/watch?v=wbwl7-JiTvA ) and if possible, an scrolling level which the camera goes from up to down. Thanks for helping me.
No prob, if it solved your problem, please mark the answer correct
Answer by highpockets · Apr 22, 2019 at 07:29 PM
Just add this script to your main camera:
public float speed; //adjust this in the inspector to make the scroll speed less or more
void Update(){
transform.Translate( 0, -speed, 0);
}
Just to elaborate, if you put positive speed as the y float parameter, it would scroll up, if you put it on the x parameter, speed would go right, -speed would go left. Hope that helps. Cheers
hi @highpockets
i used the script you gave me for my game it works very well, but now i have another problem i can't slove. when i give the scipt to a object, and when I rotate the object that contains your script, the object take another direction and does not go up to down as it should do.
(i decided to make object fall down in the players screen rather than make the play and the background go up because of a script who block me in order to not go out of the screen, i let you an image if intersted )
I'm not exactly sure I understand what you are saying here, but I think you mean that you assume that if you rotate the object that the script above is attached to, it should still move in the same direction, but the method translate will move according to the transform's local space unless you set it to world space:
https://docs.unity3d.com/ScriptReference/Transform.Translate.html
If you want it to always go up in world space (+y) or down in world space (-y). Then you can do it like this:
public float speed; //negative value for down and positive for up
void Update()
{
speed = speed * Time.deltaTime;
transform.Translate(0,-speed,0, Space.World); //Space.World sets the translation to worlds space
}
Answer by ozan036777 · Jun 13, 2019 at 05:24 PM
yeah that is what i wanted and you helped me again, thanks you ! again
Hi, @highpockets I have another question, how do we stop the scrolling when the "player" get Destroyed with the line code:
Destroy(collision.gameObject);
and if possible, add a texte when we stop the scrolling
I hope i was clear this time and thanks you, again
$$anonymous$$aybe you want to just pause the whole game?? If so, you can use Time.timeScale = 0.0f;
, but if you still have other things dependant on frame rate, you can make the scroll scripts inherit from a base class and just set the speed of each instance of the base scrolling class to 0.0f. Or you can give each of the scrolling objects a tag called “scroll”, then use FindGameObjectsWithTag() to get an array of the objects and set each scroll script speed variable to 0.0f..
Personally, I would do it with the base class option or you can also use an interface to do the same
what you asked is just too hard for the begginer I am. I really don't know how to time.scale. or can we just stop the game, or scrolling just by collision ? I almost finished my game but i don't know how to say, for exemple, If a player get collision, then stop scrolling. or what ever ...
If you want to stop all of the scrolling objects and they are each controlled by individual instances of the same script, you either have the option of pausing the game with Time.timeScale = 0.0f; which is quite simple, or you need to iterate through each instance and stop the scrolling. If you just want to stop the object in the collision, then you get the component and stop the scrolling. I don’t see another option
Your answer
Follow this Question
Related Questions
How to make a scroll rect NOT overflow? 1 Answer
Scroll list isn't working,Scroll list not scrolling. Can someone help me? 0 Answers
move automatically to item in scroll rect 0 Answers
Problems with dynamically adding and scrolling to Scrollview 0 Answers
How do I enable buttons that are within a scroll view? 0 Answers