- Home /
basically i am having collider issues and idk wat to do
so essentially i have code to detect a collision and then if its tag is equal too a building then it goes to that building only problem being that some buildings have box colliders that i use for radiuses to collect resources and it seems whenever it detects this radius then it just ignores the soon collision with the actual building here is my code unsure of what is wrong with it thanks for the help!!!`
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class Colliderai : MonoBehaviour
{
public float lookRadius = 10f;
NavMeshAgent agent;
public Transform target;
public GameObject radiussawmill;
public GameObject radiusquarry;
public GameObject radiusoilrig;
public void OnCollisionEnter(Collision collision)
{
print("touch");
if (collision.gameObject.name == "prefabsawmill" )
{
target = collision.gameObject.transform;
Target();
}
else
{
Physics.IgnoreCollision(radiussawmill.GetComponent<Collider>(), GetComponent<Collider>());
}
}
private void Start()
{
agent = GetComponent<NavMeshAgent>();
}
void OnDrawGizmosSelected()
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(transform.position, lookRadius);
}
void Target ()
{
float distance = Vector3.Distance(target.position, transform.position);
if (distance <= lookRadius)
{
agent.SetDestination(target.position);
if (distance <= agent.stoppingDistance)
{
Vector3 direction = (target.position - transform.position).normalized;
Quaternion lookRotation = Quaternion.LookRotation(new Vector3(direction.x, 0, direction.z));
transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, Time.deltaTime * 20f);
}
}
}
}`
for some odd reason my code snippet glitched a little sorry for the inconvenience! :3
Fixed. Next time, try using the button with 101010 on it
You can edit your post to fix the formatting. What is the purpose of Physics.IgnoreCollision? I am a little confused about what you want. You are checking if a collision occurs with prefabsawmill. And some of those objects have colliders that you don't actually want to collide with?
That was one of my attempts to ignore the collision with the radius but otherwise I am trying to get it to ignore it but if there is another issue then please tell me
Answer by Taylor-Libonati · Mar 28, 2019 at 12:40 AM
I think you need to move those colliders to a different transform. For example you could make a child on the building that is just for the resource collection collider and set its layer to something else. That way you know when you are colliding with the building in one way verse the other. I suggest looking at Unity Collision layers. With this you can have your ColliderAI not collide with anything but what its interested in: https://docs.unity3d.com/Manual/LayerBasedCollision.html
Your answer
Follow this Question
Related Questions
Help with some collision mechanics and movement for my game. 0 Answers
My Character is unable to move since booting up Unity this morning, worked fine last night 0 Answers
Moving a 2D character along the X and Z axis only. 1 Answer
How to apply equal force back to player when colliding with a wall? 2 Answers
SteamVR - Collider marked as isTrigger won't pass the player through. 0 Answers