1
\$\begingroup\$

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).

\$\endgroup\$
6
  • \$\begingroup\$ What have you done so far? \$\endgroup\$
    – DMGregory
    Dec 8, 2018 at 17:22
  • \$\begingroup\$ 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. \$\endgroup\$
    – W0LFB0MB
    Dec 8, 2018 at 17:52
  • \$\begingroup\$ 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. \$\endgroup\$
    – Anko
    Dec 8, 2018 at 19:41
  • \$\begingroup\$ I edited the question to make it make more sense. \$\endgroup\$
    – W0LFB0MB
    Dec 8, 2018 at 20:16
  • \$\begingroup\$ 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 ;) \$\endgroup\$
    – user115399
    Dec 8, 2018 at 21:27

2 Answers 2

1
\$\begingroup\$

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

tf.Rotate(0f,  Input.GetAxis("Mouse X") * speed, 0f);
\$\endgroup\$
0
\$\begingroup\$
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.

\$\endgroup\$
1
  • 2
    \$\begingroup\$ Just come back in 2 days, no need to duplicate the content :) \$\endgroup\$
    – Vaillancourt
    Dec 9, 2018 at 3:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .