- Home /
C# Custom Collision Script (solved)
Okay, to clarify some things, I absolutely hate using Rigidbody physics. My disliking of them is a result of bugs, as the built-in physics engine is not optimum for 2.5d dungeon crawlers, at least from my experiments. I'm trying to write a custom collision engine to work around these bugs, so that it will be much more suited for what I'm trying to accomplish.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Physics3D : MonoBehaviour {
public bool gravEnabled;
float vSpeed;
public float gravity = 3.5f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update(){
// Enable or disable gravity
GravityEnabled (true);
}
// Check if gravity is enabled
bool GravityEnabled(bool enabled){
if (enabled == true)
Gravity ();
return enabled;
}
// Gravity function
void Gravity(){
transform.Translate(Vector3.down * vSpeed * gravity * Time.deltaTime, Space.World);
if (Physics.Raycast(transform.position, Vector3.down, 1)) {
vSpeed = 0;
} else {
vSpeed=1.5f;
}
}
// This is where I need help
void OnCollisionEnter(Collision other){
foreach (ContactPoint c in other.contacts) {
if (transform.position.z == c.thisCollider.transform.position.z - 0.5f) {
// Block the direction
} //else if(other collision stuff)
}
}
}
Now, this may be relatively simple, but simple is all I need. The question is, how can I have it correctly block directions without disabling the entire axis? (If the object that was blocking was to the left, the player could not walk walk to the right in my previous version of this script)
there are many games/situations that are better suited with custom hand written physics or components, but i'm not sure your situation necessitates replacing unity's physics... when set up properly they can work well bug free.
Anyways with the script you are showing you are definatly using unity's rigidbody components and unitys collider system. If you are going to use thier collision system then your movement script (that we cant see) should simply use velocity so stops are automatic.
Answer by ollie_payne · Sep 04, 2017 at 05:37 AM
If you're looking to block directions (presumably when you walk into a wall, object, etc.), you may want to look into using a LayerMask within you Raycast, as they make it so the Ray will only return true if it hits an object on a certain layer.
For instance create a Layer called "Solid" (or something similar) and change your Raycast to this:
if (Physics.Raycast(transform.position, Vector3.down, 1, 1 << LayerMask.NameToLayer("YourLayerName")))
{
vSpeed = 0
}
else
{
vSpeed=1.5f;
}
This should help you if I am understanding your issue correctly. Regardless, would love to know what works for you once you get it working.
That's similar to what I need. The main issue here is that blocking one direction (setting the speed for that axis to 0) will not allow the player to move the other direction.
Ok, I see. Alternatively you could store the direction of the Raycast within a Vector3 variable, and change the direction when you press a movement key. Hence "colliding" only in the direction of movement.
I'll try that out, and post results. Sounds pretty solid
I tried it, though I don't believe it will work properly, mainly because of the inaccuracy of the collisions. I may use built in if worst comes to worst.
Originally, it was how hard it is to capture the "classic style" with it, as well as a couple bugs where gameobjects would fly across the map, and never stop. (probably shouldn't be developing it around that type of thing... but, you know.) Now that I've been working with it thought (increasing drag, disabling some of the axes), it's starting to show its strengths.
That's surprising as that should work. What is it exactly that's putting you off using the built in collision system?