I have a projectile and an enemy, but I want the enemy to decrease the heath variable when it touches the projectile.
I tried to shoot projectiles but it did not decrease the health.
`using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Health : MonoBehaviour
{
public int startHealth = 20;
public int health;
// Start is called before the first frame update
void Start()
{
health = startHealth;
}
// Update is called once per frame
void OnTriggerEnter2D(Collider2D col)
{
Debug.Log("Hit");
if (col.gameObject.tag == "PlayerProjectile")
{
health = health - 1;
}
}
void LateUpdate()
{
if (health < 1)
{
Destroy(gameObject);
}
}
}
You're probably missing one or more things:
A Rigid body inside both your bullet and your enemy.
A collider (non-trigger) inside both game objects.
The layer collision matrix must match between both objects (you can learn more of this inside this document: https://docs.unity3d.com/Manual/LayerBasedCollision.html)
If the bullet is too fast, you must change the "Collision Detection Mode", also, you can learn more here: https://docs.unity3d.com/ScriptReference/Rigidbody-collisionDetectionMode.html
I hope I help you!
Related
I tried to make this object move down but everytime it goes out of the screen it teleports back up with a different x position but it appears in random places on screen and collides with things. it only has this script and a rb2d and a boxcollider2d. rb2d doesnt have gravity,collision detection continious,interpolate interpolate,sleeping mode never sleep.im new to unity
this is the code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Teleport : MonoBehaviour
{
public Rigidbody2D rb;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update(){
}
void FixedUpdate(){
rb.velocity=new Vector2(0f,-5f);
if (rb.position.y<-5.5f){
float rndf= Random.Range(-10.14f,10.14f);
rb.MovePosition(new Vector2(rndf,5.5f));
}
}
}
here is a photo of the editor
It was because i moved it and didn't teleport it. It collided with objects on the way
I added a particle system to my unity project. But I can't make it attach to a certain game object or tag. How can I make a Particle System sit on the center of a cube, while it's getting played once if the player triggers with this cube?
Code used for the trigger to work:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Collision_Player_grower : MonoBehaviour
{
public GameObject Player_grower;
public ParticleSystem CollisionGrower;
// Start is called before the first frame update
void Start()
{
times_player_grower = 0;
CollisionGrower.transform.position = Player_grower.transform.position;
}
// Update is called once per frame
void OnTriggerEnter(Collider collision)
{
if (collision.gameObject.tag == "Player")
{
print("we hit an playergrower");
CollisionGrower.Play();
Destroy(Player_grower);
}
}
}
Note: It does work if I manually place the particle system in the heart of the cube, but I assume this can be done in an easier way.
From what I understand, you want the position of the particle system to be in the center of another object.
1)
You could do that simply by attaching a particle system game object as a child of the cube you were talking about.
2)
You could do this with code instead. In case you want the particle system to do something extra, you could just adjust the code.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Collision_Player_grower : MonoBehaviour
{
public GameObject ParticleSystemObject;
public GameObject Cube;
public GameObject Player_grower;
public ParticleSystem CollisionGrower;
// Start is called before the first frame update
void Start()
{
times_player_grower = 0;
CollisionGrower.transform.position = Player_grower.transform.position;
}
// Update is called once per frame
void Update()
{
ParticleSystemObject.transform.position = Cube.transform.position;
}
void OnTriggerEnter(Collider collision)
{
if (collision.gameObject.tag == "Player")
{
print("we hit an playergrower");
CollisionGrower.Play();
Destroy(Player_grower);
}
}
}
I don’t fully understand what the rest of the variables are for, and what they are, so I may have done some un-needed things.
First, I added two GameObject variables. One of them is the cube object you want the particle system to go to, and the other is the particle system as a game object. I set the position of the particle system to the position of the cube.
Important: if you have this script attached to the cube, remove the Cube variable.
Instead of using
... = Cube.transform.position;
Use
... = transform.position;
I have a fps with a gun and when I shot the particle system continue to play . I want to do the effect of pistol shot:
I want the duration to be 0.5 sec or something
It just appears more and don't make the effect of one shot , they just fall down , what can I do?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class SpawnProjectiles : MonoBehaviour
{
public GameObject firePoint;
public List<GameObject> vfx = new List<GameObject>();
private GameObject effectToSpawn;
public Button button;
// Start is called before the first frame update
void Start()
{
effectToSpawn = vfx[0];
button.onClick.AddListener(TaskOnClick);
}
// Update is called once per frame
void Update()
{
}
void SpawnVFX ()
{
GameObject vfx;
if(firePoint != null)
{
vfx = Instantiate(effectToSpawn, firePoint.transform.position, Quaternion.identity);
}else
{
Debug.Log("NoFirePoint");
}
}
void TaskOnClick()
{
SpawnVFX();
}
}
Uncheck Looping, otherwise the Particle system will keep emitting.
Can someone please tell me how to incorporate this
code
into one below?
I really don't know what I'm doing; it does not work and just freezes. I want it the player to leave without freezing the game on the one side.
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour {
void OnPlayerDisconnected(NetworkPlayer player) {
Debug.Log("Clean up after player " + player);
Network.RemoveRPCs(player);
Network.DestroyPlayerObjects(player);
}
}
using UnityEngine;
using UnityEngine.SceneManagement;
using System.Collections;
using UnityEngine.Networking;
public class RG_Disconnect : MonoBehaviour {
// Use this for initialization
void Start () {
GameObject lobbyManager;
lobbyManager = GameObject.Find ("LobbyManager");
if (lobbyManager != null)
Destroy(lobbyManager);
NetworkManager.Shutdown ();
}
// Update is called once per frame
void Update () {
SceneManager.LoadScene ("Garage");
}
}
Of course it freezes. You are loading the scene with name "Garage" in every frame update.
https://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager.LoadScene.html
LoadScene is only meant to be used once to load a specific scene.
I'm totally new with Unity (and game development in general). I followed the great simple tutorial Survival Shooter and I have one question: in this tutorial, we add a Y constraint position to the rigidbody's character plus we set the drag value and the angular drag value to infinite. How can we make the character jump since those settings prevent the character from moving to the Y axis?
If someone can give me an hand on that please...
Thanks a lot!
Why exactly do you add the constraint on the Y axis? You could remove it, and then just add gravity that will make your player stick to the ground. After that just apply a force, or just a simple translation going upwards by a set speed, to make the player jump and then wait for gravity to bring him back down.
This is what I would do to jump.
P.S. you need to remove the constraints on the Y axis on the vector
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Spawner : MonoBehaviour
{
public Vector3 force;
public Rigidbody rb;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetKey(KeyCode.Space) && transform.position.y == 0) //Enter your y axix where ground is located or try to learn a little more about raycasting ill just use 0 for an example)
{
rb.AddForce(force);//Makes you jump up when you hold the space button down line line 19 will do so that you only can jump when you are on the ground.
} if (Input.GetKeyUp(KeyCode.Space))
{
rb.AddForce(-force); //When you realase the force gets inverted and you come back to ground
}
}
}
I would do this instead edit the code over this post.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Just : MonoBehaviour {
public Vector3 force;
public Rigidbody rb;
bool isGrounded;
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Space) && isGrounded == true) //Rember to got to your "Ground" object and tag it as Ground else this would not work
{
rb.AddForce(force);
}
}
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "Ground")
{
isGrounded = true;
}
}
void OnCollisionExit(Collision collision)
{
isGrounded = false;
}
}
You need to assign your ground object a tag called Ground, You need to make your own tag called Ground its not that hard tho you click your object and top left of the inspector there's the tag and then u just make a new tag called Ground. And also plz rember to assign the other values on your player object.