0
\$\begingroup\$

I have a custom sprite class in Unity and I'm trying to make a method in where it moves from one point to another. Instead of using an Enumerator for time, I'm using real time game frames using static void methods inside of classes which inherit from a MainGame.cs script attached to the Main Camera, and this is called inside of MainGame's Update() method.

So what I have in the end are the following values: SpriteID OriginalX OriginalY MoveToX MoveToY CurrentFrames TotalFrames

I'm not asking anyone to do the work for me, just that if there's a place I could be directed to in where I could find the math necessary to solve the problem that'd be a huge help. I'm not using any vectors here, just the raw math using only those variables. I don't know if someone has found an answer for calculating it based on update frames...

public class Sprite : MainGame { 

public static int State = 0;
public static int CurrentFrames = 0;
public static int TotalFrames = 0;

 public static void Move(int SpriteID, int OriginalX, int OriginalY, int MoveToX, int MoveToY, int TotalFrames)
 {   
    switch (State) { 
    //init
       case 0: 
       CurrentFrames = 0;

       State = 1;

       break;

       //run
       case 1:
       if (CurrentFrames < TotalFrames) { 
       //do math here

          CurrentFrames++;
       }

       if (CurrentFrames >= TotalFrames) { 
          State = 2;
       }
       break;

       //end
       case 2: 
          //Reset values here
          //Shut off
          State = 3;
       break;
    }
 } 
}

EDIT 10212016 456PM: What I've figured out so far, now...

if (CurrentFrames < TotalFrames) {
    DistanceX = MoveToX - OrigX;
    MoveForwardXFloat = (float)DistanceX / (float)Frames;
    OrigX += (int)MoveForwardXFloat;
}
\$\endgroup\$

2 Answers 2

1
\$\begingroup\$

It really depends on how you have the whole setup, but according to your code I'll use the Mathf.MoveTowards for moving the object.

The calculation should be something similar to this:

// in the Update method or something similar

currentX = Mathf.MoveTowards(currentX, targetX, (distanceX/duration) * Time.deltaTime), 
currentY = Mathf.MoveTowards(currentY, targetY, (distanceY/duration) * Time.deltaTime), 

// currentX/Y = current x/y position of the sprite
// targetX/Y = target x/y position for the sprite
// distanceX/Y = distance from start position to target position
// duration = duration in seconds for the movement

You could also use Mathf.Lerp

\$\endgroup\$
1
  • \$\begingroup\$ What I found also is that I needed the currentX to be a float. \$\endgroup\$
    – Mark Aven
    Oct 22, 2016 at 1:20
0
\$\begingroup\$

This worked for me. The math is within it down there. When I figured it out it seemed like it should be more obvious, Yvelocity and Xvelocity are added to it every frame until it's arrived, and it gets the velocity based on the frame number left (frame is counting down) and the distance.

The trick that I forgot was that everything has to be in floats.

using UnityEngine; using System; using System.Collections;

public class SpriteMoveTo : MG {

    public static int State;
    private static int SpriteID;
    private static float CurrentFrames;
    private static float TotalFrames;
    private static float DestinationX;
    private static float DestinationY; 
    private static float CurrentX;
    private static float CurrentY;
    private static float DistanceX;
    private static float DistanceY;
    private static float MoveForwardXFloat;
    private static float Yvelocity;
    private static float Xvelocity;

    public static void Update (string SpriteIDstring, string DestinationXstring, string DestinationYstring, string TotalFramesString, string DoOrDontWait) {
        if (State == 0 && CurEcmd_Cmd == "SpriteMoveTo") {
            SpriteID = int.Parse (SpriteIDstring);
            TotalFrames = float.Parse (TotalFramesString);
            CurrentFrames = TotalFrames;
            DestinationX = float.Parse (DestinationXstring);
            DestinationY = float.Parse (DestinationYstring);
            CurrentX = MG_Sprites.MG_SpriteDatabase [SpriteID].X;
            CurrentY = MG_Sprites.MG_SpriteDatabase [SpriteID].Y;
            State = 1;
        }

        if (State == 1) {
            MG_Sprites.MG_SpriteDatabase [SpriteID].X = CurrentX;
            MG_Sprites.MG_SpriteDatabase [SpriteID].Y = CurrentY;

            if (CurrentFrames > 0) { 

                DistanceX = DestinationX - CurrentX;
                DistanceY = DestinationY - CurrentY;
                Xvelocity = DistanceX / CurrentFrames;
                Yvelocity = DistanceY / CurrentFrames;
                CurrentY += Yvelocity;
                CurrentX += Xvelocity;
                MG_Sprites.MG_SpriteDatabase[SpriteID].Y = CurrentY;
                MG_Sprites.MG_SpriteDatabase [SpriteID].X = CurrentX;

                CurrentFrames--;
            }

            if (CurrentFrames <= 0) {
                State = 2;
            }
        }

        if (State == 2) {
            State = 0;
            //Next Ecmds command if list isn't empty, else set it to idle
            CurEcmd_Cmd = "";
            MG_Interpreter.State = 0;
        }
    }
}
\$\endgroup\$
1
  • \$\begingroup\$ If this has solved your problem, can you mark your answer as correct? This way it won't show up in the front page, asking more people to answer it. \$\endgroup\$ Dec 10, 2018 at 15:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .