- Home /
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
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().
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.
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
![](https://koobas.hobune.stream/wayback/20220613100814im_/https://answers.unity.com/themes/thub/images/avi.jpg)