- Home /
nonlinear rigidbody velocity
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");
}
}
What is your question?? And please check your formatting.
Answer by edcx · Dec 05, 2016 at 04:28 PM
So you want to have a waypoint follow system and I believe that Unity Standart Assets should have one. You can try to have one target point that goes on your line ahead of your character and your character will move towards to that target point. So your character will not care about your path. It will just follow that target.
The point that moves on your line will not care about rotation and you will just move it on your path.
To be honest, I did not understand your problem clearly but your currentnode might have set to your final point quickly. Debugging might help you about this.