C# - speed issues with node movement
Hola fellas!
I've been looking for topics and solutions here and on the StackOverflow website, I didn't find something similar to my issue and am struggling since days so please don't score this topic down for free. Also, I started Unity + coding since a month and because I'm really active I already progressed a lot, but I still lack of skill. Thank you in advance!
I'm struggling with the movement of my player. Even if it follows its paths to my mouse clicks, I encounter 3 issues and I don't find the solutions.
Issue 1: no speed control.
private float speed = 1;
transform.position = Vector3.MoveTowards(currentPos, target, speed * Time.deltaTime);
This is what I generally use but for this project I use a "node to node" moving code. For now, without speed control my player moves really quickly. The only way to slow it down is to keep my gizmos drawn for slowing my CPU which means if someone is playing my scene with an old computer, he will move as quickly than a snail. But this is an other issue.
Issue 2: double speed when the mouse button is held down.
I think that may come from the Update function. Because I have no speed control but a "node to node" moving code, updating it every frame make it saying to my Player "come on Bro, you still here? Move on!". But the movement script I shared before and works in an other project still have this double speed issue too, so I'm a little bit lost.
Issue 3: if not inside my Pathfinding script, this code get an error on click while playing: NullReferenceException: Object reference not set to an instance of an object WorldMove.Update() (at Assets/Scripts/Worldmaps/WorldMove.cs:24)
I have to attach my player to the Pathfinding script which is attached to my Pathfinding gameObj with a public Transform Player
. It's not a big issue for now, but if I want units to move randomly or chase my player later, I'll need to separate this code from the Pathfinding script...
Here is the code:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class WorldMove : MonoBehaviour
{
static WorldMove instance;
private Pathfinding pathfinding;
private Gridd grid;
void Awake()
{
instance = this;
pathfinding = GetComponent<Pathfinding>();
grid = GetComponent<Gridd>();
}
private void Update()
{
if(Input.GetMouseButton(0))
{
Vector3 currentPos = transform.position;
Vector3 target = GetMouseWorldPosition();
Node targetNode = grid.NodeFromWorldPoint(target);
if(targetNode.walkable)
{
pathfinding.FindPath(currentPos, target);
if(grid.path != null)
{
StartCoroutine(UpdatePosition(currentPos, grid.path[0], 0));
}
}
return;
}
}
private IEnumerator UpdatePosition(Vector3 anyPos, Node n, int index)
{
Vector3 nodeWaypoint = new Vector3(n.worldPosition.x, n.worldPosition.y, n.worldPosition.z);
yield return null;
transform.position = nodeWaypoint;
index++;
if(index < grid.path.Count)
{
StartCoroutine(UpdatePosition(nodeWaypoint, grid.path[index], index));
}
}
private static Vector3 GetMouseWorldPosition()
{
Vector3 clickPosition = new Vector3();
clickPosition.y = 0;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if(Physics.Raycast(ray, out hit))
{
clickPosition = hit.point;
}
return clickPosition;
}
}