Scrollwheel changes the distance of the gameobject

So I’m making a kind of a click and dragging type of game and I was just wondering how I’d make the distance from the camera to the gameobject depend on mouse scrolling ?

using System.Collections;
using UnityEngine;

public class mouseDrag : MonoBehaviour
{

    float distance = 10;

    void OnMouseDrag()
    {
        Vector3 mousePosition = new Vector3(Input.mousePosition.x, Input.mousePosition.y, distance);
        Vector3 objPosition = Camera.main.ScreenToWorldPoint(mousePosition);

        transform.position = objPosition;
    }
}

this is the current code… I think I have to do something in the…

float distance = (how much you scroll) but I have no idea how to do it…

any help would be appreciated :slight_smile:

also… how would I stop the gameobject from clipping through the floor and everything while dragging ?

First one is easy: use Input.GetAxis(“Mouse ScrollWheel”) to modify distance.

The second one is a little trickier, and there are a couple different approaches. The most straightforward approach, which assumes that your object has a rigidbody, is basically just setting velocity so that the object moves towards “objPosition” rather than directly setting it:

Rigidbody myRB = GetComponent<Rigidbody>();

//figure out objPosition first

Vector3 targetMovement = objPosition - transform.position;
myRB.velocity = targetMovement * speed; //where "speed" is a new public variable

This will change the “look” so that it’s more “smooth” in its movement towards the point, which may or may not be something you want. A high “speed” value above will reduce that effect, at the possible cost of inaccuracy (occasionally clipping through something).

You will probably also want to set myRB.freezeRotation to true while holding the object, otherwise anytime it hits an obstacle it’s gonna start spinning.

I’m sorry but… how would I implement the Input.GetAxis(“Mouse ScrollWheel”);

?..
just putting it at the end of float distance = … wouldn’t work right ?

You’ll add it distance inside update.

distance = distance + Input.GetAxis("Mouse ScrollWheel");

You will probably want to multiply it by something so you can change the speed with which it moves.

Took me 10 minutes to figure out but I finally got it thanks to you…

Thanks alot :smile:

Now I’ll work on the clipping :slight_smile:

one last thing about… this… I got everything working really well except… when I click on the actual object it “starts” at the set position… in my case it’s

float distance = 5

how would I make it so that it’d get the current position of the actual object and it would start at that exact position ?

if it helps here’s the full current code:

using UnityEngine;

public class mouseDrag : MonoBehaviour
{
    public float speed = 1f;

    float distance = 5
    void Update()
    {
        distance = distance + Input.GetAxis("Mouse ScrollWheel");

        Rigidbody myRB = GetComponent<Rigidbody>();
        myRB.freezeRotation = true;
    }
    void OnMouseDrag()
    {

        Rigidbody myRB = GetComponent<Rigidbody>();

        Vector3 mousePosition = new Vector3(Input.mousePosition.x, Input.mousePosition.y, distance);
        Vector3 objPosition = Camera.main.ScreenToWorldPoint(mousePosition);
        transform.position = objPosition;

        Vector3 targetMovement = objPosition - transform.position;
        myRB.velocity = targetMovement * speed;
       
    }
}

it probably looks pretty bad :confused:
just started programming so I’m still getting used to it

lines 12 and 18… make “myRB” a class variable (i.e. like speed and distance at the top of the class) and set it in Awake() (ie the same as line 12/18 just without the type declaration), not every frame and every mousedrag event…

I’m a little confused about what it is you’re trying to achieve. At the moment the code checks for the scrollwheel and only applies it during the drag… did you want the “zoom” and the drag separate? in which case you’ll need to update the position.z in update based on the scrollwheel/distance and the position.x and .y in the drag. You can “pass through” values using something like

// keep the current x and set the y and z to 10
myVector = new Vector3(transform.position.x, 10f, 10f);
transform.position = myVector;

(don’t copy this, it’s an arbitary example of a “pass through”)

I changed it now… nothing’s in update now…

using UnityEngine;

public class mouseDrag : MonoBehaviour
{
    public float speed = 1f;

    float distance = 5;
    void OnMouseDrag()
    {
        distance = distance + Input.GetAxis("Mouse ScrollWheel");
        Rigidbody myRB = GetComponent<Rigidbody>();

        Vector3 mousePosition = new Vector3(Input.mousePosition.x, Input.mousePosition.y, distance);
        Vector3 objPosition = Camera.main.ScreenToWorldPoint(mousePosition);
        transform.position = objPosition;

        Vector3 targetMovement = objPosition - transform.position;
        myRB.velocity = targetMovement * speed;
        myRB.freezeRotation = true;
      
    }
}

I’m trying to make a drag and drop objects type of game…
currently still polishing out the dragging and dropping though… start with making the base of the game right ? :slight_smile:

so basically… I’m trying to get rid of the object clipping through stuff and I’d like to make it so the initial distance isn’t a number but the objects current position (position when you start dragging it around)