- Home /
Walking on a cube
Hi guys! I need to make my character walking on a cube like in this video http://www.youtube.com/watch?v=YYlGJoLUJVQ
I tried to use triggers and normal... but they don't work properly. Does someone have an idea or an example to do that?
I have only gravity at the center of the cube that is working fine :(
Regards!
I know this post is old but i'd like to know if you managed to make it work properly, cause i'm very interested in it ! :D
Thanks !
Answer by Marsupilami · Oct 14, 2013 at 11:52 PM
Follow this link CLICK ME and look for my posts at the bottom.
Answer by phxvyper · Oct 14, 2013 at 06:43 AM
He modified or created his own FPSController. There are quite a few ways to do what he's doing. However, it is somewhat complex...
One way i can think of is:
Create a script that represents a point of gravity. When the Player Controller falls off of the side (it has no bottom trigger being triggered), make the Player Controller fall towards the point of gravity.
There may be a few more hoops you'd have to jump through, but this should lead you in the right direction.
Making the player fall towards a point is pretty easy. You can use quite a few number of methods, one of which would involve using Rotate and Translate (probably the easiest, but not always the best...)
Because you don't want to be using normal PhysX Gravity Physics for this, make sure that gravity is disabled in the Rigidbody's properties.
The code would look similar to this:
/* C# */
void Update() {
//playerGrounded is a flag boolean that is true when the player is touching the ground, and false when the player is not touching the ground.
if (!playerGrounded)
{
//Using LookAt and Vector3.Down, make the player rotate towards the point of gravity.
//I wrote this code so that it will function if you have multiple points of gravity within the scene.
//This also assumes that the object that represents the point of gravity has a script attached to it called "GravityPoint".
//Find and cast every single GameObject of type GravityPoint.
GameObject[] gravityPoints = (GameObject[])GameObject.FindObjectsOfType(typeof(GravityPoint));
GameObject closestGravityPoint; //This is the value that will be assigned the closest GravityPoint game object.
if (gravityPoints.Length > 0) { //Make sure that there are gravity points in the scene.
closestGravityPoint = gravityPoints[0]; //This is just used to assign any gravity point to closestGravityPoint.
foreach (GameObject go in gravityPoints) { //Iterate through every gravity point.
//firstAndPlayer is the distance between the current object assigned to closestGravityPoint and the player.
float firstAndPlayer = Vector3.Distance(closestGravityPoint.transform.position, transform.position);
//goAndPlayer is the distance between the current object assigned to 'go' (the iterator) and the player.
float goAndPlayer = Vector3.Distance(go.transform.position, transform.position);
//Is go closer than the current value of closestGravityPoint? If so, assign the closestGravityPoint as go.
if (goAndPlayer < firstAndPlayer)
closestGravityPoint = go;
}
//Make the player rotate towards the closestGravityPoint's transform.
//I use Vector3.forward as the 2nd parameter, the reason for this is
//that the second parameter is what the up direction of the transform is, not the direction you want to look it.
//Any Vector3 that only exists on the x or z axis will work for this, theoretically.
transform.LookAt(closestGravityPoint.transform, Vector3.forward);
}
}
}
/* UnityScript */
function Update() {
if (!playerGrounded)
{
var gravityPoints : GameObject[] = GameObject.FindObjectsOfType(typeof(GravityPoint)) as GameObjects[];
var closestGravityPoint : GameObject;
if (gravityPoints.Length > 0) {
closestGravityPoint = gravityPoints[0];
for (var i = 0; i < gravityPoints.length; i++) {
float firstAndPlayer = Vector3.Distance(closestGravityPoint.transform.position, transform.position);
float goAndPlayer = Vector3.Distance(gravityPoints[i].transform.position, transform.position);
if (goAndPlayer < firstAndPlayer) {
closestGravityPoint = gravityPoints[i];
}
}
transform.LookAt(closestGravityPoint, Vector3.forward);
}
}
}
You will also have to do some rotation correct for when playerGrounded = true
so that the player doesn't have a sideways rotation.
How can I make the player falling towards the point of gravity?
@Loxli check out the updated version of my post.
Theoretically, this should work. Also, check out marsupilami's answer.
No problem! I'm glad i was able to help you understand the concept.
Your answer
Follow this Question
Related Questions
Rotating a created mesh around it's pivot 2 Answers
Walking animation following navigation 0 Answers
Measure a rotating gameobject. 0 Answers