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 rodrigodebem7 · Nov 03, 2019 at 06:01 PM · visual studiocharacter movementrpg-game

Player doesn't move after changing his position

Hi guys

I'm currently working on a tactical RPG and I have the following issue:

When the player's health is <=0 , I set him to a specific position in the grid of the game. However, after that, when I click on the player to move him to a specific tile, he doesn't move.

I'm using this script to change the player's positon. It's attached to each player in the game:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Jogador : MonoBehaviour
 {
     [SerializeField] private Mapa mapa;
 
     public Vector2Int GridPosition;
 
     public bool isPlayer = false;
 
     public float Health = 50f;
 
     public GameObject Spawnador;
 
 
     private void Awake()
     {
         if (!mapa) mapa = FindObjectOfType<Mapa>();
     }
     // Start is called before the first frame update
     void Start()
     {
         foreach(var tile in mapa.GridMatriz)
         {
             //tile.jogador = this;
         }
 
     }
 
     private void Update()
     {
         if (Input.GetMouseButtonDown(0))
         {
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             RaycastHit hit;
             if(Physics.Raycast(ray, out hit) && hit.transform == this.transform)
             {
                 mapa.GetComponent<Movimentação>().TurnEnd();
                 isPlayer = true;
                 mapa.GetComponent<Movimentação>().TurnStart();
                 Debug.Log("clicou");
                
             }
             
         }
 
         CheckHealth();
     }
 
 
     public void CheckHealth()
     {
         if (Health <= 0)
         {
             this.transform.position = new Vector3(Spawnador.transform.position.x, Spawnador.transform.position.y, Spawnador.transform.position.z);
             
         }
     }
 }

I'm using this script for the movement:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.AI;
 public class Movimentação : MonoBehaviour
 {
     //player variables
     public GameObject player;
     public GameObject[] Personagens;
 
     //moving variables
     Vector3 targetPosition;
     float posY = 1;
     public float velocity = 0.2f;
     public float movMax = 3;
     public bool ismoving = false;
     public bool moveEnabled;
     public int aux;
 
     //bullet variables
     public GameObject projetil;
     private GameObject SpawBala;
     public float ProjetilVeloc = 500f;
 
     private void Start()
     {
         //sets the first unit as the active unit at the start of the game
         Personagens[0].GetComponent<Jogador>().isPlayer = true;
         TurnStart();
 
     }
 
     // Update is called once per frame
     void Update()
     {
         //left mouse button to start movement
         if (Input.GetMouseButtonDown(0))
         {
             //raycast checks and returns an object, if it's a tile, the unit moves
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             RaycastHit hit;
             ismoving = true;
 
             if (Physics.Raycast(ray, out hit))
             {
                 if (hit.transform.tag == "Tile")
                 {
                     //checks if the tile is available based on the max movement of the unit
                     Tile tileAux = hit.transform.gameObject.GetComponent<Tile>();
                     Jogador scriptJog = player.GetComponent<Jogador>();
                     if ((tileAux.TilePostion.x - scriptJog.GridPosition.x) + (tileAux.TilePostion.y - scriptJog.GridPosition.y) >= -movMax && (tileAux.TilePostion.x - scriptJog.GridPosition.x) + (tileAux.TilePostion.y - scriptJog.GridPosition.y) <= movMax)
                     {
                         if ((tileAux.TilePostion.x - scriptJog.GridPosition.x) - (tileAux.TilePostion.y - scriptJog.GridPosition.y) >= -movMax && (tileAux.TilePostion.x - scriptJog.GridPosition.x) - (tileAux.TilePostion.y - scriptJog.GridPosition.y) <= movMax)
                         {
                             targetPosition = (hit.transform.position);
                             targetPosition.y = posY;
                             moveEnabled = true;
                         }
 
                     }
                 }
             }
 
         }
 
         //right click to shoot
         if (Input.GetMouseButtonDown(1))
         {
             //raycast checks and returns an object, if it's a tile, the unit shoots
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             RaycastHit hit;
 
             if (Physics.Raycast(ray, out hit))
             {
                 //checks if the tile is available based on the line and column of the unit
                 Tile tileAux = hit.transform.gameObject.GetComponent<Tile>();
                 Jogador scriptJog = player.GetComponent<Jogador>();
 
                 if (tileAux.TilePostion.x == scriptJog.GridPosition.x || tileAux.TilePostion.y == scriptJog.GridPosition.y)
                 {
                     if (tileAux.TilePostion.x > scriptJog.GridPosition.x)
                         tileAux.TilePostion.x = 5;
                     else
                         tileAux.TilePostion.x = 0;
 
                     if (tileAux.TilePostion.y > scriptJog.GridPosition.y)
                         tileAux.TilePostion.y = 5;
                     else
                         tileAux.TilePostion.y = 0;
 
                     Debug.Log(tileAux.TilePostion.x);
                     Debug.Log(tileAux.TilePostion.y);
 
                     //instantiates the bullet
                     GameObject tiro = Instantiate(projetil, SpawBala.transform.position, SpawBala.transform.rotation);
                     Rigidbody BalaRigid = tiro.GetComponent<Rigidbody>();
 
                     if (SpawBala.tag == "Bala1")
                     {
                         BalaRigid.AddForce(Vector3.forward * ProjetilVeloc);
 
                     }
 
                     if (SpawBala.tag == "Bala2")
                     {
                         BalaRigid.AddForce(Vector3.back * ProjetilVeloc);
                     }
                 }
             }
         }
 
         //player moves until reaches the position
         if (player.transform.position != targetPosition)
         {
             player.transform.position = Vector3.MoveTowards(player.transform.position, targetPosition, velocity);
             if (player.transform.position == targetPosition)
                 ismoving = false;
         }
 
         //if player reaches position, it's deselected
         if (moveEnabled && !ismoving)
         {
             player.GetComponent<Jogador>().isPlayer = false;
             moveEnabled = false;
         }
     }
 
     public void TurnStart()
     {
         //makes the selected unit the active unit
         for (int i = 0; i < Personagens.Length; i++)
         {
             if (Personagens[i].GetComponent<Jogador>().isPlayer == true)
             {
                 player = Personagens[i];
                 posY = player.transform.position.y;
                 targetPosition = player.transform.position;
                 SpawBala = player.transform.GetChild(0).gameObject;
             }
         }
     }
 
     public void TurnEnd()
     {
         //desactivates all units
         for (int i = 0; i < Personagens.Length; i++)
         {
             Personagens[i].GetComponent<Jogador>().isPlayer = false;
         }
     }
 }


I really don't know what's going on

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 BoraHorzaGobuchul · Nov 04, 2019 at 05:37 AM

@rodrigodebem7 I can only give some general advice. With a specific intractable problem that can't be solved, it's best to step back and look at the bigger picture and apply general programming techniques e.g.: 1) Can you be sure that all the required code is being called by Unity with all the expected values ? Debug.Log everything at the entry point for each of the methods called by Unity - before any custom code. 2) Methods should be no more than 10 lines long. The Update method is doing a lot of specific tasks. These tasks need to be clearly defined and moved into their own private methods or classes. One technique is wherever you have a comment, that comment is a potential name for the method e.g. //checks if the tile is available based on the line and column of the unit => bool IsTileAvailableForLineAndColumnOfTheUnit() 3) Separate your custom logic from Unity and place into their own methods (or better, separate classes). Because the custom logic is now outside of Unity, it is easier to validate and test without actually running the game.

Have a look at this: https://www.slideshare.net/dhelper/writing-clean-code-in-c-and-net

Comment
Add comment · 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

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

121 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

Related Questions

Error CS0029 1 Answer

Convert vector2int to vector3 1 Answer

2D Character Animation - even starting if I'm not pressing A or D 2 Answers

Red lines or errors on Scrollbar in Visual Studio ,Red Lines on Visual Studio scrollbar even though there's no error yet 0 Answers

Are switch -expressions- compatible with Unity? 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