I'm currently working on a project in Unity and as I'm fairly new to C# and programming in general I'm having a few issues. The first issue is with the collision detection for my player - I have a modelled Rigidbody with gravity and a mesh collider on (non convex) attached. For my walls, they're models imported with box colliders attached.
If I keep my box colliders the default size of the model (about twice as thick as the player) then the player can glitch through it sometimes and fly out the other side. To get around this I simply make the collider bigger for the walls / environment but this is not ideal if I want the player to be able to walk around or be on both sides of the wall. Currently the player "climbs" up the side of the wall when it collides, which should not happen? I was wondering if I have something configured wrong or if it would be better to manage collision for the player entirely through script? I have a screenshot of both my character and player components below: Wall - http://i.imgur.com/YRdTgSh.png? Player - http://i.imgur.com/DVKOdG1.png?
My second issue is when I try to check whether my player is grounded in order to let them jump or not. The code below is my current entire movement script for managing the player moving and jumping. At the moment, the player can jump infinitely high however if I spawn the player from a high space they will only jump once, until they reach a certain height which they will then jump again from repeated until space is let go.
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
// Update is called once per frame
void FixedUpdate() {
// Creating floats to hold the speed of the plater
float playerSpeedHorizontal = 4f * Input.GetAxis ("Horizontal");
float playerSpeedVertical = 4f * Input.GetAxis ("Vertical");
// Transform statements to move the player by the playerSpeed amount.
transform.Translate (Vector3.forward * playerSpeedVertical * Time.deltaTime);
transform.Translate (Vector3.right * playerSpeedHorizontal * Time.deltaTime);
// Calling the playerJump function when the jump key is pressed
if (Input.GetButton("Jump"))
{
playerJump();
Debug.Log ("Can jump");
}
}
/// Here we handle anything to do with the jump, including the raycast, any animations, and the force setting it's self.
void playerJump() {
const float JumpForce = 1.0f;
Debug.Log ("Should Jump");
if(Physics.Raycast(rigidbody.position, Vector3.up, collider.bounds.extents.y + 0.1f)) {
// Debug.Log ("Can jump");
rigidbody.AddForce (Vector3.up * JumpForce, ForceMode.VelocityChange);
}
}
}
If anyone could help I'd be very appreciative as I'm getting quite confused by this