- Home /
Collision Box problem
I made a spaceship, and it doesnt travel that fast but when i have a collider on the spaceship, it still passes through the other box, because it travels so fast. Is there any way to make my own collision detection to teleport the object or stop it from passing through other objects? Yes, i have collision boxes on both. This is my ship controller code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ShipController : MonoBehaviour {
public Transform sCam;
public float x;
public float z;
public float y;
void Start () {
sCam = GameObject.FindGameObjectWithTag("SCamera").transform;
}
void Update () {
Vector3 vec = Vector3.zero;
vec.x = Input.GetAxis("Horizontal");
vec.z = Input.GetAxis("Vertical");
if (Input.GetKey(KeyCode.LeftControl))
{
vec.y -= 0.5f;
}
if (Input.GetKey(KeyCode.Space))
{
vec.y += 0.5f;
}
if (Input.GetKeyUp(KeyCode.LeftControl))
{
vec.y = 0;
}
if (Input.GetKeyUp(KeyCode.Space))
{
vec.y = 0;
}
x = vec.x;
z = vec.z;
y = vec.y;
vec = sCam.TransformDirection(x, y, z);
transform.Translate(vec.x, vec.y, vec.z);
}
}
Every frame, save your ship's position in a variable. In the next frame, before you save it again, you cast a ray from the saved positon (the "last position") towards the current one. You can also cast multiple rays if you like, or even a boxcast or whatever, depending on the collider of your ship. If the ray hit anything, you have your collision "in between frames". You can then set your ship's positon back to the hit.point, possible offset backwards by the ship's collider size.
Answer by vietoilaviet89 · Apr 30, 2018 at 04:50 AM
// Script from Daniel Brauer, Adrian
// http://wiki.unity3d.com/index.php?title=DontGoThroughThings
using UnityEngine;
public class DontGoThroughThings : MonoBehaviour
{
// Careful when setting this to true - it might cause double
// events to be fired - but it won't pass through the trigger
public bool sendTriggerMessage = false;
public LayerMask layerMask = -1; //make sure we aren't in this layer
public float skinWidth = 0.1f; //probably doesn't need to be changed
private float minimumExtent;
private float partialExtent;
private float sqrMinimumExtent;
private Vector3 previousPosition;
private Rigidbody myRigidbody;
private Collider myCollider;
//initialize values
void Start()
{
myRigidbody = GetComponent<Rigidbody>();
myCollider = GetComponent<Collider>();
previousPosition = myRigidbody.position;
minimumExtent = Mathf.Min(Mathf.Min(myCollider.bounds.extents.x, myCollider.bounds.extents.y), myCollider.bounds.extents.z);
partialExtent = minimumExtent * (1.0f - skinWidth);
sqrMinimumExtent = minimumExtent * minimumExtent;
}
void FixedUpdate()
{
//have we moved more than our minimum extent?
Vector3 movementThisStep = myRigidbody.position - previousPosition;
float movementSqrMagnitude = movementThisStep.sqrMagnitude;
if (movementSqrMagnitude > sqrMinimumExtent)
{
float movementMagnitude = Mathf.Sqrt(movementSqrMagnitude);
RaycastHit hitInfo;
//check for obstructions we might have missed
if (Physics.Raycast(previousPosition, movementThisStep, out hitInfo, movementMagnitude, layerMask.value))
{
if (!hitInfo.collider)
return;
if (hitInfo.collider.isTrigger)
{
hitInfo.collider.SendMessage("OnTriggerEnter", myCollider);
}
if (!hitInfo.collider.isTrigger)
myRigidbody.position = hitInfo.point - (movementThisStep / movementMagnitude) * partialExtent;
}
}
previousPosition = myRigidbody.position;
}
}
Add Rigidbody, Collider and you can try this script as a component for the spaceship. Also set fixed time steps in TimeManager to 0.005 to see the result effectively. Note that it may affect the performance.
Thanks, but i have one problem. When my spaceship moves downward (using control) it stutters and starts to fly upward on its own, how would i solve this? Btw thanks, it does work , thats the only problem i have. I would accept it, but right now its not behaving like it should
Well I think locking some constraints in Rigidbody may help. If the spaceship is going to be upward on its own, try lock Y axis so it wont itself fly without your control. Anyway, good to know it worked for you.