I'm having this problem where players will respawn at spawn points every time a new player joins the room. Each player should only be moved to a spawnpoint every time they join a room. This does happen and it works. It also happens in a respawn RPC, but that isn't called until a player has already died. If any new player joins and spawns, any existing players are teleported to a spawnpoint (as if it is their first time joining). I've tried checking for local photon views, but that doesn't work. I've tried figuring out where the respawn comes from, but I can't pinpoint it. It seems that each players' Start() function runs twice, but that doesn't make any sense. Any ideas why this happens?
Player Start function:
void Start()
{
if (photonView.IsMine)
{
SetPlayerTeam(gameInst.specString);
transform.position = GameManager.instance.spawnPoints[Random.Range(0, GameManager.instance.spawnPoints.Length)].position;
transform.rotation = GameManager.instance.spawnPoints[Random.Range(0, GameManager.instance.spawnPoints.Length)].rotation;
}
}
Player Respawn RPC
void RespawnRPC()
{
SetRagdoll(false);
if (photonView.IsMine)
{
transform.position = GameManager.instance.spawnPoints[Random.Range(0, GameManager.instance.spawnPoints.Length)].position;
transform.rotation = GameManager.instance.spawnPoints[Random.Range(0, GameManager.instance.spawnPoints.Length)].rotation;
if (!playerProperties.ContainsKey("state"))
{
playerProperties.Add("state", "alive");
}
playerProperties["state"] = "alive";
PhotonNetwork.LocalPlayer.SetCustomProperties(playerProperties);
}
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
Maybe you are buffering this RespawnRPC call? that would explain.
RPC can only be called by you, so put also a debug where you actually call this RespawnRPC, this will probably help as well checking that your code is not sending more than you expect.
Related
I have two objects: a player and an enemy. The main problem is the enemy, which should start shooting at the player when it approaches him at a certain distance (in short, just start the animation).
At first I tried to implement this through Vector 3, as with other ordinary opponents. But he is as stupid and crutch as possible, however, you yourself can see everything in the pinned.
I started to implement it more allegedly correctly, through the trigger and the collider (box collider) of the enemy itself. And everything works right as it should, but there is a nuance. The enemy also has boxing implemented through the box collider, the player, hitting which, causes damage to him. There is only one box collider for these two tasks, and since I had to increase this collider so that the enemy could stop in front of the player at a certain distance. Because of this, the player can hit at a great distance (at which the enemy can shoot) from the enemy and the enemy takes damage anyway.
I tried to make a separate object the size of the enemy himself and use it as a box to receive damage. Then he already transmits data about receiving damage to the enemy object. But this does not work, all links between scripts and objects are made, but he does not want to transfer data. That is, simply making two colliders for different tasks does not work.
In general, here my powers are all. I searched the entire Internet, but I did not find how to implement this mechanic in a different way so that it does not conflict with others. Therefore, I ask the help of the local experts, where I screwed up.
private Animator ch_animator;
// Start is called before the first frame update
void Start()
{
myAgent = GetComponent<NavMeshAgent>();
myAnim = GetComponent<Animator>();
EnemyH = GetComponent<GDHealth>();
}
// Update is called once per frame
void Update()
{
dist = Vector3.Distance(/*checker.*/transform.position, target.transform.position);
if (dist > range)
{
myAgent.enabled = false;
myAnim.SetBool("Idle", true);
myAnim.SetBool("Move", false);
myAnim.SetBool("Attack", false);
}
if (dist <= range & dist > atRange)
{
myAgent.enabled = true;
myAgent.SetDestination(target.position);
myAnim.SetBool("Idle", false);
myAnim.SetBool("Move", true);
myAnim.SetBool("Attack", false);
}
if (dist <= atRange)
{
StartCoroutine(Attack());
}
if (PlayerH._health <= 0)
{
atRange = 0;
}
if (EnemyH._health < 0)
{
myAgent.enabled = false;
}
}
public IEnumerator Attack()
{
yield return new WaitForSeconds(0.5f);
myAgent.enabled = false;
myAnim.SetBool("Idle", false);
myAnim.SetBool("Move", false);
myAnim.SetBool("Attack", true);
}
void OnTriggerStay(Collider col)
{
if (col.tag == "Player")
{
//gameObject.GetComponent<Animator>().SetBool("Attack", true);
StartCoroutine(Attack());
transform.LookAt(col.transform.position);
transform.eulerAngles = new Vector3(0, transform.eulerAngles.y, 0);
}
}
void OnTriggerExit(Collider col)
{
if (col.tag == "Player")
{
myAgent.enabled = true;
myAgent.SetDestination(target.position);
myAnim.SetBool("Idle", false);
myAnim.SetBool("Move", true);
myAnim.SetBool("Attack", false);
//gameObject.GetComponent<Animator>().SetBool("Attack", false);
}
}
But this does not work, all links between scripts and objects are made, but he does not want to transfer data.
From this description it could be anything: wrong layers, no rigidbody on either of objects, misstyped tags in OnTriggerStay method.
In my project, I successfully created 2 colliders for 2 separate tasks, so this is how I would see it in your problem:
Use two colliders
Attach one collider with one script to the enemy object - this collider should have a size of the enemy. The OnTriggerStay method here should deal damage to the enemy, check for death, etc.
Create child object to the enemy. Attach new collider to it with the size of enemy's attack range. Attach a script with OnTriggerStay method that will stop enemy and begin ranged attack (or whatever you want to do).
If this doesn't work: check collision matrix or try adding a kinematic rigidbody to either of objects.
Measure distance between player and the enemy in update (which you are already doing) and apply necessary code based on distance (stop or attack) thus replacing one of the colliders.
Hope that helps!
I got a simple "OnTouch" script on my enemies, which knocks back the player if they come in contact. The player then gets invincible for a short time. Something like this:
void OnTriggerEnter2D(Collider2D collider) {
if (collider.gameObject.CompareTag("Player")) {
if (Time.time > isInvincible) {
isInvincible = Time.time + invincibleTimer;
if (enemy.IsFacingRight) {
player.SetVelocity(knockback * Vector2.right);
} else {
player.SetVelocity(knockback * Vector2.left);
}
}
}
}
(SetVelocity is just a method i use to set.. velocity)
The problem with this is when the player gets invincible after been pushed away. While invincible you can then run on top of an enemy and stay there, even after the invincible timer runs out. Which i guess makes sense since it only triggers on enter.
Using the same code but inside a "OnTriggerStay2D", works as expected. You get pushed away, go invincible, run on top of an enemy, invincible runs out and you then get pushed away out of the enemy.
But having multiple enemies running around with OnTriggerStay colliding with different objects feels like it would be bad performance wise? Is there any more efficient way to do this? Or is TriggerStay the way to go?
The way I found is manually tracking the collisions like this:
List<Collider2D> hitColliders = new List<Collider2D>();
void OnTriggerEnter2D(Collider2D collision) {
if (hitColliders.Contains(collision)) { return; }
hitColliders.Add(collision);
}
void OnTriggerExit2D(Collider2D collision) {
hitColliders.Remove(collision);
}
// Perform operations to the colliders.
void Update() {
foreach (var col in hitColliders) { DoStuff(col); }
}
Although you may not run into performance problems even with the solution you have now, if you do in the future you can try using Physics.IgnoreLayerCollision:
At the start of your invincibleTimer call:
IgnoreLayerCollision(playerLayer, enemyLayer, true);
And at the end of your timer call:
IgnoreLayerCollision(playerLayer, enemyLayer, false);
And acording to the docs:
IgnoreLayerCollision will reset the trigger state of affected
colliders, so you might receive OnTriggerExit and OnTriggerEnter
messages in response to calling this.
This means that when you call IgnoreLayerCollision(false), OnTriggerEnter will be called again, even if you are already on top of an enemy. This is exactly the behaviour you are after.
Basically, i have player object and bunch of other. For this moment they all have RigitBody2D, and colliders, code for creating:
obj = new GameObject();
obj.tag = "web";
obj.name = "web";
float randomX = Random.Range (Utilities.getMainCameraBounds ().min.x, Utilities.getMainCameraBounds ().max.x);
float randomY = Random.Range (Utilities.getMainCameraBounds ().min.y, Utilities.getMainCameraBounds ().max.y);
obj.transform.position = new Vector3(randomX, randomY);
obj.transform.localScale = new Vector3 (3.0f, 3.0f, 3.0f);
collider = obj.AddComponent <BoxCollider2D>();
body = obj.AddComponent<Rigidbody2D> ();
body.constraints = RigidbodyConstraints2D.FreezeAll;
joint = obj.AddComponent<HingeJoint2D> ();
renderer = obj.AddComponent<SpriteRenderer>();
renderer.sortingOrder = 1;
renderer.sprite = Resources.Load("Textures/dot_net", typeof(Sprite)) as Sprite;
Purpose for this is to catch collision like this :
void OnCollisionEnter2D(Collision2D collision) {
if (collision.transform.tag == "web") {
Player.getInstance().joint(collision.rigidbody);
}
}
After player collides with some of those objects, method joint called, it's purpose to connect player and web with HingeJoint2D to simulate pendulum behvior. Code of joint:
public void joint(Rigidbody2D body) {
WebCollection.getInstance ().turnOffColliders (body.gameObject);
HingeJoint2D hingeJoint = webLine.getObject ().AddComponent<HingeJoint2D> ();
hingeJoint.connectedBody = body;
hingeJoint.anchor = new Vector2 (0.00120592f, 0.1921938f);
hingeJoint.connectedAnchor = new Vector2 (0.1939605f, 0.03025406f);
}
Pendulum behavior is working just fine, but in moment of it, or just when player is moving towards web, other web objects are colliding with it, creating real colliding behavior(like crushing into each other)
My goal: i want web objects to react on the collision(calling method above), but on the screen to player just go throw them.
What i've already tried: I wanted to turnOff other objects bodies (WebCollection.getInstance ().turnOffColliders (body.gameObject);) and after pendulum movement is over, to turn them back, but it causes very strange behavior.
What also can be a solution is to manually checking if player is colliding with web object right now, and handle it, but this is to costly i think.
To put my comment into an answer for those finding your question:
If you want to have Colliders that only trigger events and do not physically collide with other Colliders, then what you want are Triggers. To make a Collider a trigger you check the Is Trigger checkbox of the collider component in the inspector (you can also set it via script with the Collider.IsTrigger boolean property).
To do something when the trigger fires, use the OnTrigger events (OnTriggerEnter, OnTriggerExit, OnTriggerStay) (see http://docs.unity3d.com/ScriptReference/Collider.html)
There is also a short introduction to Colliders as Triggers by unity: http://unity3d.com/learn/tutorials/topics/physics/colliders-triggers
Hi everyone Im a bit stuck here. I have my gameObject Enemy Spawn at a random time. But the thing is that I only want the Enemy to be in the game for say 5 seconds. The trouble is that I can't destroy the object at all. Here I wrote this code to try and destroy the Enemy Object:
public class SpawnManager : MonoBehaviour {
public GameObject Enemy;
public float mytimer;
public float enemyHealth = 5.0f;
void Start()
{
GameObject player = GameObject.Find("Player");
}
void spawnEnemy() {
Transform enemy;
GameObject enemySpawnPoint = GameObject.Find("EnemySpawn");
enemy = Instantiate(Enemy,enemySpawnPoint.transform.position,enemySpawnPoint.transform.rotation) as Transform;
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.name == "EnemyTrigger") {
mytimer = Random.Range(0,10);
//Debug.Log("Now Destroying");
Invoke("spawnEnemy", mytimer);
Debug.Log("Spawn Normal");
if(Enemy.name == "BloodyMary(Clone"){
Destroy(Enemy, enemyHealth);
Debug.Log("Now Destroying");
}
}
}
}
Everytime I run into the trigger it spawns a "BloodyMary(Clone)" which I am trying to destroy. Any advice?
In my eyes it would make more sense for the enemy to handle its own destruction. This would mean that an enemy is responsible for its own duration which I feel would make more sense if you're having possibly a ton of enemies on the scene at any time.
I would create a co-routine which would simply wait for 5 seconds and then call the Destroy(gameObject) function to destroy itself. It may look a little like this:
IEnumerator DeathTimer(float duration)
{
yeild return new WaitForSeconds(duration);
Destroy(gameObject);
}
Then inside your 'Start()' method I would then call the co-routine (not done as a normal method). This would be done using something like this:
void Start()
{
// calls the coroutine to start
StartCoroutine("DeathTimer", duration);
}
Note: By calling a co-routine using a string (like above) you can then call "StopCoroutine("MethodName");" which will stop the coroutine at any time. This would be better than passing in a method parameter into the StartCoroutine().
This means your spawner is now purely responsible for spawning the enemies and they are responsible for their own death if they last for too long. Then you're not trying to manage multiple enemies on the spawner and you don't need to worry about tracking them in there.
I had a friend ask a very similar question the other day and he used this and it worked a treat.
The code that says:
Invoke ("spawnEnemy", myTimer);
Is calling your spawnEnemy() function, which creates anther Enemy clone. It also does the call with a delay timer.
If you don't want another Enemy just remove that part of the code.
Also you are destroying with a timer. You can Destroy instantaneously with Destroy(Enemy).
I see a typo, as you missed a closing parethesis on the name check. the reason that the linter didnt pick this up was because the parenthesis is in the string, so to the program it seems perfectly reasonable to check for "bloodymary(clone" instead of "bloodymary(clone)".
Solution is straight forward.
use Destroy(gameobject,5); after u initiate that gameobject.
example
<-- line initiate gameobject -->
Destroy(gameobject,5);
which 5 is target second before gameobject will destroy.
I'm writing a 2D game and I'm trying to get moving platforms to work. After doing some previous investigation, I have it ALMOST working. The idea is to have 2 platform objects with colliders: 1 a visible object, the other an invisible object with isTrigger set (since the player would just go through a trigger). The code for the Moving Platform child (the trigger one) is set here.
using UnityEngine;
using System.Collections;
public class MovingPlatformChild : MonoBehaviour
{
public string parentPlatform = "";
void Start ()
{
transform.parent = GameObject.Find(parentPlatform).transform;
}
// Update is called once per frame
void Update ()
{
}
void OnTriggerEnter(Collider playerObject)
{
Debug.Log ("enter moving platform");
if(playerObject.gameObject.name.Contains("Player"))
{
playerObject.transform.parent = gameObject.transform;
}
}
int i = 0;
void OnTriggerStay(Collider playerObject)
{
Debug.Log ("stay" + i++);
if(playerObject.transform.position.y >= transform.position.y)
{
playerObject.transform.parent = gameObject.transform;
}
else
{
playerObject.transform.parent=null;
}
}
void OnTriggerExit(Collider playerObject)
{
Debug.Log ("EXIT");
if(playerObject.gameObject.name.Contains("Player"))
{
playerObject.transform.parent=null;
}
}
}
The Start() function just makes it a child of the visible platform. This can probably be done right in the Unity editor as well, instead of through code.
The OnTriggerEnter function adds the player object as a child of the trigger platform object, which is a child of the visible platform. So they should all move together.
The OnTriggerStay is an attempt to verify that this remains true only while the player is on the top of the platform. While the player is within the trigger, if the player is on top of the platform, then it remains attached. Otherwise, it's not. This is so that nothing happens on the bottom end.
The OnTriggerExit function just removes the player object as a child when it exits the trigger.
This is somewhat working (but we know somewhat isn't good enough). It works sometimes, but the player will be very jittery. Also, on the way down while standing atop the platform, the TriggerStay function doesn't appear to be called (implying the player is no longer within the trigger). This is observed through my Debug "stay" statement. Finally, sometimes the player will also fall straight through the platform.
What in this code would allow the player to fall through the platform, or be so jittery on the way up? Am I missing something crucial? If you need any more code, please let me know.
Below is the code for the movement of the non-trigger platform (the parent of the trigger platform and in an identical position). I will also share the Player's Update function after that.
void Start ()
{
origY = transform.position.y;
useSpeed = -directionSpeed;
}
// Update is called once per frame
void Update ()
{
if(origY - transform.position.y > distance)
{
useSpeed = directionSpeed; //flip direction
}
else if(origY - transform.position.y < -distance)
{
useSpeed = -directionSpeed; //flip direction
}
transform.Translate(0,useSpeed*Time.deltaTime,0);
}
And now the player code:
void Update()
{
CharacterController controller = GetComponent<CharacterController>();
float rotation = Input.GetAxis("Horizontal");
if(controller.isGrounded)
{
moveDirection.Set(rotation, 0, 0); //moveDirection = new Vector3(rotation, 0, 0);
moveDirection = transform.TransformDirection(moveDirection);
//running code
if(Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift)) //check if shift is held
{ running = true; }
else
{ running = false; }
moveDirection *= running ? runningSpeed : walkingSpeed; //set speed
//jump code
if(Input.GetButtonDown("Jump"))
{
//moveDirection.y = jumpHeight;
jump ();
}
}
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
}
EDIT: I've added the specifications for the platforms and player in this imgur album:
http://imgur.com/a/IxgyS
This largely depends on the height of your trigger box, but it's worth looking into. Within your TriggerStay, you've got an IF statement concerning the player y coordinates. If the trigger box is fairly large and the platform's speed fast enough, on the way up and between update ticks the player Y coords could momentarily be smaller than the trigger Y coords. This would lead to him losing the parentage, only to regain it a few ticks later. This might be the cause of the 'jittering'.
The problem I was having included
The moving platform was written using Translate. I rewrote it using a rigidbody and the rigidbody.Move function. This didn't immediately help, but...
I realized the CharacterMotor script (Unity provides this) that I had attached to the player included moving platform support. I set the MovementTransfer value to PermaLocked, and also unchecked the "Use FixedUpdate" box on the script, and it now works 99% of the time. I've had one time where I did a particular behaviour and slipped through, but I can't recreate it.
Hope this helps anyone who might be looking for an answer!