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
0
Question by navadeep2011 · Apr 22, 2013 at 10:19 AM · lookatmousedrag

Need help in drraging code?

Hi all,

In the morning i asked one question
http://answers.unity3d.com/questions/442791/how-will-my-camera-to-move-in-the-direction-of-dra.html

but i didn't get any replies. ok For that. I tried like this.

  using UnityEngine;
  using System.Collections;

 public class ShowObject : MonoBehaviour {

     public Transform myObject;
     public Camera myCamera;
     bool check = true;
     Vector3 positionOne,positionTwo;
     // Use this for initialization
     void Start () {

     }

     // Update is called once per frame
     void Update () {
     if(Input.GetMouseButtonDown(0) ){
                     //Debug.Log("Drag");
                     check = true;
                     positionOne = Input.mousePosition;
             }
             if(Input.GetMouseButton(0)) {
                     if(check) {
                             if(positionOne != Input.mousePosition) {
                                     if(positionOne.x < Input.mousePosition.x ) {
                                             myCamera.transform.position = new Vector3 (myCamera.transform.position.x+(-positionOne.x + Input.mousePosition.x)*0.01f,myCamera.transform.position.y,myCamera.transform.position.z);

                                     }
                                     else {
                                             myCamera.transform.position = new Vector3 (myCamera.transform.position.x+(-positionOne.x + Input.mousePosition.x)*0.01f,myCamera.transform.position.y,myCamera.transform.position.z);


                                     }
                                     if(positionOne.y < Input.mousePosition.y) {
                                             myCamera.transform.position = new Vector3 (myCamera.transform.position.x,myCamera.transform.position.y+(-positionOne.y + Input.mousePosition.y)*0.01f,myCamera.transform.position.z);


                                     }
                                     else {
                                             myCamera.transform.position = new Vector3 (myCamera.transform.position.x,myCamera.transform.position.y+(-positionOne.y + Input.mousePosition.y)*0.01f,myCamera.transform.position.z);


                                     }
                                     myCamera.transform.LookAt(myObject);
                             positionTwo = Input.mousePosition;
                                     positionOne = positionTwo;

                             }
                     }
             }
     }

      }


I created an empty game object and i added this code. in the inspector view also i added all the things.

My problem is when i am dragging first time the object is not shown correctly. my main camera totally changes some 10 points in the x,y,z directions. In the second time onwards it is prefect.

in my observation if i drag , I object is going back looks like but really camera is going back.

can i get any help. please help me

thanks

Navadeep

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 robertbu · Apr 22, 2013 at 03:06 PM

I took a look at your original question. Primarily I did not answer because I could not figure out exactly what you were trying to do. The question needed more information or a picture. A more minor issues is that you did not include any code, so it looked like you were asking for someone to write a script for you rather than technical help for a specific problem. Looking at your code above, I'm even more confused about your previous question since you will not get a 360 view translating the camera. With that said, here is a bit of code you can attach to the camera to drag the camera. It uses the same approach as your code but simplifies the logic:

 public class CamDrag : MonoBehaviour {
 
     public float sensitivity = 0.02f;
     private Vector3 v3Prev;
     
     void Update () {
         if (Input.GetMouseButtonDown(0)) {
             v3Prev = Input.mousePosition;
         }
         if (Input.GetMouseButton (0)) {
             transform.position += (Input.mousePosition - v3Prev) * sensitivity; 
             v3Prev = Input.mousePosition;
         }
     }
 }

Note this code is not screen resolution independent. That is, if you are targeting your game for different screen resolutions, the movement will be different depending on the resolution.

Note for your original question, you might explore Transform.RotateAround().

Comment
Add comment · Show 2 · 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 navadeep2011 · Apr 22, 2013 at 05:53 PM 0
Share

Thanks for your reply mr Robertbu, still i am getting same output after i replace with your code also.I have an object which i need to show this view 360 degrees in all directions(i.e both in horizontal and vertical directions). Actually in this case when i am attempt to drag the mouse to left most and right most then camera view should be focus to object and will shows upto 360 degrees accordingly and also in top most and bottom vice versa, so could you please let me know how to implement the code for this issue and that will be great help ful to me Thanks in advance.

avatar image robertbu · Apr 22, 2013 at 09:53 PM 0
Share

As I explained before, I don't fully understand your needs. Here is the above code rewritten to use RotateAround() (as I suggested). Note when using two different rotation like this to rotate around, it is easy to get into positions where the mouse movements aren't intuitive with respect to the camera movements. But this might give you a starting point:

 using UnityEngine;
 using System.Collections;
 
 public class Around : $$anonymous$$onoBehaviour {
  
     public Transform target;
     public float sensitivity = 0.2f;
     private Vector3 v3Prev;
  
     void Update () {
        if (Input.Get$$anonymous$$ouseButtonDown(0)) {
          v3Prev = Input.mousePosition;
        }
        if (Input.Get$$anonymous$$ouseButton (0)) {
          transform.RotateAround(target.position, target.transform.right,  (Input.mousePosition.y - v3Prev.y)  * sensitivity); 
          transform.RotateAround(target.position, target.transform.up,  (Input.mousePosition.x - v3Prev.x)  * sensitivity); 
          v3Prev = Input.mousePosition;
        }
     }
 }

 

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

11 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How do you force a player to look at something? 4 Answers

Define var target : Transform; within the script 1 Answer

Make an FPS gun point at the mouse position. 2 Answers

Look at target...is the target Destoy? 1 Answer


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