• 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 /
avatar image
0
Question by erge · Jun 05, 2013 at 11:23 PM · c#camera2dpositioncontrol

How do I control the camera position (in between borders)?

Hi guys and gals, I'm making a 2d sidescroller to touch devices and I'm currently stuck with the camera movement.

The camera starts at the given position, but the problem is that when I click on the screen, the camera goes off the scene and far to the right. So I have to drag me back (the camera) to the scene w$$anonymous$$le I hold the mouse button down. When I release the button it stays in position - but when I click again to scroll the same t$$anonymous$$ng happens again (the camera goes far to the right and I have to drag me back to the scene).

To prevent that the camera shall go off the scene, I have a "leftBorder" and a "rightBorder" where the camera not shall pass and only move in between these borders (aka the scene). The problem #2 is that the if statement is somehow ignored.

What am I doing wrong (or not doing at all)?

// erge

 using UnityEngine;
 using System.Collections;
 
 public class CameraMovement : MonoBehaviour {
     
     public bool isMoving = true;
     
     private float camX;
     private float camY = 38.0f;
     private float camZ = -74.0f;
     
     private float camStartPos = -58.7f;
     private float currentCamPos = Camera.main.transform.position.x;
     
     private float leftBorder = -79.0f;
     private float rightBorder = 87.0f;
     
     void Start () {
         Camera.main.transform.position = new Vector3(camStartPos, camY, camZ);
     }
     
     void Update () {
         // TODO: Move camera w$$anonymous$$le currentCamPos is between the borders
         if(currentCamPos >= leftBorder && currentCamPos <= rightBorder){
             if(Input.GetMouseButton(0)){
                 isMoving = true;
                 transform.position = new Vector3(camX = Input.mousePosition.x, camY, camZ);
                 Debug.Log("Mouse X Pos: " + Input.mousePosition.x);
             }
             else{
                 Debug.Log("Mouse Not Pressed");
             }
         }
         else{
             // TODO: Disable camera movement
             isMoving = false;
         }
     }
 }
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
Best Answer

Answer by gfvfubb · Jun 05, 2013 at 11:49 PM

You can't assign camera position values straight from mouseinput. It's not the same coordinate system. Well I mean you could if you were always going to have the exact same screen sizes (Screen.height / width) and they perfectly mapped to the area you were covering but that would be absurd, what you really want is a transformation matrix. Try t$$anonymous$$s:

 Ray hovray = Camera.main.ScreenPointToRay(Input.mousePosition);
         RaycastHit hov$$anonymous$$t =  new RaycastHit();
         if (Physics.Raycast(hovray,out hov$$anonymous$$t) ) { 
             wp = hov$$anonymous$$t.point;
             wp.y = 0;

With a ground collider t$$anonymous$$s will show where your mouse is intersecting with the terrain. Change the y value to be above the ground to where you want the camera.

Then save the position of the camera between successive updates when you have a mouseup and use that as the offset for the next mousedown. T$$anonymous$$s should solve your problem as I'm doing the exact same t$$anonymous$$ng with a mouseorbit and I had similar 'jumping' issues. If t$$anonymous$$s isn't clear enough say so.

There's another function also you can check out in API docs: ScreenToWorldPoint here is an example; The z coordinate is distance from the camera projection into the world. So t$$anonymous$$s resolves coordinates 30 units in from of the camera where the mouse is projecting through.

 Vector3 temps = Input.mousePosition;
 temps.z = 30;
 glit.transform.position = Camera.main.ScreenToWorldPoint(temps);

Alternatively if you don't want to do t$$anonymous$$s, just save the camera offset and mouse offset from mouseup, and then increase the camera's position by the difference in Input.mousepos values multiplied by a speed constant. Use GetInput for more sensitivity also.

Comment
Add comment · Show 3 · 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
avatar image pborg · Jun 06, 2013 at 01:10 AM 0
Share

gfvfubb is right. Use ScreenPointToRay & Raycast to get the new position. You may also want to use an invisible plane in the background of the level to give a stop point for the rays. That last one may not be necessary though; correct me if I'm wrong.

avatar image erge · Jun 06, 2013 at 08:25 PM 0
Share

I appreciate your answers! Where do you declare "wp" and what does it mean (is it a Vector3)? Unity doesn't like it. I replaced the code in the Update() with the first code ex. you gave me.

"Then save the position of the camera between successive updates when you have a mouseup and use that as the offset for the next mousedown." Could I use the "currentCamPos" to save the position?

avatar image erge · Jul 09, 2013 at 02:40 PM 0
Share

Thanks, I solved it using the "ScreenToWorldPoint()" as you said.

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

16 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

Related Questions

How to make camera position relative to a specific target. 1 Answer

Multiple resolutions considerations for Sprite Manger 2 0 Answers

How to make the Health bar on Enemys head not be in relation to Player(main) Camera? 2 Answers

Change the background color attribute of a camera in C#? 2 Answers

player goes crazy when using this, help! 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