explosion animation not in the right place when it is played - unity3d

I was making an enemy death animation on unity(again) when it did not want to work, now when I say it did not want to work I mean it did not want to work in the position it is supposed to be in(which is where the enemy is)
public class EnemyController : MonoBehaviour
{
public int health = 3;
public GameObject explosion;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public void TakeDamage()
{
health--;
if(health <= 0)
{
Destroy(gameObject);
Instantiate(explosion, transform.position, transform.rotation);
}
}
}

You're using the transform of an object that you have already destroyed, as transform is a property of gameObject.
Try instantiating the Explosion prefab before calling Destroy(gameobject).
public void TakeDamage()
{
health--;
if (health <= 0)
{
Instantiate(explosion, transform.position, transform.rotation);
Destroy(gameObject);
}
}

Related

Bullet doesnt dissapear when hittign a collision unity 2d

enter image description herei am making a script where the player can shoot in my unity game. This works but the bullet doesnt go away when hitting a collision. Can anyone help me with this problem? this is the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BulletDie : MonoBehaviour
{
public GameObject diePEffect;
public float dieTime;
// Start is called before the first frame update
void Start()
{
StartCoroutine(Timer());
}
// Update is called once per frame
void Update()
{
}
private void OncollisionEnter2D(Collision2D collision)
{
GameObject collisionGameObject = collision.gameObject;
if (collisionGameObject.name != "Player")
{
Die();
}
}
IEnumerator Timer()
{
yield return new WaitForSeconds(dieTime);
Die();
}
void Die()
{
if (diePEffect != null)
{
Instantiate(diePEffect, transform.position, Quaternion.identity);
}
Destroy(gameObject);
}
}

2D Player Shaking When Platforms Move Up and Down?

For some reason, my player character shakes up and down when the platform moves up and down, which prevents the player from jumping because the player is not grounded. I tried several things including adding a kinematic rigidbody to the platform and trying to make the player a child of the platform after landing on the platform but nothing has worked so far. Any help would be appreciated. Thank you!
Here is my code:
public class MovingPlatform : MonoBehaviour
{
private Vector3 posA;
private Vector3 posB;
private Vector3 nexPos;
public GameObject Player;
[SerializeField]
private float speed;
[SerializeField]
private Transform childTransform;
[SerializeField]
private Transform transformB;
void Start()
{
posB = childTransform.localPosition;
posB = transformB.localPosition;
nexPos = posB;
}
// Update is called once per frame
void Update()
{
Move();
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.collider.tag == "Player")
{
collision.collider.transform.SetParent(transform);
}
}
private void OnCollisionExit2D(Collision2D collision)
{
if (collision.collider.tag == "Player")
{
collision.collider.transform.SetParent(null);
}
}
private void Move()
{
childTransform.localPosition = Vector3.MoveTowards(childTransform.localPosition, nexPos, speed * Time.deltaTime);
if (Vector3.Distance(childTransform.localPosition,nexPos) <= 0.1)
{
ChangeDestination();
}
}
private void ChangeDestination()
{
nexPos = nexPos != posA ? posA : posB;
}
}
Move(); should be executed in lateupdate, because otherwise you will set the position of the player and later the physics system will update it, causing stuttering.
Another solution would be raycasting straight down and placing your GameObject on the collision point (plus some kind of offset)

Main camera only see the ground

I make a 3d game.My gun only shoot the ground , even when I select other object to shoot. I have the ground at a rotation , it may affects the game? Should I add something to the main camera?
using UnityEngine;
public class Gun : MonoBehaviour
{
public float damage = 10f;
public float range = 100f;
public Camera fpsCam;
// Start is called before the first frame update
// Update is called once per frame
void Update()
{
if (Input.GetButtonDown("Fire1"))
{
Shoot();
}
}
void Shoot()
{
RaycastHit hit;
if( Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range))
{
Debug.Log(hit.transform.name);
}
}
}
picture ground

Can't find GameObject in new scene in DontDestroyOnLoad

I have a GameController in Scene1 and I made it DontDestroyOnLoad.
When I loaded Scene2, I'm trying to find a GameObject in Scene2 and the thing is obj2 is null but obj1 is not.
Why?
How could I find the GameObjectInScene2?
public class GameController: MonoBehaviour
{
private void Awake()
{
DontDestroyOnLoad(this);
}
private void Start()
{
SceneManager.LoadScene("Scene2");
GameObject obj1 = GameObject.Find("GameObjectInScene1");
GameObject obj2 = GameObject.Find("GameObjectInScene2");
}
}
Start is not called again since your component already ran it in Scene1
You can register to SceneManager.sceneLoaded and do the find in the callback instead.
I would also store the references global like
private GameObject obj1;
private GameObject obj2;
void Start()
{
// This first line just makes sure the listener isn't added twice
SceneManager.sceneLoaded -= OnSceneLoaded;
// Whenever a scene is loaded call OnSceneLoaded
SceneManager.sceneLoaded += OnSceneLoaded;
obj1 = GameObject.Find("GameObjectInScene1");
SceneManager.LoadScene("Scene2");
}
void OnDestroy ()
{
// Always clean up listeners when not needed anymore!
SceneManager.sceneLoaded -= OnSceneLoaded;
}
void OnSceneLoaded(Scene scene, LoadSceneMode mode)
{
if(scene.name == "Scene2")
{
obj2 = Find("GameObjectInScene2");
}
}

How to make an enemy stop when he enter the required distance to shoot

The problem that i have is that when the enemy enters the range to shoot it stops and never follows the player again even if the player gets out of the shooting range. To detect if the player entered the shooting range I have made a sphere collider for the enemy.
using UnityEngine;
using System.Collections;
public class EnemyWithRifleMovement : MonoBehaviour {
private GameObject player;
private NavMeshAgent nav;
private bool playerInRange = false;
private Rigidbody rb;
void Awake()
{
rb = GetComponent<Rigidbody>();
player = GameObject.FindGameObjectWithTag("Player").gameObject;
nav = GetComponent<NavMeshAgent>();
}
void Update()
{
if(playerInRange)
{
nav.SetDestination(transform.position);
}
else if(playerInRange == false)
nav.SetDestination(player.transform.position);
}
void OnTriggerEnter(Collider other)
{
if(other.gameObject.CompareTag("Player"))
{
playerInRange = true;
}
}
void OnTriggetExit(Collider other)
{
if(other.gameObject.CompareTag("Player"))
{
playerInRange = false;
}
}
}
Your code looks all fine, except there is one typo. Replace OnTriggetExit with OnTriggerExit, and it should work!
Also, in my opinion it is easier to get the distance between the enemy and the player by doing playerInRange = (Vector3.Distance(transform.position, player) <= range.