Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
1
Question by kirkage · Jan 25, 2013 at 11:03 PM · cameradraghold

Click hold/drag move camera

Hello Unity peeps,

I'm pretty new at Unity and I'm trying to develop my first game. I've completed the course on lynda.com on Unity and I have a basic understanding of the software. I'm a web developer and I'm seasoned in PHP and Javascript so I use Javascript for my language in Unity. What I'm trying to create is game where the 3rd person army crawls across a plane.

Here's what I have:

It's a plane with a camera facing it on orthographic mode. I have rocks (spheres) where you will click and drag down and it will pull the 3rd person along. So, I need the camera to go the exact opposite way the user is dragging. When you click and drag the camera exposes more area ahead with more rocks and you will grab the next one with the opposite hand on the 3rd person rock to rock to rock.

Here's what I understand I need so far:

When I click that rock, I need to transfer from screen to world. I need the starting position of the rock click. This is the first problem I encountered. How do I get that first position? I can get the x and y position of the camera but it changes as it moves. How do I capture the very first point of the cameras position and save it in a variable to use? I figure as the user drags down on the screen, the position of the mouse can be reversed and put into the camera so it goes the exact opposite way. Any pointers in the right direction would help me out so much. I will post the update function I have right now in my click.js file on each of the spheres (rocks).

 function Update () {
 
  if (Input.GetMouseButtonDown(0))
 
         {
        
         
 var mousePos = Input.mousePosition;
 
 var cam = camera.main;
 var v = cam.ScreenToWorldPoint(Vector3(mousePos.x, mousePos.y, cam.nearClipPlane)); 
 
 
 Debug.Log(v.y);
 
 
 Camera.main.transform.position.y = v.y;
          
   
         }
 
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

3 Replies

· Add your reply
  • Sort: 
avatar image
5

Answer by Curdflappers · Aug 28, 2016 at 12:21 AM

I know I'm waaay late to the party but I've modified Franco Marini's script and cut down a few lines. Same functionality, but a bit dryer and more readable.

 private Vector3 ResetCamera; // original camera position
 private Vector3 Origin; // place where mouse is first pressed
 private Vector3 Diference; // change in position of mouse relative to origin

 void Start()
 {
     ResetCamera = Camera.main.transform.position;
 }

 void LateUpdate()
 {
     if(Input.GetMouseButtonDown(0))
     {
         Origin = MousePos();
     }

     if (Input.GetMouseButton(0))
     {
         Diference = MousePos() - transform.position;
         transform.position = Origin - Diference;
     }

     if (Input.GetMouseButton(1)) // reset camera to original position
     {
         transform.position = ResetCamera;
     }
 }

 // return the position of the mouse in world coordinates (helper method)
 Vector3 MousePos()
 {
     return Camera.main.ScreenToWorldPoint(Input.mousePosition);
 }
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
avatar image
4

Answer by sparkzbarca · Jan 25, 2013 at 11:21 PM

since your using orthographic camera im assuming your saying your basically making a 2d game.

It sounds like your "looking down" on the plane. As such your mouse exists above it.

That means to find the rock when they click you would cast a ray down.

Down can be represented simply as -Vector3.up

to get a direction you take the 2d x and y values of the mouse

and make x stay x but because your looking down. The Y value is depth or Z. then you cast from camera.transform.position.y to camera.transform.position.y + (-vector3.down) which is the same as saying (0,-1,0)

so

ray.origin = new Vector3(mouseposition.x,camera.transform.positon.y, mouseposition.z);

ray.direction = ray.origin + (-Vector3.up); or just ray.origin - Vector3.up if you want.

that casts straight down from the mouse to whatever is on the ground directly below it. thats if you want to know whats under the mouse for some reason.

Now for how to drag a camera.

if you want the camera to travel in the opposite direction of the mouse input.

every frame

input.getaxis("Mouse X") & input.getaxis("Mouse Y")

return how much the mouse has moved on that axis in that frame so if your wondering where the camera should be each frame if it should move exactly the opposite of the mouse.

 vector3 CameraPos;
 
 Float MouseX;
 Float MouseY;
 
 MouseX = input.getaxis("Mouse X");
 MouseY = input.getaxis("Mouse Y");
 CameraPos = new vector3(- MouseX, 0, - MouseY);
 
 camera.transform.position += CameraPos;

Notice we used - in front of the mousex and y to reverse the values.

also notice we didnt reset the whole value of the camera position we just added to it the opposite of the mouse input.

lastly notice we didnt change the Y value at all. the cameras height should be constant and the mouse should cause the camera to move along that axis.

MARK AS ANSWERED and have a nice day :)

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
avatar image
3

Answer by Franco-Marini · Jan 28, 2015 at 10:17 PM

Sorry if im late. this c++ script drags the camera.

 public class CameraDragMove : MonoBehaviour {
     private Vector3 ResetCamera;
     private Vector3 Origin;
     private Vector3 Diference;
     private bool Drag=false;
     void Start () {
         ResetCamera = Camera.main.transform.position;
     }
     void LateUpdate () {
         if (Input.GetMouseButton (0)) {
             Diference=(Camera.main.ScreenToWorldPoint (Input.mousePosition))- Camera.main.transform.position;
             if (Drag==false){
                 Drag=true;
                 Origin=Camera.main.ScreenToWorldPoint (Input.mousePosition);
             }
         } else {
             Drag=false;
         }
         if (Drag==true){
             Camera.main.transform.position = Origin-Diference;
         }
         //RESET CAMERA TO STARTING POSITION WITH RIGHT CLICK
         if (Input.GetMouseButton (1)) {
             Camera.main.transform.position=ResetCamera;
         }
     }
 }
 
Comment
Add comment · Show 1 · 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 jcmiron · Apr 11, 2015 at 05:48 PM 0
Share

This works good for me, in 2D. Thanks

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.

Follow this Question

Answers Answers and Comments

13 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

Related Questions

allows users to view 360º content without a VR headset 0 Answers

Dragging Camera based on Touch 0 Answers

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

Dragging Camera Speed 1 Answer

Drag Camera Around an Object With Kinect (Official Plug-In) 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