- Home /
Taking a "Range mapped" value and using it to drive the position of an object.
I would like to remap a value to output a different set of values.
Example:
Starting range goes from 0 to 10
Output range goes from 100 to 200
Input value = 5
Output value = 150
What I am trying to do is have the spot angle of a spotlight drive the y position of the spotlight. So when the light is zoomed to 7 the position will be at .743 and when its zoomed to 48 the position will be at -.143
I found this function:
public float scale(float OldMin, float OldMax, float NewMin, float NewMax, float OldValue)
{
float OldRange = (OldMax - OldMin);
float NewRange = (NewMax - NewMin);
float NewValue = (((OldValue - OldMin) * NewRange) / OldRange) + NewMin;
return (NewValue);
}
Used by this:
float scaleMe = 0.5F;
float scaled = scale(0F, 1F, 10F, 20F, scaleMe);
This is my current code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[ExecuteInEditMode]
public class Scenius : MonoBehaviour
{
[Range(7, 48)] public float zoom;
public GameObject spotlight;
private void SetValues()
{
//Set spotlight zoom
spotlight.GetComponent<Light>().spotAngle = zoom;
//Range Mapping inputs
float scaleMe = zoom;
float scaled = scale(7F, 48f, .743f, -.143f, scaleMe);
//Map zoom to position
spotlight.transform.position = Vector3(0, NewValue, 0);
}
// Range Mapper function
public float scale(float OldMin, float OldMax, float NewMin, float NewMax, float OldValue)
{
float OldRange = (OldMax - OldMin);
float NewRange = (NewMax - NewMin);
float NewValue = (((OldValue - OldMin) * NewRange) / OldRange) + NewMin;
return (NewValue);
}
void Update()
{
SetValues();
}
}
The issue I am having currently is that I am getting an error on the 'NewValue' in the spotlight.transform.position = Vector3(0, NewValue, 0);
I dont know if I cant use this type of value in this way or if there is another way to input this value into the y position of the spotlight.
Im not good with code. Like at all. So let me know if you need anymore information or if I am an idiot and just forgot a ; somewhere.
Answer by Bunny83 · Jun 21, 2018 at 10:47 PM
Your scale method is a method that returns the new value. The "NewValue" variable is a local variable inside the scale method and can't be used outside the method. However as i said your methoer returns that value. You actually store the returned value inside the "scaled" variable. So inside your Vector3 constructor you want to use "scaled" instead of "NewValue". Also you're missing the "new" keyword in that line as well:
spotlight.transform.position = new Vector3(0, scaled, 0);
Thanks for the quick reply. I did all of the changes but now i am getting and unexpected symbol 'float' for the range mapper method on line 29
Your answer
![](https://koobas.hobune.stream/wayback/20220612165226im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
An NPC "moonwalks" 2 Answers
C# transform position 1 Answer
Multiple Cars not working 1 Answer