I have two concentric Box Colliders on same GameObject. Outer Box Collider rotates the object (on swiping the screen) and inner Box Collider plays animation when we touch the screen.
But when ray goes from my mobile screen to outer Collider it destroys and does not pass through that collider.
Is there any way to do it?
You could make the outer one a Trigger Collider by checking Is Trigger in the inspector:
and have the inner collider as just a collider. Then you can check the outer one with OnTriggerEnter and the inner one with OnCollisionEnter.
Alternatively you could give them different Tags and check for each Ray hit by checking for the tag (use OnTriggerEnter for this).
void OnTriggerEnter (Collider other)
{
if (other.gameObject.tag == "Inner Cube")
{
// We have hit the inner cube
}
else if (other.gameObject.tag == "Outer Cube")
{
// We have hit the outer cube
}
}
Looking at what you want to do in your question though, using 2 concentric colliders is not as good as simply detecting which input action the user performs (tap or swipe) and acting according to that.
Take a look at Physics.RayCastAll. With RayCastAll your Ray won't return just the first collision but an array of all collisions.
RayCastHit[] hits;
Ray ray = camera.ScreenPointToRay(Input.mousePosition);
hits = Physics.RaycastAll(ray);
// If you have objects behind your object then sort the hit array by distance:
// hits = hits.OrderBy(l => l.distance).ToArray();
for(int i = 0; i < hits.Length; i++)
{
RaycastHit hit = hits[i];
// From here you can use a tag approach similar to what Flaming Zombie posted to check whether the collision object supports swipes or touches.
// Once you find the first valid object then you can perform your action and break out of this loop.
}
However, there are a couple of things to note with this approach:
You will need to loop through each collision.
You will need to determine whether or not the user swiped or touched (you probably already have this down)
Colliders behind the GameObject will also return collisions.
If there is not a specific reason you are using two colliders then I would do what Flaming Zombie suggested and use a single collider. Here is a simple implementation:
Component on collider:
public class ExampleComponent : MonoBehaviour
{
public void OnInteract(bool isSwipe)
{
if (isSwipe)
{
//Rotate
}
else
{
//Animate
}
}
}
Raycast logic:
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
ExampleComponent c = hit.transform.GetComponent<ExampleComponent>()
if(c != null)
{
c.OnInteract(isSwipe); // You'll need to implement isSwipe
}
}
Related
I'm currently trying to have a raycast detect the player caracter to have an event happen when a specific key is pressed here is the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class action: MonoBehaviour
{
public float dist;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
RaycastHit2D down = Physics2D.Raycast(transform.position, -Vector2.up, dist);
Debug.Log(transform.position);
if (Input.GetKeyDown(KeyCode.Z) == true && down.collider != null)
{
Debug.Log("hi");
}
}
}
I'm basicly trying to make a small raycast,which is why I place a distance instead of letting it go infinitely, and have it so if the raycast is triggered and the player presses z the event happens(which for now is the debug console), but all it does now is triggers whenever I press z no matter where the player caracter is. I tried drawing the raycast but I can't seem to make it work.
I think the reason for that is that the raycast is hitting the object containning the script it self and so the collider is allways non null try adding a LayerMask that differs from the GameObject's Layer
so it's something like this :
RaycastHit2D down = Physics2D.Raycast(transform.position, -Vector2.up,
dist , 1 << /*The Target's Layer*/);
You can find the layer in the up left corner of your inspector when you click on a GameObject
You have not provided a LayerMask on the down. The Raycast must be colliding with the player's Collider. In case you don't know what LayerMask is.
LayerMask can be used to detect Raycast that collides with only the objects with that layer on it.
The ayer Dropdown, on each GameObject, refers to the layer of the object as shown:
Head to the Unity Documentation for LayerMask for more details.
If you want make raycast when press Z will be good to do it in time press:
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Z)){
RaycastDown()
}
}
private void RaycastDown()
{
Debug.Log("Player position: " + transform.position);
RaycastHit2D hit = Physics2D.Raycast(transform.position,
Vector2.down, dist);
if(hit.collider != null){
Debug.Log($"Hit object: {hit.transform.name}" );
}
}
And about issue, make sure that hit object has a collider or collider is active.
Possibly problem in raycast direction. To check it, use Debug.DrawRay, it`s help you to visualize a ray which you draw
I have two objects: a player and an enemy. The main problem is the enemy, which should start shooting at the player when it approaches him at a certain distance (in short, just start the animation).
At first I tried to implement this through Vector 3, as with other ordinary opponents. But he is as stupid and crutch as possible, however, you yourself can see everything in the pinned.
I started to implement it more allegedly correctly, through the trigger and the collider (box collider) of the enemy itself. And everything works right as it should, but there is a nuance. The enemy also has boxing implemented through the box collider, the player, hitting which, causes damage to him. There is only one box collider for these two tasks, and since I had to increase this collider so that the enemy could stop in front of the player at a certain distance. Because of this, the player can hit at a great distance (at which the enemy can shoot) from the enemy and the enemy takes damage anyway.
I tried to make a separate object the size of the enemy himself and use it as a box to receive damage. Then he already transmits data about receiving damage to the enemy object. But this does not work, all links between scripts and objects are made, but he does not want to transfer data. That is, simply making two colliders for different tasks does not work.
In general, here my powers are all. I searched the entire Internet, but I did not find how to implement this mechanic in a different way so that it does not conflict with others. Therefore, I ask the help of the local experts, where I screwed up.
private Animator ch_animator;
// Start is called before the first frame update
void Start()
{
myAgent = GetComponent<NavMeshAgent>();
myAnim = GetComponent<Animator>();
EnemyH = GetComponent<GDHealth>();
}
// Update is called once per frame
void Update()
{
dist = Vector3.Distance(/*checker.*/transform.position, target.transform.position);
if (dist > range)
{
myAgent.enabled = false;
myAnim.SetBool("Idle", true);
myAnim.SetBool("Move", false);
myAnim.SetBool("Attack", false);
}
if (dist <= range & dist > atRange)
{
myAgent.enabled = true;
myAgent.SetDestination(target.position);
myAnim.SetBool("Idle", false);
myAnim.SetBool("Move", true);
myAnim.SetBool("Attack", false);
}
if (dist <= atRange)
{
StartCoroutine(Attack());
}
if (PlayerH._health <= 0)
{
atRange = 0;
}
if (EnemyH._health < 0)
{
myAgent.enabled = false;
}
}
public IEnumerator Attack()
{
yield return new WaitForSeconds(0.5f);
myAgent.enabled = false;
myAnim.SetBool("Idle", false);
myAnim.SetBool("Move", false);
myAnim.SetBool("Attack", true);
}
void OnTriggerStay(Collider col)
{
if (col.tag == "Player")
{
//gameObject.GetComponent<Animator>().SetBool("Attack", true);
StartCoroutine(Attack());
transform.LookAt(col.transform.position);
transform.eulerAngles = new Vector3(0, transform.eulerAngles.y, 0);
}
}
void OnTriggerExit(Collider col)
{
if (col.tag == "Player")
{
myAgent.enabled = true;
myAgent.SetDestination(target.position);
myAnim.SetBool("Idle", false);
myAnim.SetBool("Move", true);
myAnim.SetBool("Attack", false);
//gameObject.GetComponent<Animator>().SetBool("Attack", false);
}
}
But this does not work, all links between scripts and objects are made, but he does not want to transfer data.
From this description it could be anything: wrong layers, no rigidbody on either of objects, misstyped tags in OnTriggerStay method.
In my project, I successfully created 2 colliders for 2 separate tasks, so this is how I would see it in your problem:
Use two colliders
Attach one collider with one script to the enemy object - this collider should have a size of the enemy. The OnTriggerStay method here should deal damage to the enemy, check for death, etc.
Create child object to the enemy. Attach new collider to it with the size of enemy's attack range. Attach a script with OnTriggerStay method that will stop enemy and begin ranged attack (or whatever you want to do).
If this doesn't work: check collision matrix or try adding a kinematic rigidbody to either of objects.
Measure distance between player and the enemy in update (which you are already doing) and apply necessary code based on distance (stop or attack) thus replacing one of the colliders.
Hope that helps!
It's like a mario game. The player is jumping around and has to collect some items.
The problem is that my ray isn't colliding with the item box colliders.
I need the ray to know, so i can destroy the right item that the player has collided.
void OnCollisionEnter2D(Collision2D colisor)
{
if((colisor.gameObject.name == "floor" || colisor.gameObject.name == "floor2" || colisor.gameObject.name == "floor3"))
{
anim.SetBool("jump", false);
anim.SetFloat("speed", 0);
}
if (colisor.gameObject.name == "space(Clone)")
{
RaycastHit hit;
Ray ray = new Ray(player.position, transform.right);
Debug.Log("hit1");
if (Physics.Raycast(ray, out hit))
{
BoxCollider bc2d = hit.collider as BoxCollider;
Debug.Log("hit2");
if (bc2d != null)
{
Destroy(bc2d.gameObject);
}
}
}
}
You're mixing 3d and 2d physics; Physics will only look for 3d objects, so you should be using Physics2D instead. This raycast may still fail if the cast starts inside the target, because the surface normals point in the wrong direction.
Also note that since you already have the Collision2D, you can just grab the otherCollider and shouldn't need to raycast in the first place.
Physics.Raycast isn't working with 2D objects. Instead you need to use Physics2D.Raycast or Graphic Raycaster.
Raycast2D - https://docs.unity3d.com/ScriptReference/Physics2D.Raycast.html
Graphic Raycaster - https://docs.unity3d.com/eng/current/Manual/script-GraphicRaycaster.html
Goal: Detect when the cursor has entered a defined radius of the player.
Hello, I am in the process of trying to replicate the combat system from a game called CrossCode. The idea is that when the cursor is within a certain radius of the player, I will be able to switch to melee combat, and back to ranged once the cursor leaves this radius.
I have implemented one way I thought it could be done, however it feels slow or unreliable and I just wanted to know if there are any other methods I could possibly look into to achieve a smoother result.
Here is what I've done
attached to the player
void Update()
{
attackStyleSwitchRadius = colRef.radius;
playerCenter = colRef.transform.position;
if(Physics2D.OverlapCircle(playerCenter, attackStyleSwitchRadius, cursor))
{
meleeMode = true;
rangeMode = false;
}
else
{
meleeMode = false;
rangeMode = true;
}
}
And on a small 2D object I have this script so that it follows the cursor position.
void Update()
{
pos = Input.mousePosition;
gameObject.transform.position = Camera.main.ScreenToWorldPoint(pos);
}
when the small object enters the overlap circle it changes the bools around.
You can remove the collision detection overhead by doing something like this instead;
void Update ()
{
attackStyleSwitchRadius = colRef.radius;
playerCenter = colRef.transform.position;
var mouse = Input.mousePosition;
mouse.z = Vector3.Distance(Camera.main.transform.position, playerCenter);
var range = Vector2.Distance(Camera.main.ScreenToWorldPoint(mouse), playerCenter);
var inside = range < attackStyleSwitchRadius;
meleeMode = inside;
rangeMode = !inside;
}
Cursor
Make an object, name it cursor.
Add a small collider to the cursor object.
Add a script to the cursor object so its always at the mouses location.
Melee range zone
Add a GameObject as child of the player, name it MeleeRangeZone.
Add a collider to it, set it to be a Trigger. The size of this collider will be the players melee range,
Add a rigidbody to it so that collisions can be detected, but set the rigidbody to not rotate or change its position.
Add a script to the object and use the OnTriggerEnter and OnTriggerExit methods to detect whether or not the cursor has entered your melee zone.
You can now use the OnTriggerEnter and OnTriggerExit methods to switch between the players attack modes, meaning that when the cursor enters it changes to melee and when it exit it changes to ranged.
You can fire the ray to detect the location the cursor should have like this:
public LayerMask RaycastCollidableLayers;
public RaycastHit Hit;
public float CheckDistance = 200f;
public Transform Cursor;
void PerformRaycast(){
//Set up ray
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
//Fire ray
Physics.Raycast(ray, out Hit, CheckDistance + 0.1f, RaycastCollidableLayers);
if (Hit.collider == null)
{
//Debug.Log("Raycast hit nothing");
return;
}
else //Ray hit something
{
//Move cursor to Hit.point;
Cursor.position = Hit.point;
}
}
I have a 2.5d type game, with falling blocks (like tetris) and orthographic projection setup (I've setup my game as "3D" type).
I've defined a block like this:
public class Block{
public Block () {
this.gameObj = GameObject.CreatePrimitive (PrimitiveType.Cube);
}
public GameObject gameObj;
}
I have a BoardMgr (GameObj + script only component), where I spawn these blocks and store them in an array:
public class BoardMgr : MonoBehaviour {
protected Block[] blocks;
protected Block[,] board;
}
In BoardMgr::update(), blocks are falling down one after the other (like tetris). Now, I'd like to figure out when I click on a block, which block object is it. This is the click detection code:
if (Input.GetMouseButtonDown(0)) {
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast (ray, out hit)) {
Debug.Log ("Ray hit block");
// How do I find which block got hit here ?
} else {
Debug.Log ("Ray missed block");
}
}
When I click on a block, I do see the ray hit block on console, but then, how do I access which "Block" object got hit ? From RayCastHit, hit object, how do I decode which Block does it reference ?
I'm new to unity (2 days old) but not new to gamedev. Trying to find my way through unity here. I'd appreciate if someone can point me in the right direction here.
//Check the GameObject by name
if (hit.collider.name == "brainydexter")
{
Debug.Log("Hit: "+hit.collider.name);
}
//Check the GameObject by tag
if (hit.collider.CompareTag("brainydexterTag"))
{
}
//Check the GameObject by GameObject instance
GameObject otherGameObject = gameObject;
if (hit.collider.gameObject == otherGameObject)
{
}
EDIT: This is what you need
Use array to loop through the blocks then compare the gameobject instance.
for (int i = 0; i < blocks.Length; i++)
{
if (hit.collider.gameObject == blocks[i].gameObj)
{
Debug.Log("Block hit is " + blocks[i].gameObj);
break;
}
}
In your if block use the hit parameter to detect which object was hit , because it has the information what the ray collided with , example hit.collider.tag == "myBlock"
I like your question. And I think I got the key of what make the above 2 answers be not you want.
Usually, unity developers want to make this kind of block, they will create a cube and add script as element to it to make a prefab.
However, What you do to define a block is more programmer, creating a C# class with a primitive cube.
I think one way is to check the block's position.x and z with the position of each block in the array .because the blocks are dropping, then just y is changing, to check x and z is a solution.
Hope help.