I am having a hard time figuring out a way so the player will wrap around the screen and for it to be able to do it no matter what size the screen is. Here is what I have so far:
using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour
{
private Transform myTransform;
Vector3 playerPosition;
Vector2 viewCoords;
public int playerSpeed = 5;
// Use this for initialization
void Start ()
{
myTransform = transform;
playerPosition = transform.position;
viewCoords = camera.WorldToViewportPoint(playerPosition);
// Spawn point x y z
// Postion to be at -3, -3, -1
myTransform.position = new Vector3(-3, -3, -1);
}
// Update is called once per frame
void Update ()
{
// Move the player left and right
myTransform.Translate(Vector3.right * playerSpeed * Input.GetAxis("Horizontal") * Time.deltaTime);
if (viewCoords.x < 0f || viewCoords.x > 1f)
{
playerPosition = new Vector3(playerPosition.x * -1, playerPosition.y, playerPosition.z);
}
}
}
Unity is telling me I need a camera attached to the player so I did that and what it does is that the player doesn’t appear to move when testing which I know why that is. So my question is do I need to make a wraping script for the camera to allow the player to wrap around the screen when it goes off screen or is there a way to do it in the Player script I made. I appreciate any help you all can give me,