Hello. I am trying to make a 2D game and i need some help with may scripting.
I have a rock as my object. I want to spawn a random number of rocks at random positions. (similar to a space shooter’s asteroid spawning).
I want the speed of the spawning to increase or decrease when the player increases or decreases his speed or even better when the scrolling background increases or decreases his speed.
I also want to show an alert (icon, texture, button, etc., whatever is simpler) on screen at the position where the rock is going to spawn with 1 sec before the rock spawns…
Can someone please help me?
Consider creating a prefab which has components to render the “alert” sprite or whatever visual effects you desire. After n seconds, it instantiates your rock prefab at its own location, then destroys itself. Creating these at random positions is easy enough - sounds like you want to pick a random location within n units of the player. You can choose how often new rock-spawner prefabs are instantiated using whatever method you like, but you might consider having a RockManager class which will keep track of how many rocks are active, and simply add new ones until a desired number is achieved. Further, the RockManager - or a script on the rocks themselves - could be programmed to automatically destroy those which are sufficiently far from the player. This is more or less the way my work-in-progress game creates the desired numbers of each entity during play.
Ok i think i know how to do that in code but i want to enable or disable the alerts from the options section in the main menu.
Can you help me with some code with this?
I sure don’t mind sharing some advice. Learning how to approach common game design features will go a long way.
( For smaller projects, it’s probably sufficient and much easier to use PlayerPrefs Unity - Scripting API: PlayerPrefs for your saving / loading needs. My advice here is better suited for complex projects, but the principle is the same. )
Create a class for your player account to store things like high scores, inventory items, and per-account options like whether to display these warnings. I call mine PlayerAccount. Then I’ve got a singleton AccountLoader class which lets me save, load, and access the properties of my active account.
In whatever script displays your options menu, a button or toggle will write to a bool like AccountLoader.Instance.ActiveAccount.showWarningIcons. In game code, you can read the variable the same way to decide whether to display your warning effects.
Best of luck,
Hello.
I am trying to make a character controller fon android.
I want to move my object to the tap position and i want there to be a smooth transition from my object to the tap position.
I have the following code but the move it’s made instantly with no smoothing, even with Vector3.Lerp. Can someone help me?
public class MoveToTap : MonoBehaviour {
public float speed=5;
Vector3 target;
void Update ()
{
if(Input.touchCount > 0 Input.GetTouch(0).phase == TouchPhase.Began) {
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
if(Physics.Raycast(ray,out hit)) {
if(hit.collider != null) {
target = hit.point;
}
}
}
transform.position = Vector3.Lerp(transform.position, target, Time.deltaTime * speed);
}
}
}
}
This question should’ve gone in a new thread since it’s different from your original question, and it’s more likely people will see it that way too. Try forming a habit of using code tags around code snippets - the shortcut to do this is the # button you’ll see when creating a post. They make code much easier to read! 
To get the desired behavior with the fewest changes to your code, just add a class-scope Vector3 variable to hold your target position. As it is now, you’re only asking it to move during the frame in which you received input. I’m guessing if it snaps into place, your speed value is much too high. Then make use of this target position outside of your input check. It’s fine to use Vector3.Lerp for this, but it’s not your only option for creating motion towards a point in space!
public class MoveToTap : MonoBehaviour {
public float speed;
Vector3 target;
void Update () {
if(Input.touchCount > 0 Input.GetTouch(0).phase == TouchPhase.Began) {
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
if(Physics.Raycast(ray,out hit)) {
if(hit.collider != null) {
target = hit.point;
}
}
}
transform.position = Vector3.Lerp(transform.position, target, Time.deltaTime * speed);
}
}
Hello. My moving script is above.
I have 2 animations in my 2D game named “left” and “right”. One turns the characters eyes to the right th eother one to the left
I want my character to to play the left animation or at least turn his eyes to the left when i tap on the screen to move him to the left and play the right animation when i tap on the screen to move him to the right.
When he is not moving play my idle animation.
I asked in other parts of the forum and no answers.
Can someone help me?