Screenwrap for my player in Asteroid Avoider not working

Hi there,

I am following Mobile game course and my Asteroid Avoider game seems far from finishing.

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

public class PlayerMovement : MonoBehaviour
{
    [SerializeField] private float forceMagnitude;
    [SerializeField] private float maxVelocity;
    [SerializeField] private float rotationSpeed;

    private Rigidbody rb;
    private Camera mainCamera;

    private Vector3 movementDirection;

    void Start()
    {
        rb = GetComponent<Rigidbody>();

        mainCamera = Camera.main;
    }

    void Update()
    {
        ProcessInput();

        KeepPlayerOnScreen();

        RotateToFaceVelocity();
    }

    void FixedUpdate()
    {
        if (movementDirection == Vector3.zero) { return; }

        rb.AddForce(movementDirection * forceMagnitude * Time.deltaTime, ForceMode.Force);

        rb.velocity = Vector3.ClampMagnitude(rb.velocity, maxVelocity);
    }

    private void ProcessInput()
    {
        if (Touchscreen.current.primaryTouch.press.isPressed)
        {
            Vector2 touchPosition = Touchscreen.current.primaryTouch.position.ReadValue();

            // Log the touch position before converting it to world position
            Debug.Log("Touch Position: " + touchPosition);

            // Check for infinite values in the touch position
            if (float.IsInfinity(touchPosition.x) || float.IsInfinity(touchPosition.y))
            {
                Debug.Log("Touch Position contains Infinity");
                return;
            }

            Vector3 worldPosition = mainCamera.ScreenToWorldPoint(touchPosition);

            // Log the world position after the conversion
            Debug.Log("World Position: " + worldPosition);

            movementDirection = transform.position - worldPosition;
            movementDirection.z = 0f;
            movementDirection.Normalize();
        }
        else
        {
            movementDirection = Vector3.zero;
        }
    }

    private void KeepPlayerOnScreen()
    {
        Vector3 newPosition = transform.position;
        Vector3 viewportPosition = mainCamera.WorldToViewportPoint(transform.position);
        bool positionChanged = false;

        Debug.Log("Initial viewport position: " + viewportPosition);

        if (viewportPosition.x > 1)
        {
            viewportPosition.x = 0;
            positionChanged = true;
        }
        else if (viewportPosition.x < 0)
        {
            viewportPosition.x = 1;
            positionChanged = true;
        }

        if (viewportPosition.y > 1)
        {
            viewportPosition.y = 0;
            positionChanged = true;
        }
        else if (viewportPosition.y < 0)
        {
            viewportPosition.y = 1;
            positionChanged = true;
        }

        Debug.Log("Updated viewport position: " + viewportPosition);

        if (positionChanged)
        {
            newPosition = mainCamera.ViewportToWorldPoint(viewportPosition);
            newPosition.z = transform.position.z;
            transform.position = newPosition;

            Debug.Log("Updated world position: " + newPosition);
        }
    }



    private void RotateToFaceVelocity()
    {
        if (rb.velocity == Vector3.zero) { return; }

        Quaternion targetRotation = Quaternion.LookRotation(rb.velocity, Vector3.back);

        transform.rotation = Quaternion.Lerp(
            transform.rotation, targetRotation, rotationSpeed * Time.deltaTime);
    }
}

Do you have any idea about what i am missing here? :thinking:

I think your ship is actually still on the screen. You just have your scale set WAAAAAAY up
image
A scale of 1 is what will be in a built game

i was thinking that scale part was only affecting the simulator wievport so my ship is visible on the first frames. How can i fix this issue in this case? :thinking:

You are looking at only a small part of the actual viewport, so it looks like the ship is not on the screen (because it’s not in the simulator) but the actual viewport is much larger, so the ship is actually still in the viewport and not wrapping. You can see this in your console: The values show that the ship is still within the viewport bounds, even when you are not seeing it on the screen. There’s nothing to solve. You need to change scale to 1 to see what it will look like on an actual mobile device. At that scale, the ship should wrap as you expect

I checked with my phone via unity remote app and scaled it to 1 but player still leaves the screen and it is not visible on the screen.

Do I need to set another scale for the player or its sprite?
My player game object has a 1, 1, 1 scale while its child StarSparrow’s Scale is 0.1,0.1,0.1.

I’ve tried to change these values as well but it seems the ship is still leaving the screen and does not comeback.

I’m not sure, but I tested the code (not in a mobile setting) and it wraps as it should. I’m not sure what else could be happening

The code is correct.

When you run in the simulator with the scaling set to 1, is it running correctly?

When you’re testing on Unity Remote, do you still have the scale set at 22 in the Device Simulator/Game window? In Unity Remote, the game is still running completely in the Editor. Unity Remote only sends what is in the Game View/Device Simulator and pulls touch information from the phone. The Camera (including the camera.worldtoviewport functions) has no idea that you’re even using Unity Remote. This means that the scale in the simulator is the scale that will be used when it’s passed on.

1 Like

Hi, thanks for the reply, I’ve tried to set the scale to 1 but it seems i clould only go 10 as the smalllest number in simulator.


still screenwrapping is not working with these setup in the simulator or with unity remote for mobile.

Yes, I see that now. In almost every other course context I TA, we’re strictly dealing with the Game View.
I took the code and pasted it directly into the course repo, and it worked correctly. I even split the screen so that the Scene view was visible, thinking it could be reading that instead. I simply couldn’t get that behavior to exhibit…

Until I added a rigidbody to the StarSparrow object under the player… Is there any chance you have a Rigidbody on the model?

1 Like

that’s a spot on fix :slight_smile: thank you so much :relieved:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms