Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
avatar image
0
Question by White-Fire · May 19, 2020 at 11:07 AM · updatenpcturn-based

issues with Update() in turn-based movement

Hello!
I'm trying to make turn-based game. At this point i have few object on scene .
alt text

So i'm trying to do basic movements of player and npc. After player moves, npc also moves to player direction. Also they can't go into wall and into each other and they can move only to 4 directions.

My issue is that sometimes NPC don't "see" player changing it position. So if they move to each other, sometimes npc can go inside player. Or if they stay next to each one and player moves away, npc should follow, but some times he stay , like players is still there.
After a lot of time spend, i think the issue is in Update() , cause if i use FixeUpdate(), there no such issues. But with FixeUpdate() it's not always respond to key's pressed at once.

So any advice's how to fix this problem? Should i use Update at all or there is other possible solutions?

there is my codes:

1) GameManager :

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class GameManager : MonoBehaviour
 {
     public GameObject player, enemy;
     //private PlayerController playerController;
     private EnemyMovingToPlayer enemyMovingToPlayer;
     private PlayerController playerController;
     public static bool playerTurn = true;
     void Start()
     {
         playerController = player.GetComponent<PlayerController>();
         enemyMovingToPlayer = enemy.GetComponent<EnemyMovingToPlayer>();
     }
 
        void Update()
         {
             if (playerTurn)
             {
                // player.GetComponent<PlayerController>().PlayerMovement();
                 playerController.PlayerMovement();
             }
             else
             {
                 //enemy.GetComponent<EnemyMovingToPlayer>().EnemyAI();
                 enemyMovingToPlayer.EnemyAI();
             }
 
         }
 
 }


2) Player

 public class PlayerController : MonoBehaviour
 {
     public int moveSpeed = 1;
     private Vector3 playerDestenation;
               
    public void PlayerMovement()
     {
         if (Input.GetKeyDown(KeyCode.A))
         {
             playerDestenation = transform.position + new Vector3(-moveSpeed, 0, 0);
             transform.rotation = Quaternion.Euler(0, -90, 0);
             //transform.localEulerAngles = new Vector3(0, -90, 0);
             //if (!Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), moveSpeed))
             if (!Physics.Raycast(transform.position, transform.forward, moveSpeed))
             {
                 transform.position += new Vector3(-moveSpeed, 0, 0);               
           
             }
             if (playerDestenation == transform.position)
             {
                 GameManager.playerTurn = false;
             }
         }
 
         else if (Input.GetKeyDown(KeyCode.D))
         {
             playerDestenation = transform.position + new Vector3(moveSpeed, 0, 0);
             transform.rotation = Quaternion.Euler(0, 90, 0);
             //if (!Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), moveSpeed))
             if (!Physics.Raycast(transform.position, transform.forward, moveSpeed))
             {
                 transform.position += new Vector3(moveSpeed, 0, 0);
             }
             if (playerDestenation == transform.position)
             {
                 GameManager.playerTurn = false;
             }
         }
         else if (Input.GetKeyDown(KeyCode.W))
         {
             playerDestenation = transform.position + new Vector3(0, 0, moveSpeed);
             transform.rotation = Quaternion.Euler(0, 0, 0);
             //if (!Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), moveSpeed))
             if (!Physics.Raycast(transform.position, transform.forward, moveSpeed))
             {
                 transform.position += new Vector3(0, 0, moveSpeed);
             }
             if (playerDestenation == transform.position)
             {
                 GameManager.playerTurn = false;
             }
         }
         else if (Input.GetKeyDown(KeyCode.S))
         {
             playerDestenation = transform.position + new Vector3(0, 0, -moveSpeed);
             transform.rotation = Quaternion.Euler(0, 180, 0);
             //if (!Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), moveSpeed))
             if (!Physics.Raycast(transform.position, transform.forward, moveSpeed))
             {
                 transform.position += new Vector3(0, 0, -moveSpeed);
             }
             if (playerDestenation == transform.position)
             {
                 GameManager.playerTurn = false;
             }
         }
         else if (Input.GetKeyDown(KeyCode.Space))
         {
             GameManager.playerTurn = false;
         }
     }
 }

3) Enemy

 public class EnemyMovingToPlayer : MonoBehaviour
 {
     private Transform player;
     private float snappedValue;
 
     void Start()
     {
         // player = GameObject.Find("Player").transform;
         player = GameObject.FindWithTag("Player").transform;
     }
 
 
     public void EnemyAI()
     {
         transform.LookAt(player);
         snappedValue = Mathf.RoundToInt(transform.rotation.eulerAngles.y / 90) * 90;
         transform.rotation = Quaternion.Euler(0, snappedValue, 0);
         RaycastHit raycastHit;
         //if (!Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), 1))
         if (!Physics.Raycast(transform.position, transform.forward, out raycastHit, 1))
         {
             transform.position += transform.forward;
             transform.LookAt(player);
             snappedValue = Mathf.RoundToInt(transform.rotation.eulerAngles.y / 90) * 90;
             transform.rotation = Quaternion.Euler(0, snappedValue, 0);
             GameManager.playerTurn = true;
         }
         else
         {
             if (raycastHit.collider.transform == player)
             {
                 Debug.Log("Player got hit");
             }
             transform.LookAt(player);
             snappedValue = Mathf.RoundToInt(transform.rotation.eulerAngles.y / 90) * 90;
             transform.rotation = Quaternion.Euler(0, snappedValue, 0);
             GameManager.playerTurn = true;
         }
 
     }
 }


basic-scene.jpg (60.0 kB)
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by logicandchaos · May 19, 2020 at 02:01 PM

I would make it was more simpler, code it all in a 2d grid and then just set the objects in the correct space using the grid data. No need for physics or for gameObjects to have updates. You should have one script that has an update that manages the board and who's turn it is.

Comment
Add comment · Show 1 · Share
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
avatar image White-Fire · May 19, 2020 at 04:18 PM 0
Share

Thanks, but i would like to find solution for this problem.
Let say, this project is my own tutorial into Unity. So i wanna find way to make it work then just skip. Also it's looked like some simple stuff to do..

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

131 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

Related Questions

How to make a GUI that changes between players when it is there turn? 0 Answers

Does Event fire only once during Update()? 0 Answers

Editing color with C# script 0 Answers

Check for input in a non-Update() function? 1 Answer

Variables in GUI not updating/changing? (javascript) 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