How to create an integer between 0 - 100 but have it change based on my gamobjects position?

I’m thinking up experiments on how to solve this problem as it’s proving pretty tricky, I’ve set up a very nice grab mechanic for a gameobject where I can drag it between two clamped points on the Y axis using the mouse and I use the empties so that this dragging effect stays the same no matter what height I place the gameobject at run time.

This all works, but now I’ve got the problem of wanting to get the gameobject to do something, in this case it’s a totem and you can drag it all the way up and all the way down. It’s easy enough getting something to happen when it drags to the maximum height or the minimum height but what I want to be able to do is to have some kind of integer change according to where the totem is. So for example if I set the totem halfway between the minimum and maximum position I want the integer to set to 50.

Does anyone have any idea how I’d be able to do this? I was wondering about converting the floats to an integer and then working off that but I don’t know. I’m thinking about an integer quite a bit because if I can have the integer change nice and accurately I’d be able to do a lot with that feature and it would be easier to work out the maths for when I want the totem change things in the game.

For reference, here’s the code I have so far.

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

public class DragTotemScript : MonoBehaviour

{
    public float dragSpeed = 0.001f;
    Vector3 lastMousePosition;

    public Transform minYEmpty;
    public Transform maxYEmpty;

    public float totemDragAmount = 0;

    public void DragTotem()

    {
        Vector3 delta = Input.mousePosition - lastMousePosition;
        Vector3 position = transform.localPosition;
        position.y += delta.y * dragSpeed;
        position.y = Mathf.Clamp(position.y, minYEmpty.transform.localPosition.y, maxYEmpty.transform.localPosition.y);
        transform.localPosition = position;
        lastMousePosition = Input.mousePosition;
        totemDragAmount = transform.localPosition.y;

    }



}

Could it be that I’m actually going about this all wrong and I should have an entirely separate integer that just changes based on the mouse position when the totem is being dragged? I just realised that now, I may experiment and see it works.

I’m actually an idiot I asked this question ages ago in April and I just need to look at the answer in detail now, I found it on the top page of google as well lol.

1 Like
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DragTotemScript : MonoBehaviour

{
    public float dragSpeed = 0.001f;
    Vector3 lastMousePosition;

    public Transform minYEmpty;
    public Transform maxYEmpty;

    public float totalDistance;
    public float currentDistance;

    public float totemDragAmount = 0;

    public void DragTotem()

    {
        Vector3 delta = Input.mousePosition - lastMousePosition;
        Vector3 position = transform.localPosition;
        position.y += delta.y * dragSpeed;
        position.y = Mathf.Clamp(position.y, minYEmpty.transform.localPosition.y, maxYEmpty.transform.localPosition.y);
        transform.localPosition = position;
        lastMousePosition = Input.mousePosition;
        totemDragAmount = transform.localPosition.y;

        totalDistance = Vector3.Distance(minYEmpty.transform.position, maxYEmpty.transform.position);
        currentDistance = Vector3.Distance(transform.position, minYEmpty.transform.position);

        var percentPassed = currentDistance / totalDistance;
        Debug.Log(percentPassed);

    }



}

Just did what was in that post and it seems to be working perfectly between 0 and 1 so now I just need to take those values and use them as a percentage properly, but I’ll need to do more research into that.

Good old Mathf.Lerp() is probably what you’re looking for to convert a 0-1 range into an arbitrary X to Y range.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.AI;

public class DragTotemScript : MonoBehaviour

{
    public float dragSpeed = 0.001f;
    Vector3 lastMousePosition;

    public Transform minYEmpty;
    public Transform maxYEmpty;

    public float totalDistance;
    public float currentDistance;

    public float totemDragAmount = 0;
    public float currentPercent;

    public Text totemPolePercentageText;

    public float currentWorshippers;
    public float worshippersSelectedFloat;


    public void DragTotem()

    {
        Vector3 delta = Input.mousePosition - lastMousePosition;
        Vector3 position = transform.localPosition;
        position.y += delta.y * dragSpeed;
        position.y = Mathf.Clamp(position.y, minYEmpty.transform.localPosition.y, maxYEmpty.transform.localPosition.y);
        transform.localPosition = position;
        lastMousePosition = Input.mousePosition;
        totemDragAmount = transform.localPosition.y;

        totalDistance = Vector3.Distance(minYEmpty.transform.position, maxYEmpty.transform.position);
        currentDistance = Vector3.Distance(transform.position, minYEmpty.transform.position);

        currentPercent = currentDistance / totalDistance * 100;
        GetVillagersList getVillagersList = GetComponentInParent<GetVillagersList>();

        currentWorshippers = currentPercent / 100 * getVillagersList.playerWorshippers.Count;
        worshippersSelectedFloat = currentWorshippers;
        int worshippersSelectedInt = (int) worshippersSelectedFloat;

        Debug.Log(worshippersSelectedInt);


        totemPolePercentageText.text = currentPercent.ToString("n0") + "%";

    }



}

I should post up the finished code that I have, now it gets the count of my worshippers correctly from a percentage.