Hi there, I wanted to ask if anyone knows about Unity and scripting because I have a problem.
The problem is that I’ve been looking for a script or tutorial for a long time that shows how I can spawn objects that come from the right and go to the left (the player has to avoid these objects).
Does anyone know a tutorial for this or a script for me?
Thanks!
There are many out there, just google Spawn Objects Unity and you should find plenty. Here is the first one i googled:
Try rewording you search next time
I even watched this video in full, but it didn’t work. I will try again now!
If it doesnt work, then paste your code into this thread and let the community try to help you find your errors. Make sure to use code tags from the text ribbon if you do.
Okay, maybe you can help me. I am supposed to do another one (boundaries) to do the tutorial. Then there was the problem that I have no idea how to change the “extents”, because the boundaries are too small and I have to make them bigger, but I have no idea how. Heres the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Boundaries : MonoBehaviour
{
public Camera MainCamera;
private Vector2 screenBounds;
private float objectWidth;
private float objectHeight;
void Start()
{
screenBounds = MainCamera.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, MainCamera.transform.position.z));
objectWidth = transform.GetComponent<SpriteRenderer>().bounds.extents.x; //extents = size of width / 2
objectHeight = transform.GetComponent<SpriteRenderer>().bounds.extents.y; //extents = size of height / 2
}
void LateUpdate()
{
Vector3 viewPos = transform.position;
viewPos.x = Mathf.Clamp(viewPos.x, screenBounds.x * -1 + objectWidth, screenBounds.x - objectWidth);
viewPos.y = Mathf.Clamp(viewPos.y, screenBounds.y * -1 + objectHeight, screenBounds.y - objectHeight);
transform.position = viewPos;
}
}[/code]
Heres the code again: (Because I failed it)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Boundaries : MonoBehaviour
{
public Camera MainCamera;
private Vector2 screenBounds;
private float objectWidth;
private float objectHeight;
void Start()
{
screenBounds = MainCamera.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, MainCamera.transform.position.z));
objectWidth = transform.GetComponent<SpriteRenderer>().bounds.extents.x; //extents = size of width / 2
objectHeight = transform.GetComponent<SpriteRenderer>().bounds.extents.y; //extents = size of height / 2
}
void LateUpdate()
{
Vector3 viewPos = transform.position;
viewPos.x = Mathf.Clamp(viewPos.x, screenBounds.x * -1 + objectWidth, screenBounds.x - objectWidth);
viewPos.y = Mathf.Clamp(viewPos.y, screenBounds.y * -1 + objectHeight, screenBounds.y - objectHeight);
transform.position = viewPos;
}
}
Looking at the boundaries video, i dont think you typed it properly. Look closely at the snippet of code from the video, yours is a little bit different:
After looking into the comments after a certain mistake, many had the same problem as me, so he has a new script on his website (video description, and the second script).
Then the error came up, which I meant here.
Well your main issue is that the boundaries are too small. That code should work well since it is using the Screen.height and width properties. But, since it is not, you can manually increase them by creating a variable (or 2) to add to the screenBounds line.
something like:
screenBounds = MainCamera.ScreenToWorldPoint(new Vector3(Screen.width + newVariableOne, Screen.height + newVariableTwo, etc. etc. etc.
Or you could have a multiplier and do Screen.width * widthMultiplier and the same for height. Make those new variables public so you can test it and change them while it is running and you can find the perfect area for your game.
It worked!
Thank you soooooo much my guy!
Of course. Glad you got it fixed.


