Forum rules - please read before posting.

How to use right mouse button instead of left for drag-controlled camera?

I would like to use the right mouse button to control the camera spin and pitch rotation when "drag-controlled" is checked, but can't figure out how to set that.

Left-mouse clicking is still perfect for interactions, but I want to use right mouse clicking/dragging for camera spin and pitch dragging. Is there somewhere to do that relatively easily?

Searched around first but couldn't find anything about this.

Thanks!

Comments

  • Welcome to the community, @Ony.

    Are you referring to the "GameCamera 2D Drag" or the "GameCamera Third Person" camera? The former only allows for positional dragging - not rotational. If you can share a screenshot of the Inspector of the camera in question, that'll help understand what your situation is.

    By default, drag movement is detected whenever the left mouse button is held down. However, you can override this via the "Drag Override Input" box in the GameEngine object's Player Input component.

    In Unity's Input Manager, create a new input named e.g. "Right mouse button", and map it to "mouse 1" (see Unity's docs on this here).

    Then set the "Drag Override Input" field to match this new Input's name, i.e. "Right mouse button". Doing so should cause drag input to be detected whenever the right mouse button is held down.

  • OnyOny
    edited March 2019

    Yay! Thanks, Chris (also thanks for the welcome). That did the trick. :) For the record I am using the Third Person 3D game camera.

  • Hi Chris,

    I followed this advice for my 2D game and it works well. Right-clicking and dragging pans the camera and left-clicking interacts with hotspots.

    However, now when I interact with a 'Draggable' game object on a track I must use the right mouse button instead of the left. Is there any way I can keep the right-click and drag functionality to move the camera while using left-click and drag to interact with 'Draggable' game objects?

    Thank you for your help.

  • You can alter the field at runtime through script, so that it switches between left and right buttons dynamically:

    AC.KickStarter.playerInput.dragOverrideInput
    

    What are you currently setting this to?

    Is the same camera active when Draggables are involved, and do the Draggables have Hotspot components attached?

  • What are you currently setting this to?

    I am not setting that, so whatever is the default (My game contains almost no code written by me since I don't really know how to code).

    Is the same camera active when Draggables are involved, and do the Draggables have Hotspot components attached?

    Yes it is the same camera and yes I have a 'dummy hotspot' attached, just so the cursor changes when over the object to indicate it can be interacted with.

    https://imgur.com/a/tLShtQX

  • edited July 2024

    Sorry, I need to be clear: how are you setting the drag input to the right mouse button? The advice above involves overriding input in the "Drag Override Input" box, which the code I just mentioned references.

  • Sorry if I misunderstood. I did exactly what it says here:

    **_In Unity's Input Manager, create a new input named e.g. "Right mouse button", and map it to "mouse 1" (see Unity's docs on this here).

    Then set the "Drag Override Input" field to match this new Input's name, i.e. "Right mouse button". Doing so should cause drag input to be detected whenever the right mouse button is held down._**

  • Try this:

    using UnityEngine;
    using AC;
    
    public class DynamicDragInput : MonoBehaviour
    {
    
        public string rmbInputName = "Right mouse button";
        void Update ()
        {
            if (KickStarter.playerInput.GetDragState () == DragState.None && KickStarter.playerInput.GetMouseState () == MouseState.Normal)
            {
                bool overDraggable = KickStarter.playerInteraction.GetActiveHotspot () && KickStarter.playerInteraction.GetActiveHotspot ().GetComponentInParent<DragBase> ();
                KickStarter.playerInput.dragOverrideInput = overDraggable ? string.Empty : rmbInputName;
            }
        }
    
    }
    
  • LuxLux
    edited July 2024

    Thank you for your reply.

    **Edited

    I can now move the Draggable objects with the left mouse button but I can no longer pan the camera with either mouse button.

    Occasionally the camera will move instead when pressing and holding the left mouse button to move a Draggable object, but whether it happens or not seems erratic and random. I should mention that this same erratic behaviour was happening before I added this script, when I was interacting with the Draggables using the right mouse button (which is currently set to pan the camera).

    If I remove the dummy Hotspot from the object this unwanted behaviour does not happen. However I need the Hotspot to be present to change the cursor when over the object so that the player knows the object is interactive.

    for the record, this is what my dummy Hotspot looks like : https://imgur.com/a/CMmTBox

  • A dummy Hotspot like this should typically be placed on the same object as the Draggable - is it placed on a child because yours is a 2D game, so that a 2D collider is necessary?

    We'll need to solve these issues separately - remove the Hotspot for now, and return to the script. Is the Inspector field set to the name of the input you defined?

    Keep the GameEngine object selected in the Hierarchy, and check the Player Input component's Drag Override Input field - what is it set to when the camera cannot be panned?

  • A dummy Hotspot like this should typically be placed on the same object as the Draggable - is it placed on a child because yours is a 2D game, so that a 2D collider is necessary?

    Without your DynamicDragInput script running :

    With the Hotspot placed on the Draggable object the cursor does not change when over the Hotspot. The functionality works perfectly but the Draggable can only be moved with the right mouse button.

    With the Hotspot as child of the Draggable object the cursor does change when over the Hotspot. The Draggable can be used but the behaviour is erratic as previously described.

    With your DynamicDragInput script running :

    With the Hotspot placed on the Draggable object the cursor does not change and the Draggable can not be interacted with using either mouse button.

    With the Hotspot as a child object the Draggable can be interacted with using the left mouse button with the same erractic behaviour. The right mouse button no longer moves the camera.

    In fact, if the script is running then the Draggable is only usable when a Hotspot is attached as a child object. If the Hotspot is on the Draggable object or there is no Hotspot at all then the cursor does not change when over it and the Draggable cannot be interacted with.

    Player Input Drag Override Input field while game is running and camera can not be panned :
    https://imgur.com/a/ldh8lAb

    Name of defined input :
    https://imgur.com/a/56tyvYk

    Name in script :
    https://imgur.com/a/jzG37Ph

    In summary, if I remove the Hotspot and run the script I can't interact with the Draggable or move the camera.

  • Sorry, I didn't answer this question directly :

    A dummy Hotspot like this should typically be placed on the same object as the Draggable - is it placed on a child because yours is a 2D game, so that a 2D collider is necessary?

    It was because the cursor does not change when over the Draggable if the Hotspot is on the same object. I tried attaching it as a child and that solved the problem but introduced the erratic behaviour where the right mouse button would move the Draggable most of the time but would pan the camera some of the time.

  • edited July 2024

    Remove the Hotspot object completely for now, and use this replacement script:

    using UnityEngine;
    using AC;
    
    public class DynamicDragInput : MonoBehaviour
    {
    
        public string rmbInputName = "Right mouse button";
    
        void Update ()
        {
            bool overDraggable = KickStarter.playerInput.IsDragObjectHeld ();
    
            Vector2 mousePosition = KickStarter.playerInput.GetMousePosition ();
            Ray ray = KickStarter.CameraMain.ScreenPointToRay (mousePosition); 
            RaycastHit hit = new RaycastHit ();
    
            if (Physics.Raycast (ray, out hit, KickStarter.settingsManager.moveableRaycastLength, 1 << LayerMask.NameToLayer (KickStarter.settingsManager.hotspotLayer)))
            {
                DragBase dragBase = hit.collider.GetComponent <DragBase>();
                if (dragBase && dragBase.CanGrab ())
                {
                    overDraggable = true;
                }
            }
    
            KickStarter.playerInput.dragOverrideInput = overDraggable ? string.Empty : rmbInputName;
        }
    
    }
    

    By "Inspector field", I'm referring to the Dynamic Drag Input component you've attached to a GameObject the scene. Is "Right mouse button" the exact name of the input you've defined in your Input Manager?

  • By "Inspector field", I'm referring to the Dynamic Drag Input component you've attached to a GameObject the scene. Is "Right mouse button" the exact name of the input you've defined in your Input Manager?

    Yes it is exactly the same. This is almost working correctly now, thank you. I can move the camera with the right button and move the Draggable with the left button if only holding it for a couple of seconds. However, if I press and continue to hold the left button while moving a Draggable for about 3 or 4 seconds or more and move the mouse around it reverts back to moving the camera until I let go of it.

    I have tried this on several Draggables and it will happen every time after a few seconds if you continue to hold the button down.

  • Having experimented a bit more I have a clearer idea of the problem. When left-clicking and holding the button you can move the Draggable object, this works perfectly now.

    However, if the cursor moves outside the boundary of the sphere collider then instead of simply no longer being in control of the Draggable, the player is immediately in control of the camera, while still holding down the left mouse button.

    This means if the player is careful it works nicely but if they make larger, faster mouse movements they will suddenly be in control of the camera and it will move all over the place.

  • Gotcha. I've updated the script above - give it a try now.

  • LuxLux
    edited July 2024

    Thank you so much Chris this works perfectly now, except I still have an issue when attaching a Hotspot.

    When creating a Hotspot on the Draggable object, functionality remains perfect but the cursor does not change to show the object is usable when hovering over it.

    When attaching a Hotspot as a child of the Draggable object the camera moving bug is reintroduced but the cursor does change to show the object is usable.

    Is there a solution to this? I just want the cursor icon to change to the 'Use' icon so the player knows the Draggable can be interacted with.

    Thank you for your efforts, I really appreciate it.

    *Edit :
    I also tried leaving a Dummy Hotspot in the same location as the Draggable object (as a seperate object, not a child) and the result was the same. Perhaps there is a conflict that arises from having a 2D collider in the same space as a 3D one?

  • It sounds like a case of a 2D collider not being able to perfectly overlap a 3D one.

    As I see it, there's two ways to solve it:

    1. Have the scene override the game's 2D perspective, so that it is a 3D scene and works with 3D colliders on Hotspots (that way you can attach the Hotspot to the Draggable object directly)
    2. Disable the Hotspot while the Draggable is held. You can do this with a Hotspot: Enable or disable Action in the Draggable's "Interaction on grab / let go" ActionLists.
  • 1. Have the scene override the game's 2D perspective, so that it is a 3D scene and works with 3D colliders on Hotspots (that way you can attach the Hotspot to the Draggable object directly)

    I changed this setting in Project Settings > Editor https://imgur.com/a/RAU7Yrn

    It had no effect. Is there another setting I am unaware of? I have searched for it but I can't find anything.

    2. Disable the Hotspot while the Draggable is held. You can do this with a Hotspot: Enable or disable Action in the Draggable's "Interaction on grab / let go" ActionLists.

    I added an ActionList to the object Draggable > Interactions > Interaction On Move. This causes the ActionList to execute several times per second whether the Draggable is being interacted with or not. It begins doing this as soon as the scene starts. An object with the name of the ActionList appears and dissapears in the Hierarchy several times per second.

    As a result the Draggable cannot be interacted with at all.

    https://imgur.com/a/14vBqXV

  • It had no effect. Is there another setting I am unaware of?

    I'm referring to the Scene Manager's Override camera perspective? option that appears before you opt to organise the scene objects.

    This'll disappear once a scene has been organised, but you can make the change manually by locating the Scene Settings component in the scene's GameEngine object, checking Override Camera Perspective, and then setting Camera Perspective to Three D.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.