I’m working on drawing path game that the player draw path for the character and the character follow the path
the problem is the path isn’t being drawn correctly because I Instantiate list of points
first I need to correct the nodes shape
and the don’t walk correctly on the line because the velocity is linear it moves from the first point to the final point directly
I need it to trace all the points
this is my code.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.SceneManagement;
public class FollowPath : MonoBehaviour
{
public Vector2 target;
public float speed;
float tr;
int score = 0;
public Animator anime;
public GameObject node;
public List<GameObject> drawnNodes;
public Rigidbody2D rb;
private Vector2 currentNodePosition;
public static bool moving = false;
public int drawnNodesLength = 0;
public float moveSpeed;
public static bool startMoving = false;
private Vector2 dir;
// Use this for initialization
void Start()
{
rb = GetComponent<Rigidbody2D>();
moving = false;
startMoving = false;
anime = GetComponent<Animator>();
drawnNodes = new List<GameObject>();
}
// Update is called once per frame
void Update()
{
if (moving)
{
anime.SetInteger("State", 1);
}
else
{
anime.SetInteger("State", 0);
}
}
// Drawing the path when the screen touched
void OnMouseDrag()
{
{
StartCoroutine("DrawLine");
}
}
// Drawing the line
public IEnumerator DrawLine()
{
Vector2 nodePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
if (!Node.isHitted)
{
drawnNodes.Add(Instantiate(node, nodePosition, Quaternion.identity) as GameObject);
currentNodePosition = nodePosition;
}
yield return new WaitForSeconds(.5f);
Vector2 playerpos = transform.position;
dir = currentNodePosition - playerpos;
rb.velocity = dir * moveSpeed;
moving = true;
startMoving = true;
yield return new WaitForSeconds(.21f);
if (moving)
{
if (drawnNodesLength >= 0 && drawnNodesLength < drawnNodes.Count)
{
Destroy(drawnNodes[drawnNodesLength]);
drawnNodesLength++;
}
}
MovePlayer();
}
// Make the player move around the nodes of the path
public void MovePlayer()
{
moving = true;
{
if (currentNodePosition.x < transform.position.x)
{
transform.rotation = Quaternion.Euler(0, 180, 0);
}
if (currentNodePosition.x > transform.position.x)
{
transform.rotation = Quaternion.Euler(0, 0, 0);
}
}
if (drawnNodes.Count == drawnNodesLength)
{
moving = false;
rb.velocity = Vector3.zero;
rb.angularVelocity = 0;
drawnNodesLength = 0;
drawnNodes.Clear();
}
}
// When the player hits anything
void OnCollisionEnter2D(Collision2D hit)
{
if (hit.gameObject.tag == "Finish")
{
if (score >= 3)
{
Invoke("LoadNextLevel", 2);
gameObject.SetActive(false);
}
}
if (hit.gameObject.tag == "Enemy")
{
SceneManager.LoadScene("demo");
}
}
void OnTriggerEnter2D(Collider2D hit)
{
if (hit.gameObject.tag == "Obstacles")
{
drawnNodesLength = drawnNodes.Count;
foreach (GameObject g in drawnNodes)
{
Destroy(g);
}
moving = false;
rb.velocity = Vector2.zero;
rb.angularVelocity = 0;
drawnNodes.Clear();
}
if (hit.gameObject.tag == "Coins ")
{
score++;
Debug.Log(score);
}
}
private void LoadNextLevel()
{
SceneManager.LoadScene("Movie");
}
}