I'm developing a shooting game in Unity and I have a problem with multiplayer mode when using Unity Multiplayer Platform(Unity Servers). My bullets are using Network Identity and Network Transform.
Some bullets doesn't show up on the side that shoots them but they are visible in other side and the damage is taken.
Here's my shooting script:
[Command]
public void CmdShoot(){
this.transform.FindChild("gun").SendMessage ("CmdGunshoot");
}
Here's my gun script:
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
public class shoot : NetworkBehaviour {
public GameObject bulletPref;
public int power;
private Transform spawner;
public int damage;
void Start () {
spawner = this.transform.FindChild("Bspwaner");
}
[Command]
public void CmdGunshoot (){
GameObject bull = Instantiate(bulletPref, spawner.position, Quaternion.identity) as GameObject;
bull.GetComponent<bullet>().dmg = damage;
bull.gameObject.transform.GetComponent<Rigidbody>().AddForce(spawner.right * power);
NetworkServer.Spawn (bull);
Destroy(bull, 2.0f);
}
}
Here's my bullet script:
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
public class bullet : NetworkBehaviour {
public GameObject ie;
public int dmg;
void OnCollisionEnter(Collision other){
Instantiate(ie, transform.position, Quaternion.identity);
NetworkServer.Destroy (this.gameObject);
if (other.gameObject.tag != "Player")
return;
other.gameObject.SendMessage ("takeDamage", dmg);
}
}
What the problem can be?
Thanks in advance,
Related
I'm using this code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class CollisionPlayer : MonoBehaviour
{
public bool alreadyDied = false;
public GameObject player;
public float timeDeath;
public ParticleSystem particles;
public GameObject explosionGO;
private SpriteRenderer sr;
private BoxCollider2D bc;
private PlayerController walkScript;
void Start()
{
sr = GetComponent<SpriteRenderer>();
bc = GetComponent<BoxCollider2D>();
walkScript = GetComponent<PlayerController>();
}
void OnCollisionEnter2D (Collision2D collide)
{
if (collide.gameObject.CompareTag("Dead"))
{
Instantiate(particles, player.transform.position, Quaternion.identity);
Instantiate(explosionGO, player.transform.position, Quaternion.identity);
CinemachineShake.Instance.ShakeCamera(30f, .1f);
alreadyDied = true;
}
}
void Update()
{
if(alreadyDied == true)
{
timeDeath -= Time.deltaTime;
sr.enabled = false;
bc.enabled = false;
walkScript.enabled = false;
}
if(timeDeath <= 0)
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
}
}
This is the bullet's code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LeftBulletScript : MonoBehaviour
{
// Start is called before the first frame update
public float speed;
public float destructionLeftTime;
public ParticleSystem particles;
private GameObject thisGameObject;
void Start()
{
thisGameObject = this.gameObject;
Destroy(gameObject, destructionLeftTime);
}
void Update()
{
transform.Translate(Vector2.left * speed * Time.deltaTime);
if(destructionLeftTime > 0.05f)
{
destructionLeftTime -= Time.deltaTime;
}
else
{
Instantiate(particles, thisGameObject.transform.position, Quaternion.identity);
}
}
}
This code should spawn some particles and a sound effect when the player gets hit by something with tag "Dead". But that does not happen. I have a box collider 2D on both the bullet (that should kill me) and the player. My Rigidbody2D is dynamic on the player with z freezed. The bullet does not have a rigidbody. I made sure that the bullet actually has the tag "Dead", spelled the exact same way as the way I wrote on the script. The weirdest thing is that I used this code on another game and nothing changed (just the name of a script). Both the player and the bullet are on the same layer. Anyone could tell me what could have happened?
I have my player which uses character controller for moving. I placed a sprite in the scene and I'd like for when my player collides with the sprite to disable the sprite, like if the player grabs the sprite (which is Doom's 64 chainsaw).
The sprite's collisions of course work well with everything, but not with the player. How can I get proper collision between them?
You could do it like this:
1-Attach "Pickable" script to the sprite.
2-Attach "Player" script to the character controller.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Pickable : MonoBehaviour
{
public float radius = 1f;
private void Start()
{
SphereCollider collider = gameObject.AddComponent<SphereCollider>();
collider.center = Vector3.zero;
collider.radius = radius;
collider.isTrigger = true;
}
}
Here is the other script.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
private void OnTriggerEnter(Collider other)
{
Pickable pickable = other.GetComponent<Pickable>();
if(pickable != null)
{
Destroy(other.gameObject);
}
}
}
I writing simple game on Unity (C#)
I have player and want to make the destroyer, that will destroy player.
I create prefab of destroyer. And next, I create Quad.
I have spawn script:
using UnityEngine;
using System.Collections;
public class SpawnScript : MonoBehaviour {
public GameObject[] obj;
public float spawnMin = 1f;
public float spawnMax = 2f;
// Use this for initialization
void Start () {
Spawn();
}
void Spawn()
{
Instantiate(obj[Random.Range(0, obj.GetLength(0))], transform.position, Quaternion.identity);
Invoke("Spawn", Random.Range(spawnMin, spawnMax));
}
}
Also I write DestroyerScript:
using UnityEngine;
using System.Collections;
public class DestroyerScript : MonoBehaviour {
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Player")
{
Application.LoadLevel(1);
return;
}
if (other.gameObject.transform.parent)
{
Destroy(other.gameObject.transform.parent.gameObject);
}
else
{
Destroy(other.gameObject);
}
}
}
My destroyer spawning, but when player get it, I don't have Game Over screen.
Add rigid body to both player and "player destroyer" and then set onTriggerEnter on your player destroyer like so:
void OnTriggerEnter(Collider other) {
Destroy(other.gameObject);
}
For some fine tuning, you can do some checks if the other object is in fact the Destroyer (you can compare the tag or something, I won't go into too much detail now).
EDIT: Uncheck "isTrigger" on your BoxColliders and try this:
void OnCollisionEnter (Collision col)
{
Destroy(col.gameObject);
}
Can please someone format the code for me?
my code:
using UnityEngine;
using System.Collections;
public class ScorePoint : MonoBehaviour
{
private AudioSource audioSource;
public AudioClip Scored;
void OnTriggerEnter2D(Collider2D collider)
{
if(collider.tag == "Player")
{
audioSource = GetComponent<AudioSource>();
audioSource.clip = Scored;
audioSource.Play();
}
}
}
This code only works when the collider is Is Trigger.
If you want to make it work with a collider not set as a trigger you should use OnCollisionEnter2d instead. Make sure to change the parameter from Collider2d to Collision2d.
using UnityEngine;
using System.Collections;
public class ScorePoint : MonoBehaviour
{
private AudioSource audioSource;
public AudioClip Scored;
void OnCollisionEnter2D(Collision2D collision)
{
if(collision.gameObject.tag == "Player")
{
audioSource = GetComponent<AudioSource>();
audioSource.clip = Scored;
audioSource.Play();
}
}
}
OnTriggerEnter2d
Sent when another object enters a trigger collider attached to this object (2D physics only).
http://docs.unity3d.com/ScriptReference/MonoBehaviour.OnTriggerEnter2D.html
OnCollisionEnter2d
Sent when an incoming collider makes contact with this object's collider (2D physics only).
http://docs.unity3d.com/ScriptReference/MonoBehaviour.OnCollisionEnter2D.html
I found a good re-spawner for my game and I respawn 50 spheres. I want them to disappear when I walk into them but they do nothing.
Here is my script:
using UnityEngine;
using System.Collections;
public class BoxDestroy : MonoBehaviour
{
void OntriggerEnter(Collider collider)
{
if (collider.gameObject.tag == "Player")
{
Destroy(gameObject);
}
}
}
Here is my re-spawner:
using UnityEngine;
using System.Collections;
public class spawner : MonoBehaviour
{
public GameObject objectToSpawn;
public int numberOfEnemies;
private float spawnRadius = 5;
private Vector3 spawnPosition;
// Use this for initialization
void Start ()
{
SpawnObject ();
}
void Update ()
{
}
void SpawnObject()
{
for (int i= 0; i < numberOfEnemies; i++)
{
spawnPosition = transform.position + Random.insideUnitSphere * spawnRadius;
Instantiate(objectToSpawn, spawnPosition, Quaternion.identity);
}
}
}
Any ideas?
OntriggerEnter should be OnTriggerEnter! Case sensitive :)
Did you actually tag your player-gameobject with a "player" tag?
Did you mark the colliders as triggers? OnTriggerEnter vs OnCollisionEnter
Do you have a rigidbody component on your player object?