- Home /
Dive collide
Right now the character jumps and dives, but I need to figure out when he hits a wall or the ground. I can't check for any collider because I don't want the dive to stop if he hits a small object. Without tags, can anyone think of soem good ways to find walls?
Answer by clunk47 · May 23, 2013 at 06:18 PM
void OncontrollerColliderHit(ControllerColliderHit hit)
{
if(hit.collider.gameObject.name == "Wall")
{
DoSomething()
}
}
OR
RaycastHit hit;
void Update()
{
if(Physics.Raycast(transform.position, transform.forward, out hit, 1f)
{
if(hit.collider.gameObject.name == "Wall")
{
DoSomething();
}
}
}
Not really understanding your question, but try checking out the Script Reference for Raycast and ControllerColliderHit
I wanted to do it without any kind of "tag" but I think this will work out. I need to label my stuff anyways. How can I add multiple names and make it more efficient? Would this work?:
col = hit.collider.gameObject.name
if(col == "Wall" || col == "Ground" || col == "Crate" || )
{
DoSomething();
}
I would like to make a table to store and continuously add on names to this list but I'm not sure how to go about that in this language. Is it possible?
Yes that would work, except take out that last || there.
col = hit.collider.gameObject.name
if(col == "Wall" || col == "Ground" || col == "Crate")
{
DoSomething();
}
I guess || is OR, kinda how && is AND. If || doesn't work, try | I'm not sure what the difference is, I can never find any good documentation on the vertical bar character lol
You could also do an array, this example is in C-Sharp.
using UnityEngine;
using System.Collections;
public class test : $$anonymous$$onoBehaviour
{
string[] names;
void Awake()
{
names = new string[]
{
"Wall", "Ground", "Crate"
};
}
void OnControllerColliderHit(ControllerColliderHit hit)
{
foreach(string name in names)
{
if(hit.collider.gameObject.name.Equals(name))
{
}
}
}
}
Answer by Bunnybomb7670 · May 24, 2013 at 11:12 AM
Look at other games and see how then do it. Looking at something like call of duty, where you can dive, if you dive at a wall, you literally collide with it and slide down the wall, just make it so that when you dive, if you have an animation, you should add a collider around the diving animation object so that if it dives into a wall, it will slide down.
I know what my plans are, all I needed to know was a good method of detecting a collider, but the best method after all will be giving it a tag.
Your answer
Follow this Question
Related Questions
Player is moving through walls when force is added from a knockback script? *UNANSWERED* 3 Answers
How to make player (sphere) shoot the ball (another sphere)? 2 Answers
Click-to-move questions 2 Answers
How to make a GameObject that collides but the player can go through it? 2 Answers
How to let the player collide with animated objects? 0 Answers