- Home /
3D cube box collider not working correctly
In the scene I'm working on I've got a game object acting as the player and a 3D cube to act as an invisible wall so that the player cannot leave the level area. However, I'm running into some issues in which the player object happily passes through the cube which has a box collider set. Please see the links below for how both of the objects have been configured:
Player object: https://www.dropbox.com/s/oystjc34rne1ac9/player-configuration.jpg?dl=0 Cube object: https://www.dropbox.com/s/f5dn0ucv6kcwuy8/wall-configuration.jpg?dl=0 Issue demonstration: https://www.dropbox.com/s/vmorg7ielxz4tqp/issue-demonstration.mp4?dl=0
If anyone could give me insight as to why I'm getting a different result that what I'd like to achieve, then that would be great.
Thanks.
As requested, here is my Player$$anonymous$$ovement script:
using UnityEngine;
using System.Collections;
public class Player$$anonymous$$ovement : $$anonymous$$onoBehaviour {
public float speed = 10.0f;
public float jumpForce = 600.0f;
private bool grounded = true;
private bool moving = false;
private Vector3 previousPosition;
private Quaternion previousRotation;
void Start() {
previousPosition = transform.position;
}
void Update () {
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(x, 0.0f, z);
Quaternion rotation = Quaternion.LookRotation(movement * speed * Time.deltaTime);
transform.position += movement * speed * Time.deltaTime;
if (is$$anonymous$$oving() && (rotation.y < -0.01 || rotation.y > 0.01)) {
previousRotation = transform.rotation;
transform.rotation = rotation;
}
if ( transform.position.y < 10.0f) {
Jump();
}
}
void Jump() {
if (Input.GetButtonDown("Jump") && grounded) {
rigidbody.AddForce(Vector3.up * jumpForce);
}
}
bool is$$anonymous$$oving() {
if (Time.timeScale > 0) {
Vector3 displacement = transform.position - previousPosition;
previousPosition = transform.position;
return displacement.magnitude / Time.deltaTime > 0.001;
}
return false;
}
}
Answer by sam2k13 · Jan 11, 2015 at 09:37 PM
Since you are moving by modifying the transform.position, the physics of the Box Collider will be ignored. Instead, you could use rigidbody.addForce() for movement. However, based on what I have seen, the character controller component is likely the way to go. The character controller is designed to behave in a more realistic way and its movement will interact with Physics. Check out these links and you should be good to go.
http://docs.unity3d.com/Manual/class-CharacterController.html
http://docs.unity3d.com/ScriptReference/CharacterController.html
Thanks for your answer; the invisible walls are now working as intended. However, I've now got a couple of new issues from using it though:
The rotation of the Player game object is no longer functioning (after trying my original rotation code, it does not work as previously)
The player's forward direction is now its left; if you watch the video I linked to in my OP when the player moves forward, I'm pressing "W" on a keyboard/forward on a joystick.
If you've got any ideas regarding these, then that would be great.
Thanks.
For rotation, try inserting a Debug.Log() to make sure the if statement is being called. Since you are using a character controller you may want to use the Velocity property of the character controller to deter$$anonymous$$e if the character is$$anonymous$$oving().
The simplest way to make your character face the forward direction is by deleting the mesh from that gameobject and creating a new gameobject with the mesh attached to it. Then make this new gameobject with the mesh a child of the gameobject with the character controller. Now simply rotate the gameobject with the mesh until it is in the forward direction. This allows you to rotate your mesh without affecting the forward direction of the character controller.
Thanks for getting back to me, but I'm not sure if I was clear regarding the new issue of the player movement. In the video I originally linked to above, the played move the direction it was facing, or to the right from our perspective.
However now when I was forward (i.e. "W" or forward on the joystick) the player moves towards the mountains. Any thoughts?
Please feel free to let me know if you need anything else on my part.
Thanks.
Oh, okay, well... I've managed to fix the player direction issue (I had to switch the Input.GetAxis() elements around in my Vector3 and negate the x value), the only issue left now is rotation not working as it should.
I've attached my code as a reference point for anyone who cares to answer:
using UnityEngine;
using System.Collections;
public class Player$$anonymous$$ovement : $$anonymous$$onoBehaviour {
public float speed = 10.0f;
public float jumpForce = 600.0f;
public float gravity = 20.0f;
private Vector3 direction = Vector3.zero;
private CharacterController controller;
private Vector3 previousPosition;
void Start() {
controller = GetComponent<CharacterController>();
}
void Update() {
Vector3 movement = transform.position - previousPosition;
if (controller.isGrounded) {
direction = new Vector3(-Input.GetAxis("Vertical"), 0.0f, Input.GetAxis("Horizontal"));
direction = transform.TransformDirection(direction);
direction *= speed;
if (direction != Vector3.zero) {
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(direction), 999999999f * Time.deltaTime);
}
if (Input.GetButton("Jump")) {
direction.y = jumpForce;
}
}
direction.y -= gravity * Time.deltaTime;
controller.$$anonymous$$ove(direction * Time.deltaTime);
}
void LateUpdate() {
previousPosition = transform.position;
}
}
Any help is much appreciated. Thanks! (:
Your answer
Follow this Question
Related Questions
3D Cube collision not working correctly 0 Answers
Can't go into building because of collider around it 1 Answer
OnMouseDown() cube face? 1 Answer
Face detection 1 Answer
How do I add a texture to a cube (yes I know I'm a scrub)? 1 Answer