- Home /
Preventing glitching through colliders
I'm making an open world city game, and have began implementing planes to fly around. However the plane controller script makes the planes colliders be able to just glitch through other colliders. I'm pretty sure this is because I'm using this to move the plane in the direction its facing:
transform.position = transform.position + (transform.forward * currentForwardSpeed * Time.deltaTime * -1);
This line of code just makes the plane move forward, without accounting for any object in front, making it glitch through colliders. Is there different way of moving the plane so that it interacts with other colliders, while still being able to fly like it would in this script (I assume it would be physics based, and not simply changing the objects position, but I don't know how to do that)?
The solution may be simple, but I'm fairly new, and have only been working in 3d for a few weeks, so I don't have a clue what I'm doing.
Here is my full script just in case:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ShipController : MonoBehaviour
{
public float forwardSpeed = 25f, turnSpeed = 7.5f;
public float currentForwardSpeed;
public float forwardAccel = 2.5f, turnAccel = 2f;
collisionCheck check; //has bool isTouching to check if ship is touching any other colliders
public float lookRotateSpeed = 90f;
private Vector2 lookInput, screenCentre, mouseDistance;
void Start()![alt text][1]
{
check = this.gameObject.GetComponent<collisionCheck>();
screenCentre.x = Screen.width * 0.5f;
screenCentre.y = Screen.height * 0.5f;
}
// Update is called once per frame
void Update()
{
lookInput.x = Input.mousePosition.x;
lookInput.y = Input.mousePosition.y;
mouseDistance.x = (lookInput.x - screenCentre.x) / screenCentre.y;
mouseDistance.y = (lookInput.y - screenCentre.y) / screenCentre.y;
mouseDistance = Vector2.ClampMagnitude(mouseDistance, 1f);
transform.Rotate(mouseDistance.y * lookRotateSpeed * Time.deltaTime, mouseDistance.x * lookRotateSpeed * Time.deltaTime, 0f, Space.Self);
currentForwardSpeed = Mathf.Lerp(currentForwardSpeed, Input.GetAxisRaw("Vertical") * forwardSpeed, forwardAccel * Time.deltaTime);
this.GetComponent<Rigidbody>().useGravity = false;
transform.position = transform.position + (transform.forward * currentForwardSpeed * Time.deltaTime * -1);
if (Input.GetKey(KeyCode.A))
{
transform.Rotate(0, 0, turnSpeed * Time.deltaTime * -1);
}
if (Input.GetKey(KeyCode.D))
{
transform.Rotate(0, 0, turnSpeed * Time.deltaTime);
}
}
}
[1]: /storage/temp/189548-shipglitch.png
You will need to add a Rigidbody and Collider to the plane, though by wanting the plane to interact with world objects it means you will need to restructure the entire method for the planes flight, rather than simply moving forward (I personally have never worked with a flying object) though I can only assume it is similar to ground vehicles in the same sense that if you force the translation of the vehicle to move forward according to a button/mouse press it will always phase through colliders, however if you use propulsion/velocity on the wheels the grip of the wheels wont be 100% and crashing into a wall/building will simply make the wheels slip rather than forcing the vehicle through the wall/building, all I can suggest is make the plane have some sort of Rigidbody velocity, which again I have never worked with flying Rigidbodies so I am unable to provide an example, though I have worked with Physical Bullets that have an initial velocity upon release and slowly decrease in velocity over time, in the case of a Plane it may be similar however you will need to be constantly adding velocity to keep the plane flying and not slowly drop out of the sky
Answer by Pathojen · Dec 06, 2021 at 03:21 PM
I'm assuming what you want is to move forward automatically, and left and right from keyboard keys "a" and "d". would try using transform.Translate (whatever direction) instead of transform.position. Translate will react and collide with other colliders, so long as it is not kinematic. Something similar to this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Flight : MonoBehaviour
{
public float speed;
public float rotSpeed;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
transform.Translate(Vector3.forward * speed * Time.deltaTime);
transform.Rotate(0, Input.GetAxis("Horizontal") * rotSpeed * Time.deltaTime, 0);
}
}
Input.GetAxis("Horizontal)" and Input.GetAxis("Vertical") is automatically set to control things based on the a and d, and w and s keys respectively, as well as left and right, and up and down.
Your answer