# Unity, hold down right mouse button to turn the camera

How would I use my right mouse button to turn a player character in Unity, while only turning in the direction and speed of the drag?

The game is 3D and you should hold down right click and drag to turn the camera.

Here is the code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Control : MonoBehaviour {

public int speed;
public Transform tf;

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {
if (Input.GetMouseButton(1))
{
tf.eulerAngles = new Vector3 (0, Input.GetAxis("Mouse X") * speed, 0);
}
}
}


The code above works, but after moving the mouse in the specified direction the character jumps back to its original position and kind of does this glitchy turn while moving the mouse (keeps jumping back to original position).

• What have you done so far? Dec 8, 2018 at 17:22
• I'm new to C# so I cant remember what things I did exactly (my game files are on my college PC) but it was an if statement t for if the mouse's X position was over or under 500 while right clicking, this worked but only rotated the character while holding right click on either side of the screen. I need one that moves to the speed and direction that you drag the mouse. Dec 8, 2018 at 17:52
• I think this question needs lots more detail to be answerable. Is your game 2D, or 3D? Top-down, or side view? The title implies you're asking about "dragging to turn", but the question body implies you also want movement. What does "moving in the direction and speed of the drag" mean exactly? A drawing might help.
– Anko
Dec 8, 2018 at 19:41
• I edited the question to make it make more sense. Dec 8, 2018 at 20:16
• it looks like a if(Input.GetMouseButton(0)) statement should do the trick. You could then just multiply a speed variable, by Input.GetAxis("MouseX") and use the product to turn your player on an axis ;)
– user115399
Dec 8, 2018 at 21:27

An even better solution IMO would be to use transform.Rotate:

tf.Rotate(0f,  Input.GetAxis("Mouse X") * speed, 0f);

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Control : MonoBehaviour {

public int speed;
public Transform tf;

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {
if (Input.GetMouseButton(1))
{
tf.eulerAngles = tf.eulerAngles + new Vector3 (0, Input.GetAxis("Mouse X") *
speed, 0);
}
}
}


^ I solved it, thanks @Gabriele Vierti.

Also, I can't accept my own answer for two days, someone please re answer this with the code so that I can set as the answer.

• Just come back in 2 days, no need to duplicate the content :) Dec 9, 2018 at 3:01