Question by
marcobucciacchio · Apr 09, 2021 at 09:22 PM ·
movementnodes
Can anyone help with my character stutter during node movement?
I've been making a Pacman clone in 3D and I'm using nodes to move Pacman and the ghosts around the level and so far everything works as it should moving between nodes. The only issue is that the player and ghosts stutter for a frame after reaching each node but then continues on as it should. this is the entire code for my Pacman movement, I suspect the issue is with "OverShotNode()."
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
public class TestMovement : MonoBehaviourPun
{
[SerializeField]
float Speed = 3.5f;
[SerializeField]
float boostTimer;
bool boosting = false;
[SerializeField]
float respawnDuration = 3.0f;
private Node currentNode ,previousNode, targetNode;
public Animator animator;
private AudioSource wakaSound;
private AudioSource powerUpSound;
private AudioSource energize;
private AudioSource deathSound;
private Score score;
private Vector3 direction = Vector3.zero;
private Vector3 nextDirection;
private void Start()
{
animator = GetComponent<Animator>();
Node node = GetNodeAtPosition(transform.localPosition);
if (node != null)
{
currentNode = node;
}
direction = Vector3.zero;
ChangePosition(direction);
score = GameObject.FindGameObjectWithTag("Score").GetComponent<Score>();
AudioSource[] audio = GetComponents<AudioSource>();
wakaSound = audio[2];
powerUpSound = audio[1];
deathSound = audio[0];
energize = audio[3];
}
void Update()
{
CheckInput();
Move();
UpdateOrientation();
UpdateAnimationState();
if (boosting)
{
boostTimer += Time.deltaTime;
if (boostTimer >= 7)
{
Speed = 3.5f;
boostTimer = 0;
boosting = false;
}
else
{
Speed = 4.5f;
}
}
}
void UpdateAnimationState()
{
if(direction == Vector3.zero)
{
GetComponent<Animator>().enabled = false;
}
else
{
GetComponent<Animator>().enabled = true;
}
}
Node GetNodeAtPosition(Vector3 pos)
{
GameObject tile = GameObject.Find("Game").GetComponent<GameBoard>().nodePos[(int)pos.x, (int)pos.z];
if(tile != null)
{
return tile.GetComponent<Node>();
}
return null;
}
void CheckInput()
{
if (Input.GetKeyDown(KeyCode.LeftArrow) || Input.GetKeyDown(KeyCode.A))
{
ChangePosition(Vector3.left);
}
else if (Input.GetKeyDown(KeyCode.RightArrow) || Input.GetKeyDown(KeyCode.D))
{
ChangePosition(Vector3.right);
}
else if (Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.W))
{
ChangePosition(Vector3.forward);
}
else if (Input.GetKeyDown(KeyCode.DownArrow)|| Input.GetKeyDown(KeyCode.S))
{
ChangePosition(Vector3.back);
}
}
void ChangePosition(Vector3 d)
{
if (d != direction)
nextDirection = d;
if(currentNode != null)
{
Node moveToNode = CanMove(d);
if(moveToNode != null)
{
direction = d;
targetNode = moveToNode;
previousNode = currentNode;
currentNode = null;
}
}
}
void Move()
{
if(targetNode != currentNode && targetNode != null)
{
if(nextDirection == direction * -1)
{
direction *= -1;
Node temp = targetNode;
targetNode = previousNode;
previousNode = temp;
}
if (OverShotTarget())
{
currentNode = targetNode;
transform.localPosition = currentNode.transform.position;
GameObject otherPortal = GetPortal(currentNode.transform.position);
if(otherPortal != null)
{
transform.localPosition = otherPortal.transform.position;
currentNode = otherPortal.GetComponent<Node>();
}
Node moveToNode = CanMove(nextDirection);
if (moveToNode != null)
direction = nextDirection;
if (moveToNode == null)
moveToNode = CanMove(direction);
if (moveToNode != null)
{
targetNode = moveToNode;
previousNode = currentNode;
currentNode = null;
}else{
direction = Vector3.zero;
}
}else{
transform.localPosition += (direction * Speed) * Time.deltaTime;
}
}
}
void MoveToNode(Vector3 d)
{
Node moveToNode = CanMove(d);
if(moveToNode != null)
{
transform.localPosition = moveToNode.transform.position;
currentNode = moveToNode;
}
}
void UpdateOrientation()
{
// if (base.photonView.IsMine)
{
if (direction == Vector3.left)
{
transform.localScale = new Vector3(.75f, .75f, .75f);
transform.localRotation = Quaternion.Euler(180, 90, 90);
}
else if (direction == Vector3.right)
{
transform.localScale = new Vector3(.75f, .75f, .75f);
transform.localRotation = Quaternion.Euler(0, 90, 90);
}
else if (direction == Vector3.forward)
{
transform.localScale = new Vector3(.75f, .75f, .75f);
transform.localRotation = Quaternion.Euler(0, 0, 90);
}
else if (direction == Vector3.back)
{
transform.localScale = new Vector3(.75f, .75f, .75f);
transform.localRotation = Quaternion.Euler(0, 180, 90);
}
}
}
Node CanMove(Vector3 d)
{
Node moveToNode = null;
for (int i = 0; i < currentNode.neighbours.Length; i++)
{
if(currentNode.validDirections[i] == d)
{
moveToNode = currentNode.neighbours[i];
break;
}
}
return moveToNode;
}
public void EatPellet()
{
if (base.photonView.IsMine)
{
score.IncrementScore();
wakaSound.Play();
}
else
{
score.IncrementOpponentScore();
}
}
public void EatSuperPellet()
{
if (base.photonView.IsMine)
{
score.IncrementScore();
boosting = true;
powerUpSound.Play();
energize.Play();
energize.SetScheduledEndTime(AudioSettings.dspTime + (7f));
}
else
{
score.IncrementOpponentScore();
}
}
bool OverShotTarget()
{
float nodeToTarget = LengthFromNode(targetNode.transform.position);
float nodeToSelf = LengthFromNode(transform.position);
return nodeToSelf > nodeToTarget;
}
float LengthFromNode(Vector3 targetPosition)
{
Vector3 vec = targetPosition - previousNode.transform.position;
return vec.sqrMagnitude;
}
GameObject GetPortal(Vector3 pos)
{
GameObject tile = GameObject.Find("Game").GetComponent<GameBoard>().nodePos[(int)pos.x, (int)pos.z];
if(tile != null)
{
if (tile.GetComponent<Tile>() != null)
{
if (tile.GetComponent<Tile>().isPortal)
{
GameObject otherPortal = tile.GetComponent<Tile>().portalReciever;
return otherPortal;
}
}
}
return null;
}
}
Comment