A* (Astar) Pathfinding doesn't work with Player Clone / Instantiate Unity - unity3d

My player spawns at game play.
when i put my Player Prefab into "target" then it doesn't follow.
but when i put my clone(instance) of my prefab (what spawned in the hierachy when i start the game) into "target" then it works.
how can i do that the target get automatically the instance of my player prefab? i was thinking about anything with "find game object with tag" but i'm legit a c# noob, i prefer using bolt. only for the pathfinding i need to use that way.

One option is to use the function GameObject.Find. E.g. GameObject.Find("Player"); or GameObject.Find("Player(clone)");. This will find the GameObject with that name. Here is the documentation for it: https://docs.unity3d.com/ScriptReference/GameObject.Find.html

Related

Adding scripts to gameobjects at runtime with PUN

I have a coop game that I'd like to extend to online multiplayer. My game has a series of minigames, sort of like mario party but in a single level. My current code adds and removes scripts from the player objects at runtime depending on the current minigame. For example, when the Tag game loads, it automatically adds the TagPlayerScript to each player, and then removes it once the game ends.
The way I manage this is with a single GameEngine class that iterates through each player in a list.
With Photon, I can't figure out how to make this functionality work. I can instantiate a player prefab for each player, but if I add a script during runtime it only does so on one player's version of the game. How can I make it update on every player's game?

Unity: My enemy prefab can't find the player on the scene

So my problem is, that I have a GameObject, which spawns enemies, and it has a variable, where I can put my enemy prefab. My only problem is, that my enemy doesn't follow the player.
I made an enemy prefab, with the A* path-finding algorithm, I included the AIDestinationSetter also. The A* works just fine when my enemy is in the scene, but when I try to spawn it, it somehow doesn't seem to know what to do. Any ideas what is wrong?
Thanks for all help, much appreciated!
If the problem is setting the target as the player prefab instead of the player in the scene, add a tag to the player and in the script, get the reference of the player in the scene using
target = GameObject.FindWithTag("Player");
Note: Works only if there is only one game object with the tag "Player" in the scene and if there are multiple of them, this method returns the first one it finds.
To get multiple players as targets, use
targets[] = GameObject.FindGameObjectsWithTag("Player");
For more information, refer:
https://docs.unity3d.com/ScriptReference/GameObject.FindWithTag.html
https://docs.unity3d.com/ScriptReference/GameObject.FindGameObjectsWithTag.html

How to make Unity NavMeshAgent path around the player

in my project there's a Unity Multipurpose Avatar Crowd that uses the NavMeshAgent component. However when in VR it will come very close to the player in a very intimidating way, it be best to avoid that. Been trying to get the to path around the player, but not quite sure how.
Things tried so far:
Putting a NavMeshAgent on the player - this makes them able to push the player if he's in their path
NavMeshObstacle on the player - this makes the AI sink into the ground if the player approaches them
Thinking the best way to do is in a script, but not entirely sure how to just slightly go around the player keeping the current desired destination.

Unity Photon Player Instantiation

I have a SteamVR Unity project which I'm converting to multiplayer.
When another client joins the game, instead of two different players seeing each other, each player has it's own version of the game where he controls all fo the player instances.
For example, while one player is connected everything is fine, but when a second player joins, the game just adds another Player prefab which the first player controls as well.
I tried replacing the Player with a simple cube and everything seems fine.
both the Player and the cube have Photon Transform View and Photon View scripts.
I would appreciate any help I can get.
This is a common problem, when you start with PUN. You probably setup a player prefab with network synchronization and instantiate that for each player. All scripts on the instances will act on the local input, which is what you see now.
You want two variants of the prefab, for local and remote representation. As it's impractical to always configure two prefabs, instead you build one which initializes itself (in Awake or Start) as local or remote. Your scripts should check the object's PhotonView if it's .isMine or not.
This can be done per Component (in each distinct script) or you could add a component which enables/disables the scripts on a GameObject, depending on isMine.
The Basics Tutorial does this, for example.
Unity doesn't know if it's multiplayer or not. When you give an input all of the scripts who are waiting for input takes it and behaves accordingly. To solve this basically create another player that doesn't take any input and spawn it for other players.

Issue with character models in unity

I am using the Unity 4 game engine and I have two scripts, one that tells the enemy to attack the player, and one that lets the player attack the enemy. The enemy can attack the player, but the other script does not work. If i apply the script to a game object such as a capsule or cube, the script works fine, but not when i apply it to an imported character model. The script basically works by checking the enemy's health and destroying the gameobject if it is equal to or below zero. I have a feeling it has to do with the character model not being an actual game object but i am pretty sure it is. Can anyone help?
When you make the enemy character. Make an empty game object. Then create your enemy inside of that object. In the hierarchy view it should be parented by the empty object. You can also do this by dragging the enemy to the empty game object in the hierarchy view. Then attach the script to the empty game object instead of the model.
Solution
Please check all the tags you are applying to the scripts .No Script could able to work until unless the tagging is done . The tagging help the game object to differentiate between normal object and the enemy so the tagging plays a vital role to run scripts .