-3
\$\begingroup\$

I have a cube which i am moving in circle shape (with horizontal key input) is below code suggested.

public class Oscillator : MonoBehaviour {
    float timeCounter = 0;

    float speed,width, height;

    public float yPosition = 30;
    // Use this for initialization
    void Start () {
        speed = 2; width = 10; height = 10;
    }

    // Update is called once per frame
    void Update () {

        timeCounter += Time.deltaTime * speed * Input.GetAxis("Horizontal");
        float x = Mathf.Sin (timeCounter)* height; 
        float y = yPosition;
        float z = Mathf.Cos (timeCounter) * width;
        transform.position = new Vector3 (x, y, z);

    }
}

Now my object is moving in circular shape which is fine. Now i want to translate my objects movement into time. Let suppose

if my object x position is 1 then it should give me time 1.0

if it is 1.5 then it should give me 1.5

it increase or decrease according to x postion of my object (or possibly throught z).

I logged my object's x position which is starting from 0 to 9.999 and then become decrease 0, then -1 to -9 then it become decrease 0 and reached to its initial position. This circular movement x values are strange for me, i am unable to form any formula that can convert my x position into time. Please can any one help me in this purely mathematics and 3d math problem?

\$\endgroup\$
  • 2
    \$\begingroup\$ You already have a timeCounter field. Isn't it what you want? \$\endgroup\$ – S. Tarık Çetin May 19 '17 at 8:55
  • \$\begingroup\$ No man, i want that is another thing. \$\endgroup\$ – Muhammad Faizan Khan May 19 '17 at 10:02
  • \$\begingroup\$ So are you wanting to know how fast your moving, or provide movement as a value of units per time? This question is unclear, and it shows no research effort. \$\endgroup\$ – Gnemlock May 19 '17 at 22:04
  • \$\begingroup\$ Are you trying to calculate the timestamp at which your object will reach a particular spot on its circular path? \$\endgroup\$ – DMGregory May 20 '17 at 15:23
  • \$\begingroup\$ Thanks for all your comments i have manage to do this using my answer. \$\endgroup\$ – Muhammad Faizan Khan May 22 '17 at 9:33
0
\$\begingroup\$

I am able to convert my range

0 to 9.999 and then become decrease 0, then -1 to -9 then it become decrease 0 and reached to its initial position

Into time using this code snippet. Thanks to this stackoverflow answer.

public float ConvertRange(float originalStart, float orignialEnd, float newStart, float newEnd, float value){
        double scale = (newEnd - newStart) / (orignialEnd - originalStart);
        return (float) (newStart + ((value - originalStart) * scale));
    }

I am calling CovertRange Method using these parameters.

hour = ConvertRange(-9.99f,9.99f, 0f, 23.99f, controlledCube.transform.position.x); 

while variables means

     int originalStart, int originalEnd, // original range, my object position of any axis
    int newStart, int newEnd, // desired range
    int value) // value to convert

Note : This is not a complete answer. It somehow changing one range to another range but partially not completely.

\$\endgroup\$

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.