I made a fps with a gun and it has a particles system as effect for the gun shot. The problem is that the effect multiply and falls down. I don't know if I can control this from the effect inspector or the script of spawning projectiles
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();
}
}
Related
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);
}
}
My collision detection is not working! Debug message wont pop up when I collide with a game object with the tag "Enemy". Does anyone know what the issue is? Please help
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
//using UnityEngine.TextMeshPro;
using TMPro;
public class PlayerController : MonoBehaviour
{
public float speed = 3;
public float jumpPower = 1.5f;
private Rigidbody2D rigidBody;
// Start is called before the first frame update
void Start()
{
rigidBody = GetComponent<Rigidbody2D> ();
}
// Update is called once per frame
void Update()
{
if(Input.GetKey(KeyCode.A))
transform.Translate(-.1f, 0.0f, 0.0f);
if(Input.GetKey(KeyCode.D))
transform.Translate(.1f, 0.0f, 0.0f);
}
**void OnCollisionEnter(Collision col){
if(col.gameObject.CompareTag("Enemy")){
Debug.Log("CONTACT");**
}
}
}
Not sure if this already solves your issue but:
The issue might be caused by you moving an Object with a Rigidbody2D component by using the transform component. This breakes the physics and collisions might not get called.
Rather always go through the Rigidbody2D component and use Rigidbody2D.MovePosition in FixedUpdate like
bool keyA;
bool keyD;
void Update()
{
if(Input.GetKey(KeyCode.A)) keyA = true;
else if(Input.GetKey(KeyCode.D)) keyD = true;
}
private void FixedUpdate ()
{
if(keyA)
{
keyA = false;
rigodBody.MovePosition(rigiBody.position - Vector3.right * 0.1f);
}
else if(keyD)
{
keyD = false;
rigidBody.MovePosition(rigidBody.position + Vector2.right * 0.1f);
}
}
In general your Rigidbody should be isKinematic in order to work
Note: MovePosition is intended for use with kinematic rigidbodies.
ImageCamera
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MainGame : MonoBehaviour
{
public float speed;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved)
{
Vector2 touchDeltaPosition = Input.GetTouch(0).deltaPosition;
transform.Translate(-touchDeltaPosition.x * speed, -touchDeltaPosition.y * speed, 0);
}
}
}
I made a 3D game and I want my camera to stay in the a fix position and just rotate, this is the script that I used. What do I need to add to fix the camera and just rotate?
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
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.