Home Game Development Scrolling the screen left, right, up and down based on cameras rotation in Unity with C#

Scrolling the screen left, right, up and down based on cameras rotation in Unity with C#

0
Scrolling the screen left, right, up and down based on cameras rotation in Unity with C#

[ad_1]

I’m making a building game in Unity and C#

I want to be able to scroll the screen in all 4 directions when in building mode.

At the moment, I can rotate the camera and scroll the screen left and right, but no matter what rotation the camera is oriented to, it always scrolls in the same direction. I.e. Starting the game the camera pans, left, right as intended. But if I rotate the camera 90 degrees, scrolling left now scrolls up and right scrolls down.

I want the camera to pan left, right, up and down based on the camera’s rotation.
I still have not figured out how to pan up or down as this just moves the camera up and down, so I have commented this out for now.

I tried scrolling relative to the cameras rotation but that didn’t work. You can see my attempts in the update function.

I’ve been searching to find a solution but I’ve had no luck. This video shows my issue.

using System.Collections.Generic;
using UnityEngine;

public class CamFollowPlayerScript : MonoBehaviour
{
    //----------- This script rotates the cam in isometric veiw

    
    public GameObject player; // declare player gameobject
    public Vector3 spinCam; // declare vector 3 for the camera
    public float camSpinSpeed; // declare float to hold the speed of the camera rotation
    public bool isCameraFollwingMouse;
    public Cursor mouseCurson;
    public float mSpeed; // Scale. Speed of the movement
    public Camera isoCam;

    public GameObject eastDirectrion;

    public Vector3 mRightDirection;// = 3.right; // Direction the camera should move when on the right edge
    public Vector3 mleftDirection;// = Vector3.left; // Direction the camera should move when on the right edge
    public Vector3 mUpDirection;// = Vector3.up; // Direction the camera should move when on the right edge
    public Vector3 mDownDirection;// = Vector3.down; // Direction the camera should move when on the right edge

    private void Start()
    {
        mSpeed = 5;
        camSpinSpeed = 20f;
    }

    private void Update()
    {
       
        spinCam = new Vector3(Input.GetAxis("VerticalIso"), Input.GetAxis("HorizontalIso"), 0.0f); // spinCam is a new vector 3, taking the axis from the unity input settings on the X and Y axis and 0 on the Z axis
        this.transform.Rotate(spinCam * camSpinSpeed * Time.deltaTime); // rotate the camera using the vector 3 from the spin cam, by the spinCamSpeed float by time.delta time. 

         mUpDirection = new Vector3(0,0,0); // Direction the camera should move when on the right edge
         mDownDirection = new Vector3(0,0,0); // Direction the camera should move when on the right edge
         mRightDirection = new Vector3(isoCam.transform.rotation.x, 0, isoCam.transform.rotation.z);
         mleftDirection = new Vector3(-isoCam.transform.rotation.x, 0, -isoCam.transform.rotation.z);

        if (Input.GetKeyDown(KeyCode.M))
        {
            isCameraFollwingMouse = !isCameraFollwingMouse;
        }
    }

    void LateUpdate() // called after all update() methods have been called
    {
        if (!isCameraFollwingMouse)
        {
            transform.position = player.transform.position;// 
        }

        if (isCameraFollwingMouse)
        {
            // Check if on the right edge
            if (Input.mousePosition.x >= Screen.width )
            {
                // Move the camera
                transform.position += Vector3.right * Time.deltaTime * mSpeed;
            }

            if (Input.mousePosition.x <= 0)
            {
             // mm  // Move the camera
               transform.position += Vector3.left * Time.deltaTime * mSpeed;
            }

            if (Input.mousePosition.y >= Screen.height)
            {
                // Move the camera
               // transform.position += mUpDirection * Time.deltaTime * mSpeed;
            }

            if (Input.mousePosition.y <= 0)
            {
                // mm  // Move the camera
               // transform.position += mDownDirection * Time.deltaTime * mSpeed;
            }
        }
    }
}

[ad_2]