- Home /
Is it possible to continue a method after Input?
I'm working on a chess game, the base foundation is set up, however I've been trying to create animations. I at first set out to use the animation window, but I couldn't seem to find out how to call upon transform positions set in the code in the animation editor for each keyframe. So I moved away from the in-engine animation to use transform.translate with lerps. I had used lerps before and they work perefectly, as I'd be able to set the variables wherever and whenever I wanted, with an easy means to change. However, I'm trying to design the game to have it so a single button press will cause the object to go through its entire translate lerp until its new position is met.
However, I know that if you call an Input.GetMouseButtonDown(0) in the update function will only cause it to run the lerp in a single frame, causing it to only move slightly forward. This could easily be fixed by changing to Input.GetMouseButton obviously as it would continue to translate no matter what, but by design that's not what the game should offer. I've tried creating loops in and out of the update function to make sure the lerp fully executes, but that would just cause the game object to act like I was calling transform.position, rather than transform.Translate, executing the entire move in a single frame.
I'm asking for help not just as to how I could possibly have a method continue on after a single button press is called, but if it is at all possible to create specific transform positions in C# to then be placed in individual keyframes in the animation window.
Answer by Blooper64 · Sep 23, 2018 at 05:17 PM
Okay try this
in your button input put bool BeginMove = true;
in update do
if(BeginMove){
//do slurp here
float DistanceTospace = Vector3.Distance(chesspice position, chessSpace position);
if(DicstanceTospace < 0.2f){
hesspice position = chessSpace position
BeginMove= false;
}
}
Oh thank you thank you thank you! It worked! While I was using boolean variables to check to see if the piece was moving, I never thought to use Vector3.Distance to check the distance between the two.
Answer by LCStark · Sep 23, 2018 at 05:24 PM
Have a boolean field called something like chessPieceMoving
. When you detect Input.GetMouseButtonDown
, set that field to true
. In your Update
, if that field is true
, lerp your movement until the animation is complete (and once it is, set the field value to false
).
Your answer
Follow this Question
Related Questions
code using lerp with input 2 Answers
Problem of creating smooth animations 3 Answers
Learning to move 1 Answer
Making an object move smoothly without using Update 2 Answers
Ellipse Animation by code 2 Answers