Customise Consent Preferences

We use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below.

The cookies that are categorised as "Necessary" are stored on your browser as they are essential for enabling the basic functionalities of the site. ... 

Always Active

Necessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.

No cookies to display.

Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.

No cookies to display.

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.

No cookies to display.

Performance cookies are used to understand and analyse the key performance indexes of the website which helps in delivering a better user experience for the visitors.

No cookies to display.

Advertisement cookies are used to provide visitors with customised advertisements based on the pages you visited previously and to analyse the effectiveness of the ad campaigns.

No cookies to display.

[ad_1]

utilizing UnityEngine;

public class AlienMovement : MonoBehaviour
{
    [SerializeField]
    non-public float screenBorder = 30;

    public float moveSpeed = 1.0f;
    public Rigidbody2D rb;
    public float maxVariability = 4.0f;
    public float minVariability = -4.0f;

    non-public Digital camera camera1;

    non-public void Begin()
    {
        camera1 = Digital camera.primary;

        // Generate a random variability throughout the specified vary
        float variability = Random.Vary(minVariability, maxVariability);

        // Set the rate based mostly on variability
        rb.velocity = new Vector2(variability, 0) * moveSpeed;
    }
    non-public void FixedUpdate()
    {
        reverseVelocity();
    }

    /*
     * Checks if the Alien is close to the border
     * Adjustments the course to the other method if the Alien hits the border
     */
    non-public void reverseVelocity()
    {
        Vector2 screenPosition = camera1.WorldToScreenPoint(rb.place);
        if ((screenPosition.x < screenBorder) || (screenPosition.x > camera1.pixelWidth - screenBorder))
        {
            //Reverses the course
            rb.velocity = new Vector2(-rb.velocity.x, 0);

        }
    }
}
utilizing UnityEngine;

public class Boundaries : MonoBehaviour
{
    public Digital camera MainCamera;
    public Vector2 screenBounds { get; non-public set; }
    non-public float objectWidth;
    non-public float objectHeight;

    // Use this for initialization
    void Begin()
    {
        MainCamera = FindObjectOfType<Digital camera>();
        screenBounds = MainCamera.ScreenToWorldPoint(new Vector3(Display.width, Display.peak, MainCamera.rework.place.z));
        objectWidth =  rework.GetComponent<SpriteRenderer>().bounds.extents.x; //extents = measurement of width / 2
        objectHeight = rework.GetComponent<SpriteRenderer>().bounds.extents.y; //extents = measurement of peak / 2
    }

    // Replace known as as soon as per body
    void LateUpdate()
    {
        Vector3 viewPos = rework.place;
        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);
        rework.place = viewPos;
    }
}

Within the Unity editor, the code works, permitting the alien to reverse their velocity when it touches the border. Nevertheless, the alien is caught on the border after constructing the sport.

Movies

[ad_2]

Leave a Reply

Your email address will not be published. Required fields are marked *