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?

Not sure if I'm meant to post this here or stackoverflow.

EDIT: game is 3D

EDIT2: Hold right click and drag to turn the camera

Edit3: Got this 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);
    }
}
}

^ Code works, but after moving the mouse in the specified direction the character jumps back to its original position and kinda does this glitchy turn while moving the mouse (keeps jumping back to original position)

^ nevermind, I think that I figured it out

\$\endgroup\$
  • \$\begingroup\$ What have you done so far? \$\endgroup\$ – DMGregory Dec 8 '18 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 '18 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 '18 at 19:41
  • \$\begingroup\$ I edited the question to make it make more sense. \$\endgroup\$ – W0LFB0MB Dec 8 '18 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\$ – Gabriele Vierti Dec 8 '18 at 21:27
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
    \$\begingroup\$ Just come back in 2 days, no need to duplicate the content :) \$\endgroup\$ – Vaillancourt Dec 9 '18 at 3:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.