Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by TeA134679 · Nov 29, 2021 at 05:32 PM · listeventsrecording

How to record moves and then play them frame by frame after a button is clicked?

I'm trying to record the functions that have been called, and play them frame by frame after a button is clicked.

For example, I have four buttons here (the triangles) to control my player's movements.

alt text

 public void MoveRight() {

     float x = playerRb.transform.position.x;
     float y = playerRb.transform.position.y;
     playerRb.transform.position = new Vector3(x + 1, y, playerRb.transform.position.z);
 }

 public void MoveLeft() {

     float x = playerRb.transform.position.x;
     float y = playerRb.transform.position.y;
     playerRb.transform.position = new Vector3(x - 1, y, playerRb.transform.position.z);
 }

 public void MoveUp() {

     float x = playerRb.transform.position.x;
     float y = playerRb.transform.position.y;
     playerRb.transform.position = new Vector3(x, y + 1, playerRb.transform.position.z);
 }

 public void MoveDown() {

     float x = playerRb.transform.position.x;
     float y = playerRb.transform.position.y;
     playerRb.transform.position = new Vector3(x, y - 1, playerRb.transform.position.z);
 }

Is there a way to record the order of buttons that I have clicked, and then call those functions after the confirm button is clicked?

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image TeA134679 · Nov 29, 2021 at 11:47 PM 0
Share

help pleassee

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by AaronBacon · Nov 30, 2021 at 12:20 AM

Simple way is probably just to keep a list of all the past moves that gets added to each time the player moves, then read that list out (here I'm using a list of strings, but no reason it couldn't be a number or Enum)

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class MoveScript : MonoBehaviour
 {
     public Rigidbody playerRb;
     
     public List<string> moves = new List<string>(){};
     
     
     public void MoveRight() 
     {
         float x = playerRb.transform.position.x;
         float y = playerRb.transform.position.y;
         playerRb.transform.position = new Vector3(x + 1, y, playerRb.transform.position.z);
         moves.Add("R");
     }
     
     public void MoveLeft() 
     {
         float x = playerRb.transform.position.x;
         float y = playerRb.transform.position.y;
         playerRb.transform.position = new Vector3(x - 1, y, playerRb.transform.position.z);
         moves.Add("L");
     }
     public void MoveUp() 
     {
         float x = playerRb.transform.position.x;
         float y = playerRb.transform.position.y;
         playerRb.transform.position = new Vector3(x, y + 1, playerRb.transform.position.z);
         moves.Add("U");
     }
     
     public void MoveDown() 
     {
         float x = playerRb.transform.position.x;
         float y = playerRb.transform.position.y;
         playerRb.transform.position = new Vector3(x, y - 1, playerRb.transform.position.z);
         moves.Add("D");
     }
 }
 

⠀

Then once the player hits Play, just go through each item in the list and play it back, probably using a Coroutine so that you can wait between moves. (Here I've also added a boolean that makes sure the moves aren't added in "Playback mode"). In theory you'd just need the button to run "PlayButton" on press, though that will depend on your implementation.

⠀

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class MoveScript : MonoBehaviour
 {
     public Rigidbody playerRb;
     
     public List<string> moves = new List<string>(){};
     public bool playbackMode = false;
     
     
     public void MoveRight() 
     {
         float x = playerRb.transform.position.x;
         float y = playerRb.transform.position.y;
         playerRb.transform.position = new Vector3(x + 1, y, playerRb.transform.position.z);
         if(!playbackMode) moves.Add("R");
     }
     
     public void MoveLeft() 
     {
         float x = playerRb.transform.position.x;
         float y = playerRb.transform.position.y;
         playerRb.transform.position = new Vector3(x - 1, y, playerRb.transform.position.z);
         if(!playbackMode) moves.Add("L");
     }
     public void MoveUp() 
     {
         float x = playerRb.transform.position.x;
         float y = playerRb.transform.position.y;
         playerRb.transform.position = new Vector3(x, y + 1, playerRb.transform.position.z);
         if(!playbackMode) moves.Add("U");
     }
     
     public void MoveDown() 
     {
         float x = playerRb.transform.position.x;
         float y = playerRb.transform.position.y;
         playerRb.transform.position = new Vector3(x, y - 1, playerRb.transform.position.z);
         if(!playbackMode) moves.Add("D");
     }
     
     public void PlayButton()
     {
         StartCoroutine(PlayMoves());
     }
     
     IEnumerator PlayMoves()
     {
         playbackMode = true;
         foreach (string moveItem in moves)
         {
             if(moveItem == "L") MoveLeft();
             else if(moveItem == "R") MoveRight();
             else if(moveItem == "U") MoveUp();
             else if(moveItem == "D") MoveDown();
             
             yield return new WaitForSeconds(0.5f);
         }
         playbackMode = false;
     }
     
 }
 

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image TeA134679 · Nov 30, 2021 at 12:27 AM 0
Share

thank you so much! this is very helpful

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

136 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

Where can I find complete list of events? 1 Answer

Creating a record of values generated with Random.Range 1 Answer

Scripting and Coding Dictionary 0 Answers

[NGUI] How can I drag item from UIlist and insert to new position 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges