Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
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
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

212 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Why does the FreezeRotation constraint still allow the rotation to be modified? 1 Answer

First person controls for GoogleCardboard 2 Answers

Cant Move towards Right and Jump at same time? 2 Answers

Gyro camera control with forward and backward movement 1 Answer

how to move a gameobject based on a dice roll 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges