- Home /
Question by
naapperas · Apr 01, 2017 at 07:16 AM ·
transform.positiontransform.rotation
Ghost like feature
I've seen in games this feature where the player would move and after a certain condition was met a ghost would appear, and would repeat the exact same movement the player did earlier. I thought that the following code would work as i expected but it doesnt (I think it is due to the fact that I add the position and rotation of the player on the wrong loop)
Here's the code:
using UnityEngine;
using System.Collections.Generic;
public class Player_Controller : MonoBehaviour {
public Rigidbody rb;
private List<Vector3> posList = new List<Vector3>();
private List<Quaternion> rotList = new List<Quaternion>();
public GameObject ghostPrefab;
public bool canMove = true;
private const int speed = 500;
// Update is called once per frame
void FixedUpdate () {
if (!canMove) return;
if (Input.GetKey("w"))
{
rb.AddForce(0f, 0f, speed * Time.deltaTime);
}
if (Input.GetKey("a"))
{
rb.AddForce(-speed * Time.deltaTime, 0f, 0f);
}
if (Input.GetKey("s"))
{
rb.AddForce(0f, 0f, -speed * Time.deltaTime);
}
if (Input.GetKey("d"))
{
rb.AddForce(speed * Time.deltaTime, 0f, 0f);
}
posList.Add(transform.position);
rotList.Add(transform.rotation);
}
//this is suposed to make a "ghost" appear and make it so he replicates the player's movement
public void playGhost()
{
canMove = false;
GameObject go = (GameObject)Instantiate(ghostPrefab, posList[0], rotList[0]);
for (int i = 0; i < posList.Count; i++)
{
go.transform.position = posList[i];
go.transform.rotation = rotList[i];
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Resetting Ball Object to the original parent and position 2 Answers
Align Parent object using child object as point of reference 3 Answers
The instantiated prefab doesn't fire correctly from the player 2 Answers
Coroutine - transform for every frame for duration? 0 Answers
trying to take my door script and apply to a treasure chest 0 Answers