- Home /
Manual Collision Detection...
I have a basic cube that moves a certain space when a movement key is pressed. However, since I am using direct Transform variables, there is no Player Controller involved, therefore no collision. However, what I intend to do is create a surface that the player cannot move passed. How can this be achieved? I already thought of an idea that involves recording the previous position and resetting the current position as the previous position every time the player hits the wall. But that is not effective... What are your views? Thanks in advance!
$$anonymous$$y script is set up the way I already set it up - I am not in the mood to write one that uses Character Controllers :P Sorry, but I am lazy and a little bit of a program$$anonymous$$g newbie - I work with Java, and not UnityScript, and I am kinda new to Unity3D.
I don't think adding a Rigidbody for detecting collisions will be more time consu$$anonymous$$g than implementing a manual collision detection.
Yeah, but only problem is, I have attached a Rigidbody, but my movement still goes through walls, because as I said, my script is designed to move a direct amount of spaces to the desired direction, and is not intended to implement Rigidbody collisions, so my character ends up juttering annoyingly, and even if I freeze all rotation, it still penetrates walls.
If you want collisions, don't modify the transform position variable directly. Use a rigidbody or a character controller. This is the best (and probably only) advice you will get.
There is no easy solution. The easy way is to use a rigidbody, and not modify the transform variable directly. If you tell a transform to position somewhere, it will, no matter what the collider setup.
As stated above, writing a complex collision detection system is just making a huge amount of work for yourself. Using a rigidbody and modifying the velocity variable (or a character controller with move) is much easier.
Answer by SchecterKing · Jul 09, 2013 at 08:44 PM
Try to do something like what I explained here with the transparent boxes and using them as triggers in code. http://answers.unity3d.com/questions/489848/how-to-improve-the-performance-of-removing-trees-d.html
Answer by Alturis2 · Dec 04, 2013 at 03:36 AM
Funny I was just trying to get pretty much the exact same thing going. I briefly attempted to use rigid bodies but that is just way more beef than the problem requires. Physics.RaycastAll is the answer I believe.
Try this:
// Update is called once per frame
void Update ()
{
bool up = Input.GetKeyDown(KeyCode.W);
bool right = Input.GetKeyDown(KeyCode.D);
bool left = Input.GetKeyDown(KeyCode.A);
bool down = Input.GetKeyDown(KeyCode.S);
Vector3 move = new Vector3( 0, 0, 0 );
Vector3 nmlMove;
if (up)
move.z += 1;
if (right)
move.x += 1;
if (left)
move.x -= 1;
if (down)
move.z -= 1;
if ( move.magnitude > 0 )
{
nmlMove = move;
nmlMove.Normalize();
RaycastHit[] hits;
hits = Physics.RaycastAll( transform.position, nmlMove, move.magnitude );
if ( hits.Length == 0 )
transform.Translate( move );
}
}
Your answer
Follow this Question
Related Questions
Trying to make a procedurally generated dungeon (3D), but rooms won't spawn 1 Answer
chronological order of collision to give a winner unity 2d 4.3 1 Answer
Collision Enter one of the objects ? 1 Answer
Weapon System with collide detection (Helps with script pls)!!! 0 Answers
How to detect when an object goes past (so it looks like it touches) a UI element. 1 Answer