problems with collisions on flight script
Hi Everyone,
This is my first time posting here, and I can say I am pretty desperate. I am struggeling with adding collisions to my (VR) player for a while now, and since I am quite fresh to unity and c#, I hope someone can help me out.
.
I have:
- player flying in a spaceship + rigidbody + movement script + sphere collider. So far so good.
- (VR)Camera that is a child of the player.
- objects that have colliders.
.
First I used Camera.main.transform.forward to move the player but I read that's basically a teleport and won't work. Though, with this I got the player to go forward in the direction they look, which is what I intend to do. So I swapped to rb.AddForce because I read that this will automatically allow the player to detect collisions. Bottomline: both methods do not give collisions (yet) and with rb.AddForce I can't get the player to go towards the direction the camera looks.
How can I get my player to collide with other objects? In my case it's completely destroying the gameplay, since I need collision triggers to change levels.
.
My script looks like this: (it might be a bit of a mess after 4 weeks of trying, all the previous tries are commented out)
.
public class InputTesting : MonoBehaviour {
// public CharacterController FPScontroller;
// private Vector3 movementVector = Vector3.zero;
private Vector3 movementVector2 = Vector3.zero;
float h = 1;
float v = 1;
private CharacterController m_CharacterController;
private CollisionFlags m_CollisionFlags;
public Rigidbody rb;
public float thrust;
// Use this for initialization
private void Start () {
m_CharacterController = GetComponent<CharacterController>();
rb = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
// previous tries //
// FPScontroller.Move(movementVector);
// Vector3 pos = FPScontroller.transform.position;
// pos += movementVector;
// FPScontroller.transform.position = pos;
// movementVector *= 0.1f;
Vector3 pos2 = rb.transform.position;
pos2 += movementVector2;
rb.transform.position = pos2;
movementVector2 *= 0.01f;
}
// Update is called once per frame
void Update () {
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
if (h > 0.01)
{
Debug.Log("UP pressed");
rb.AddForce(movementVector2 -= transform.forward * thrust *Time.deltaTime);
// previous tries //
// transform.localEulerAngles = new Vector3(Camera.main.transform.localEulerAngles.x, Camera.main.transform.localEulerAngles.y, Camera.main.transform.localEulerAngles.z);
// transform.forward = Camera.main.transform.forward;
// movementVector -= Camera.main.transform.forward *25 * Time.deltaTime;
}
if(h < -0.01)
{
Debug.Log("DOWN pressed");
rb.AddForce(movementVector2 += transform.forward * thrust * Time.deltaTime);
//previous tries //
// transform.localEulerAngles = new Vector3(Camera.main.transform.localEulerAngles.x, Camera.main.transform.localEulerAngles.y, Camera.main.transform.localEulerAngles.z);
// movementVector += Camera.main.transform.forward *25 * Time.deltaTime;
}
}
// an attempt to force collisions
void OnCollisionEnter(Collision collisionInfo)
{
print("Detected collision between " + gameObject.name + " and " + collisionInfo.collider.name);
print("There are " + collisionInfo.contacts.Length + " point(s) of contacts");
print("Their relative velocity is " + collisionInfo.relativeVelocity);
}
void OnCollisionStay(Collision collisionInfo)
{
print(gameObject.name + " and " + collisionInfo.collider.name + " are still colliding");
}
void OnCollisionExit(Collision collisionInfo)
{
print(gameObject.name + " and " + collisionInfo.collider.name + " are no longer colliding");
}
private void OnControllerColliderHit(ControllerColliderHit hit)
{
Rigidbody body = hit.collider.attachedRigidbody;
//dont move the rigidbody if the character is on top of it
if (m_CollisionFlags == CollisionFlags.Below)
{
return;
}
if (body == null || body.isKinematic)
{
return;
}
// SceneManager.LoadScene("level2");
body.AddForceAtPosition(m_CharacterController.velocity * 0.1f, hit.point, ForceMode.Impulse);
}
}
Your answer
Follow this Question
Related Questions
How Can I Know which collider has call OnCollisionExit() on the script's gameObject?? 1 Answer
Player cannot destroy enemy 1 Answer
[SOLVED] Problem with OnCollisionEnter2D script after upgrading to Unity 5.3(from Unity 4.6) 1 Answer
create object on raycast collision that follows raycast. 0 Answers
Unity 5: AddForce Increases power when already being pushed towards a collider. How to make stop? 1 Answer