How to Selectively Disable Sphere Colliders? - c#

I'm trying to build a pool game where the pot has a collider. Now on collision with a ball, I expect that particular ball that collided to disable.
The code below is what I've tried but it only allows me to set the ball manually. How do I automat and detect the right ball directly?
using UnityEngine;
using System.Collections;
public class pot: MonoBehaviour
{ //allows me to set collider
public SphereCollider ball;
void OnTriggerEnter(Collider other) {
Debug.Log("Ball potted");
ball = GetComponent<SphereCollider>();
ball.enabled = !ball.enabled;
}
}

Is
other.enabled = false;
what you are looking for?

It looks like your code is getting the sphere collider of the pot itself. Isn't "other" the ball in this case?
You can disable the "other" object (assuming it is the ball) with "other.gameObject.SetActive(false)" -- that's if you want the whole ball to disappear. If you just want it's collider to stop working then use "other.enabled = false".

public SphereCollider sphereCollider;
void Update()
{
if (Input.GetKeyDown(KeyCode.Q)) //or however you want to call it.
{
sphereCollider = gameObject.GetComponent<SphereCollider>();
sphereCollider.enabled = false;
}
}

Related

How could I make a collider in unity 3d not collide with my player, but still collide with raycasts? (new to unity!)

How could I make a collider in unity 3d not collide with my player, but still collide with raycasts? I have been using the built in layer Ignore Raycast to allow my raycasts to go through objects, but stop my player from going through.
You can also use the isTrigger or raycast layerMask option to do this. But I think this is a more sensible solution.
public class Player : MonoBehaviour
{
public Collider[] ignoreColliders = Array.Empty<Collider>();
public void Start()
{
var myCollider = GetComponent<Collider>();
foreach (var _ignoreCollider in ignoreColliders)
{
Physics.IgnoreCollision(myCollider, _ignoreCollider); // to ignore affect on player collider
}
}
}

Activating animation when within radius

I am trying to create a script for my enemy turret, but it is not going well. I have a couple animations of the turret being activated and deactivated. What I need is that based on the distance from the player, it plays either animation. So once it moves inside the detection radius it plays the activation animation and once it is outside it plays the deactivation animation. Most of the other ways I try require me to create an Animation Controller, which I have little experience in using. I want a simple way to play one animation once it is inside and play a different one when it is outside. I think there was a way to store the animation clip in the script, and then play it. I have attached my current script, so you know what I mean.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyTurret : MonoBehaviour
{
public GameObject Player;
public float DistanceToPlayer;
public float DetectionRadius = 75;
// Start is called before the first frame update
void Start()
{
Player = GameObject.FindGameObjectWithTag("PlayerTank");
}
// Update is called once per frame
void Update()
{
DistanceToPlayer = Vector3.Distance(transform.position, Player.transform.position);
if (DistanceToPlayer<=DetectionRadius)
{
Debug.Log("Within Radius");
}
if (DistanceToPlayer >= DetectionRadius)
{
Debug.Log("Outside Radius");
}
}
}
Calculating and checking the distance of the player for every update() is not ideal. It will work, but it will do more work than it needs to when the player isn't even near it. Its not efficient.
What you may want to do if your player is a Rigidbody, is add a SphereCollider to the turret, set isTrigger=true, set the radius to be your detection radius, and handle the OnTriggerEnter() and OnTriggerExit() events to play or stop animations.
You can also add two public Animiation objects to the script, drag and drop your animations in the editor, then you can use animation.Play() and .Stop() etc. to control the animations.
Something similar to this. Not tested fully, but you can get the idea.
public float detectionRadius = 75;
public Animation activateAnimation;
public Animation deactivateAnimation;
void Start()
{
SphereCollider detectionSphere = gameObject.AddComponent<SphereCollider>();
detectionSphere.isTrigger = true;
detectionSphere.radius = detectionRadius;
detectionSphere.center = Vector3.zero;
}
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "PlayerTank")
{
activateAnimation.Play();
}
}
private void OnTriggerExit(Collider other)
{
if (other.gameObject.tag == "PlayerTank")
{
deactivateAnimation.Play();
}
}
Your animations must not loop otherwise you will have to add more logic to check if animation.isPlaying and do your own animation.Stop() etc.

Button switching color in unity

I am very new to unity and coding in general, I am trying to create a button in unity that eventually opens a door in my game, but I immediatle ran into a problem, when I collide with the button it doesnt switch color, Im not sure if something is wrong with my code or my settings in unity
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Button : MonoBehaviour
{
[SerializeField]
GameObject switchOn;
[SerializeField]
GameObject switchOff;
public bool isOn = false;
void Start ()
{
gameObject.GetComponent<SpriteRenderer>().sprite = switchOff.GetComponent<SpriteRenderer>().sprite;
}
void OnTriggerEnter2D(Collider2D col)
{
gameObject.GetComponent<SpriteRenderer>().sprite = switchOn.GetComponent<SpriteRenderer>().sprite;
isOn = true;
}
}
I have 2 different button textures put on the on and off gameObjects a green one and a red one, does anyone know if something is wrong with the code
debug a line in your on collision enter, likely its not getting called. Make sure your player has a rigidbody and a collider and make sure your door has a collider. Also make sure trigger is checked on your player collider.

Unity3D: Having player light torches using ParticleSystem and OnCollisionEnter?

I'm trying to get my player to light torches in a scene, but not quite sure how to do this.
I have a torch prefab that has a particle system. Each time the player's torch collides into an unlit torch, I would like that torch to start burning.
I have been trying to follow the docs but have not been able to understand (https://docs.unity3d.com/ScriptReference/ParticleSystem.html, https://docs.unity3d.com/ScriptReference/ParticleSystem.Play.html).
Also have this question posted here: https://answers.unity.com/questions/1491419/having-player-light-torches-using-particle-system.html
My current code is below. I have each torch object tagged as torch, and my player tagged as Player. All particle systems, except the player's torch, have 'Play on Awake' off and prewarm on.
Any advice or tips?
Thanks!
/*
* Attach this script to all the torches. It will be used to start the fire
using OnCollision?/OnTrigger? See which is better
* Start with the particle effect/light being off, get all the components
* Turn the torches on when the player's torch collides with them
* 1.) Must make sure each torch object has a collider
* */
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class StartFire : MonoBehaviour
{
public GameObject torch;
public ParticleSystem fireParticleSystem;
bool lightOn;
void Start()
{
lightOn = false; //Start with the light off
fireParticleSystem = GetComponent<ParticleSystem>(); //get Particle System
torch = GetComponent<GameObject>(); //get Torch
}
/*
* if player's torch hits this torch (that is not lit)
* Turn on the fire
* Set the light being on to true
* */
private void OnCollisionEnter(Collision collision)
{
if(this.gameObject.tag==("torch") && collision.gameObject.tag==("Player") && lightOn==false)
{
fireParticleSystem.Play(); //start the particle system
lightOn = true;
}
}
}
1.Create your ParticleSystem and change the tag of its GameObject to "torch".
2.Attach BoxCollider to the SphereCollider to that GameObject with the ParticleSystem.
3.Mark the IsTrigger of the collider created from #2 to be true because it doen't make sense to collide with a touch. It seems like you just want to detect when the player is touching it.
4.The touch script should attached to the player instead of the touch. Use OnTriggerEnter to handle the detection and detect when player touches the touch-light then use GetComponent to get the ParticleSystem and play it. Stop the particle in OnTriggerExit.
If you actually want player to collide and be stopped by the touch then ignore #2 and also use OnCollisionEnter and OnCollisionExit instead of OnTriggerEnter and OnTriggerExit.
Attach to the Player:
public class ParticlePlayer : MonoBehaviour
{
void OnTriggerEnter(Collider other)
{
//Make sure player is touching a touch
if (other.CompareTag("torch"))
{
//Get ParticleSystem from the Gameobject the player collided with
ParticleSystem ps = other.GetComponent<ParticleSystem>();
//Play Particle
ps.Play();
}
}
void OnTriggerExit(Collider other)
{
//Make sure player is touching a touch
if (other.CompareTag("torch"))
{
//Get ParticleSystem from the Gameobject the player collided with
ParticleSystem ps = other.GetComponent<ParticleSystem>();
//Stop Particle
ps.Stop();
}
}
}
I've used the following code in some of my projects:
private ParticleSystem _particleSystem;
private ParticleSystem.EmissionModule _emissionModule;
private void Awake()
{
_particleSystem = GetComponent<ParticleSystem>();
_emissionModule = _particleSystem.emission;
_emissionModule.enabled = false;
}
private void OnCollisionEnter(Collision collision)
{
_emissionModule.enabled = true;
_particleSystem.Play();
}
I believe you're missing the emission module.

Move Object Along Raycast

I have made a Raycast that goes from my camera to the point of the object clicked. However, I am trying to make an object (in this case a bullet) to fly along the path of the ray. At the moment it flies straight forwards from the camera no matter where on the object you click because of the vector 3. How would I get it to follow the Ray?
C#
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RaycastShot : MonoBehaviour {
public Camera camera;
private Ray ray;
private RaycastHit hit;
public GameObject bullet;
private GameObject createBullet;
private Collider collider;
void Update () {
if (Input.GetMouseButtonDown (0)) {
ray = camera.ScreenPointToRay (Input.mousePosition);
createBullet = Instantiate (bullet, camera.transform.position, bullet.transform.rotation);
createBullet.AddComponent<Rigidbody>();
createBullet.GetComponent<Rigidbody>().AddRelativeForce (new Vector3(0, 1500, 0));
createBullet.GetComponent<Rigidbody>().useGravity = false;
collider = createBullet.GetComponent<Collider> ();
Destroy (collider);
if (Physics.Raycast (ray, out hit)) {
}
}
Debug.DrawLine (ray.origin, hit.point, Color.red);
}
}
You would want to use ray.direction property instead of (0,1500,0) as the direction of the force.
The add force should occur in FixedUpdate, and should only occur if the Ray hits something. Where you have it now is probably not the best spot.
Of course, make sure the bullet gets instantiated at the camera's location first.
Ray.direction gives you the vector3 direction of the ray object. If you need the distance at which it hit, you could also use ray.distance.
Edit: I'm near my computer now, so here's a more detailed answer relating to your comments.
First off: Here's the way I set up the test Project:
I Created a prefab bullet. This is just a sphere with a rigidbody, with my "BulletController" script attached to it. The point of prefabs is to avoid all of those lines where you have to add components. For testing purposes I set the rigibody to ignore gravity and its mass to 0.1.
Next, I created the BulletController script, which will be attached to the bullet prefab.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BulletController : MonoBehaviour {
Rigidbody rb;
public float bulletForce;
bool firstTime = false;
Vector3 direction;
// Use this for initialization
void Start () {
rb = GetComponent<Rigidbody> ();
}
public void SetDirection (Vector3 dir) {
direction = dir;
firstTime = true;
}
void OnCollisionEnter () {
//code for when bullet hits something
}
void FixedUpdate () {
if (firstTime) {
rb.AddForce (direction * bulletForce);
firstTime = false;
}
}
}
This script is is charge of controlling bullets. The (later on) script that will create the bullets doesn't really care what happens to them afterwards, since its job is just to create bullets. This BulletController script is in charge of dealing with bullets once they're created.
The main parts are the SetDirection method which tells the bullet which direction to travel in. Also it adds a one-time force in its FixedUpdate method that pushes it in the direction you just set. FixedUpdate is used for physics changes like adding forces. Don't use Update to do this kind of thing. It multiplies the force by a force that you set called "bulletForce".
Finally the BulletListener Script, which is simply attached to an empty game object in the scene. This script is in charge of listening for mouse clicks and creating bullets towards them.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BulletListener : MonoBehaviour {
public Camera mainCamera;
public BulletController bulletPrefab;
void Update () {
if (Input.GetMouseButtonDown (0)) {
//create ray from camera to mousePosition
Ray ray = mainCamera.ScreenPointToRay (Input.mousePosition);
//Create bullet from the prefab
BulletController newBullet = Instantiate (bulletPrefab.gameObject).GetComponent<BulletController> ();
//Make the new bullet start at camera
newBullet.transform.position = mainCamera.transform.position;
//set bullet direction
newBullet.SetDirection (ray.direction);
}
}
}
In the inspector for this empty game object, I added this script, and then dragged the camera, and the bulletPrefab into the appropriate fields. Be sure to drag the prefab from the FOLDER, not from the SCENE. Since this will use the prefab, not an object in the scene.
Now click around and you'll see the bullets flying! Note that using a low force is good to test, and then increase it later.
The main things to take away from this is to split up your logic. A script should only be in charge of one thing. For example, your enemies might also fire bullets. You can now reuse your bulletController script for those bullets as well. Also, say you have different sized or shaped bullets, you can just drag the bulletcontroller script onto the different prefabs you've made for your bullets. This will not affect your listener script which will still create bullets where you click.
If you have the end point then you can move along the vector with MoveTowards:
Vector3 target = hit.point;
StartCoroutine(MoveAlong(target));
private IEnumerator MoveAlong(Vector3 target){
while(this.transform.position != target){
this.transform.position = MoveTowards(this.transform.position, target, step);
yield return null;
}
}

Categories