English 中文(简体)
I was creating a ditto zig zag game i.e. available on Google play store by Ketchapp, Can anyone help me with the specific platform generation?
原标题:

enter image description here enter image description here

So, in the above images you can see my script generates platform though I want to limit the generation to only main camera s width i.e. the generated platform should not go beyond camera screen width. If you had played the available game you will know.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PathGenerator : MonoBehaviour

{
    private Vector3 spawnPos;
    [SerializeField] Vector3 lastPos;
    [SerializeField] float offset;
    [SerializeField] GameObject trackPrefab;

    private GameObject temp;

    [SerializeField] int amountToPool;
    List<GameObject> platformPool = new List<GameObject>();

    // Start is called before the first frame update
    void Start()
    {
        CreatePath(amountToPool);
        UsePath();
    }

    // Update is called once per frame
    void Update()
    {

    }

    private void CreatePath(int amt)
    {
        for (int i = 0; i < amt; i++)
        {
            temp = Instantiate(trackPrefab);

            temp.transform.SetParent(GameObject.Find("Tracks").transform);

            platformPool.Add(temp);
        }
    }

    private void UsePath()
    {
        foreach (GameObject p in platformPool)
        {
            RandomPicker();

            p.transform.position = spawnPos;

            lastPos = p.transform.position;
        }
    }

    public void RespawnTrack(GameObject pathGameObject)
    {
        RandomPicker();

        pathGameObject.transform.position = spawnPos;

        lastPos = pathGameObject.transform.position;
    }

    private void RandomPicker()
    {
        var randomNumber = Random.Range(0, 2);
        if (randomNumber < 1)
        {
            spawnPos = new Vector3(lastPos.x - offset, 0, lastPos.z);
        }
        else
        {
            spawnPos = new Vector3(lastPos.x, 0, lastPos.z - offset);
        }

        lastPos = spawnPos;
    }
}

enter image description here

I want to move my cam exactly and also you see how the platforms are never off screen.

问题回答

To achieve the desired effect you ll need to limit the spawn position when generating the path.

In your RandomPicker function, you re setting the next position to either be to the right or the left from the last position, here s where you can limit the position

    //.....
    float minX, maxX;
    float modelWidth = 4;

    private void Start()
    {
        //...rest of the code

        minX = Camera.main.ScreenToWorldPoint(Vector2.zero).x + modelWidth;
        maxX = Camera.main.ScreenToWorldPoint(Vector2.right * Screen.width).x - modelWidth;
    }


    private void RandomPicker()
    {
       //...rest of the code

       spawnPos.x = Mathf.Clamp(spawnPos.x, minX, maxX);
       lastPos = spawnPos;
 }

?Solution explanation: You can grab the screen info using the Screen class and its static members, such as width. This, however, will return info in "Screen space" coordinates, so we can use Camera.ScreenToWorldPoint to translate that info up.

Also, to fully achieve the desired effect, you ll want to line up the spawned path borders. Doing this on different path object sizes can be tricky, so be aware of the positioning and pivoting of the paths when creating/designing them

Some considerations when working with this kind of rule

  • Screen => Actual game screen, so it will reflect both the camera configuration and the running game (such as resizable windows)
  • Position zero in Screen coordinates refers to the bottom left corner, whereas the "max" position (using Screen.height and Screen.width) will return the top right corner

Note: this code snippet will only works on ortographic cameras, but can be managed on perspective ones.





相关问题
Finding the angle of a fleeing dodo

I want my critter AI to run directly away from the player when the player kicks it, and I don t know the math nor the syntax to calculate that angle. They are two characters moving independently ...

Game engines for iPhone vs. native iPhone sdk development

What are people s opinions and/or experiences with game engines such as Unity or Torque Engine? If one were new to iPhone game dev is it worth it to learn one of the engines? What is the performance ...

Random 2d movement (similar to flies) in Unity3d

I want to add random movement to some of my game objects similar to the way flies swarm in Unity3D. I ve developed a method using the addforce() method but would like to bypass the physics engine. ...

热门标签