• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by unity_MBVHvEMDJyG9OQ · Apr 18, 2019 at 10:18 AM · cameracursorfollow

(Camera follow cursor) Camera moves differently depending on my cursor's location when I start my Game. (Video in post)

Hello everyone,

I've making a 2D-scene where I've written a scriptthat makes the Camera follow the cursor (with boundaries). I have a problem however, the camera moves differently depending on where I start the cursor.

I made a video of how it acts so you can see here: https://streamable.com/q1irw

  1. The first time I start the Game (in the video above) with the cursor in the middle, the cursor makes the camera move as I want. The camera moves around, and when you reach the corners/edges, it has space where you can move the mouse without the camera moving (I think that feels best). This is how I would like the camera to always behave.

  2. However if I start the game/scene with the cursor located left/right/up/down, the camera moves smoothly, but those edges where the mouse can move without the camera moving dissappear, and the camera doesn't reach as far to the edges anymore either.

I would like the camera to move/behave just like it does in case 1 (when I start with the cursor in the middle) all the time, regardless of where the cursor is located at start. Does anyone know I should rewrite my code to achieve this?

The Camera-move-script I'm using (I've just attached it to the camera):

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class SmoothCameraFollow : MonoBehaviour {
 
     //public Transform target;
     public float cameraSpeed = 2;
     private Vector3 InitMousePos;
     private float xMouseInit;
     private float yMouseInit;
 
     public float MAX_X = 6;
     public float MIN_X = -6;
     public float MAX_Y = 3;
     public float MIN_Y = -3;
 
     void Start()
     {
         //Gets the FIRST cursor-position at the start of game 
         //(to subtract to goal-position in update, 
         //so that the camera moves unless the cursor moves)
         Vector3 InitMousePos = Input.mousePosition;
         InitMousePos.z = transform.position.z;
         InitMousePos = Camera.main.ScreenToWorldPoint(InitMousePos);
         xMouseInit = InitMousePos.x;
         yMouseInit = InitMousePos.y;
     }
 
     void Update()
     {
         //Gets the cursor-position in terms of pixels and 
         //translates it to game-coordinates each frame/update
         Vector3 v3 = Input.mousePosition;
         v3.z = transform.position.z;
         v3 = Camera.main.ScreenToWorldPoint(v3);
 
         //Create the coordinates to where the camera shall move.
         Vector3 newPos = transform.position;
         newPos.x -= v3.x - xMouseInit;
         newPos.y -= v3.y - yMouseInit; // yOffset;
 
 
         //Making sure that the camera doesn't exceed 
         //the min/max values it's allowed to move to!
         if (newPos.x > MAX_X)
         {
             newPos.x = MAX_X;
         }
         if (newPos.x < MIN_X)
         {
             newPos.x = MIN_X;
         }
 
         if (newPos.y > MAX_Y)
         {
             newPos.y = MAX_Y;
         }
         if (newPos.y < MIN_Y)
         {
             newPos.y = MIN_Y;
         }
 
         //Slowly moves the camera linearly with Lerp towards the new position
         transform.position = Vector3.Lerp(transform.position, newPos, cameraSpeed * Time.deltaTime);
     }
 }

Here are the Camera-Settings in case they are important: https://imgur.com/cmQzKIu

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by unity_MBVHvEMDJyG9OQ · Apr 19, 2019 at 08:03 AM

I think I managed to solve it on my own. I altered the script so it uses the mouse-input (tells if it's moving and in what direction) rather than where the cursor's pixel-coordinates are at. It doesn't work EXACTLY like in the script above, but works good enough for me :) here the finished script is if someone else needs it. It also has a zoom now. Just attach the script below to your camera and it should work:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class SmoothCameraFollow : MonoBehaviour {
 
     //The speed of the movement
     public float cameraSpeed = 0.5f;
     public float speedofIncrease = 3;
 
     //Min and max-values the camera can move to.
     private float MAX_X = 6;
     private float MIN_X = -6;
     private float MAX_Y = 3;
     private float MIN_Y = -3;
 
 
     //The current x and y movement of the cursor
     private float Xmouse;
     private float Ymouse;
 
     //Variables for zoom
     float ZoomAmount = 0; //With Positive and negative values
     public float MaxToClamp = 5;
     public float ROTSpeed = 5;
 
 
     void Start()
     {
 
     }
 
     void Update()
     {
 
         //THE ZOOM
         ZoomAmount += Input.GetAxis("Mouse ScrollWheel");
         ZoomAmount = Mathf.Clamp(ZoomAmount, -MaxToClamp, MaxToClamp);
         var translate = Mathf.Min(Mathf.Abs(Input.GetAxis("Mouse ScrollWheel")), MaxToClamp - Mathf.Abs(ZoomAmount));
         gameObject.transform.Translate(0, 0, translate * ROTSpeed * Mathf.Sign(Input.GetAxis("Mouse ScrollWheel")));
         //END OF ZOOM
 
         Xmouse = Input.GetAxis("Mouse X");
         Ymouse = Input.GetAxis("Mouse Y");
 
         //Fångar upp mus-positionen först i pixlar sen översatt till koord denna frame/update
         Vector3 v3 = Input.mousePosition;
         v3.z = transform.position.z;
         v3 = Camera.main.ScreenToWorldPoint(v3);
 
         //Skapar koordinater dit kameran ska röra sig.
         Vector3 newPos = transform.position;
         newPos.x += Xmouse*speedofIncrease;
         newPos.y += Ymouse*speedofIncrease; 
 
         //Making sure that the camera doesn't exceed the min/max values it's allowed to move to!
         if (newPos.x > MAX_X)
         {
             newPos.x = MAX_X;
         }
         if (newPos.x < MIN_X)
         {
             newPos.x = MIN_X;
         }
 
         if (newPos.y > MAX_Y)
         {
             newPos.y = MAX_Y;
         }
         if (newPos.y < MIN_Y)
         {
             newPos.y = MIN_Y;
         }
 
         //Flyttar långsamt/linjärt med Lerp kameran mot den nya koordinaten.
         transform.position = Vector3.Lerp(transform.position, newPos, cameraSpeed * Time.deltaTime);
     }
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

229 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Cinemachine not showing up in toolbar. 0 Answers

2D Object following the cursor without glitchyness at the center 0 Answers

Camera Follows 2D player 0 Answers

2.5D Camera follow script 0 Answers

Keep cursor centered with allowed rotation on 3rd person shooter?,How to get camera to follow cursor with cursor always centered? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges