- Home /
Glitching through ceiling when spawning blocks under the player
In a magic based game I am using a somewhat unconventional flight script that works through spawning invisible cubes under the player and destroying them soon after. The only problem with this is that it causes the player to glitch through the ceiling of the chamber. In what ways can this be avoided? I was thinking of finding a way to test distance to the ceiling, but I am unfamiliar with how to do this. I am also open to other suggestions.
It might be a little easier to just learn how to work with a CharacterController. Instantiating things like that can be costly.
Answer by Wolfrik_Creations · Jun 03, 2016 at 06:31 AM
Here you go.
var distToRoof : float;
function Update(){
var temp : RaycastHit;
Physics.Raycast(transform.position, Vector3.up, temp);
distToRoof = Vector3.Distance(temp.point, transform.position).y;
}
@Wolfrik_Creations When I use this code, I get an error saying "'y' is not a member of 'float'" how can I fix this?
Oh sorry, I thought Vector3.Distance returned a Vector3.
At the end of line 6 remove ".y".
It should be this
distToRoof = Vector3.Distance(temp.point, transform.position);
Unfortunately, that doesn't work. the player will still glitch through the ceiling. $$anonymous$$y current code looks like this:
var distToRoof : float;
var flying = false;
function Update ()
{
var temp : RaycastHit;
Physics.Raycast(transform.position, Vector3.up, temp);
distToRoof = Vector3.Distance(temp.point, transform.position);
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.Space))
{
if (flying == true)
{
if (distToRoof < .28)
{
Debug.Log ("Flight Attempted");
flightTimer ++;
var fly = Instantiate(flightPrefab, transform.position, transform.rotation);
Destroy(fly.gameObject, 2);
Debug.Log ("Flight Successful");
}
}
}
}
When I start the level, and the ceiling is immediatly overhead, distToRoof registers as .28 For whatever reason, when the player is farther down from the ceiling, the distToRoof decreases by a small amount ins$$anonymous$$d of increasing.
Thanks for trying to help, although I could not get your suggestion to work. I found my own simple way of doing it. I put an invisible cube above the player which, on trigger enter, would change a specific variable disabling flight. So, if the player ever got too close to the ceiling, the cube's code would trigger and stop them from going higher.
Oh, sorry my solution didn't work.
If you want, you could click "$$anonymous$$ore..." next to your comment and turn it into an answer.
Your answer
![](https://koobas.hobune.stream/wayback/20220612082203im_/https://answers.unity.com/themes/thub/images/avi.jpg)