0
\$\begingroup\$

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;
            }
        }
    }
}

\$\endgroup\$
4
  • \$\begingroup\$ have you tried transform.right("my right") instead of Vector3.right ("world right")? \$\endgroup\$
    – DMGregory
    Dec 6, 2023 at 14:20
  • \$\begingroup\$ Holy crap, this worked. Thank you so much. I was going round in circles and you answer my question instantly. I set it the rotation of the camera and now I can scroll left and right as desired. Thank you again Edit: Spelliing \$\endgroup\$ Dec 6, 2023 at 14:29
  • 1
    \$\begingroup\$ Is that all you needed? Is vertical scrolling working as intended? If so, you can post your revised code as an Answer below and mark it as accepted after a short waiting period. If you still need help, you can edit the question to show your current situation. \$\endgroup\$
    – DMGregory
    Dec 6, 2023 at 14:44
  • \$\begingroup\$ I've posted the revised code below and will check in in 48 hours and accept it as the answer. Thank you again dude. \$\endgroup\$ Dec 6, 2023 at 15:14

1 Answer 1

0
\$\begingroup\$

Using transform.right instead of Vector3.right worked great! I used it on my isoCamera camera object.

I used transform.forward and -transform.forward to move the camera forward and backwards. I used the rotation of an empty gameobject to set the forward and back direction as I am using a rotated isometric view on my camera.

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 float mSpeed; // Scale. Speed of the movement
    public Camera isoCam;
    public GameObject forwardFacingDirectionObject;

    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. 
           
        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 += isoCam.transform.right * Time.deltaTime * mSpeed;
            }

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

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

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

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .