- Home /
The question is answered, right answer was accepted
Check which side of a cube my player collided with
I have this simple setup, where I have a moving player, and a cube object. When my player collides with the cube, he can press the key 'p' to push the cube.
However so far I have only found out to move the cube in a specific direction, by setting its vector. But what I want is to push the cube in the opposite direction of which side the player collided with.
I have tried to check out this answer - http://answers.unity3d.com/questions/339532/how-can-i-detect-which-side-of-a-box-i-collided-wi.html However I can't make the solution with a raycast work.
So I hope you will help me understand and find a solution to push my block in the oposite direction of the player, according to which side he has collided with.
My script attached to the cube is this
using UnityEngine;
using System.Collections;
public class BlockBehaviorScript : MonoBehaviour {
private Vector3 newPosition;
// Use this for initialization
void Start () {
newPosition = transform.position;
}
// Update is called once per frame
void Update () {
transform.position = Vector3.Lerp(transform.position, newPosition, Time.deltaTime);
}
public void changePosition(Vector3 to) {
newPosition = to;
}
void OnCollisionStay(Collision col) {
Debug.Log(col.gameObject.name);
if (Input.GetKeyDown(KeyCode.P))
{
Debug.Log("'P' has been pressed");
changePosition(new Vector3(transform.position.x, 0.0f, newPosition.z + 4f));
}
}
}
Answer by PouletFrit · May 31, 2014 at 05:07 PM
If I understand your problem correctly you want to be able to push a cube in the left right, up, bottom direction; no diagonals allowed... right? If that's the case pretty simple just find the direction, keep the larger axis value, then normalize your result and apply a constant:
public float pushDistance;
void OnCollisionStay(Collision col) {
Debug.Log(col.gameObject.name);
if (Input.GetKeyDown(KeyCode.P))
{
Debug.Log("'P' has been pressed");
// Find where the player is from the cube
Vector3 dir = transform.position - col.gameObject.transform.position;
// Do not consider the y direction
dir.y = 0;
// Are we going to push in the x or z axis?
if (Mathf.Abs(dir.x) > Mathf.Abs(dir.z))
{
dir.z = 0;
}
else
{
dir.x = 0;
}
// Normalize then apply a constant to be sure that the distance from which the player push won't affect the speed of the movement
dir.Normalize();
changePosition(transform.position + dir*pushDistance);
}
}
If you wish to be able to push diagonaly just remove:
if (Mathf.Abs(dir.x) > Mathf.Abs(dir.z))
{
dir.z = 0;
}
else
{
dir.x = 0;
}
Hi and thanks for the answer.
When I am using your if statement it still pushes diognally. If I want to do so it doesn't, but only pushes linear from the side I am standing af, how can I do that??
I corrected my script it should work now...
changePosition(transform.position + dir*pushDistance);
Thank you, it seems to work now. However, Do you know How I can make the block move a certain distance everytime?
PushDistance is that a vector for that purpose, and How do I initialize that vector?
$$anonymous$$now I use a float ins$$anonymous$$d of the variable pushDistance.
pushDistance is a float and it's deter$$anonymous$$ing the distance the cube will move everytime. Since it's a public global variable, you can see it from the inspector.
you can multiply Vector3 by constant value (int,float,etc...)
exemple:
float pushDistance = 5.0f;
Vector3 dir = new Vector3(1, 0, 0);
dir = dir * pushDistance;
// Dir is now equal to (5, 0, 0);