- Home /
2D Player Movement
Hello, I am trying to construct a 2D platform game based in a 3D world. I want the character to move on platforms that are wrapped around a cube. Each time the player gets to the edge of the cube he will rotate, the camera will switch and player will head around the cube. Thus the game will always be experienced from a 2D perspective but it exists in a 3D world. Now I am having trouble getting the player to move down the z axis at the appropriate time.
The "front" face of the cube (x,y) axis is working fine, but when the character reaches the edge of the cube, he needs to rotate to walk down the "right" side of the cube (z,y) axis. I have been working with the 2D platform tutorial from Unity's website, the one with lerpz, but I have started writing my own movement script using a rigidbody character because the platform controller from the tutorial is above my pay grade and I am having trouble figuring out where to even start. They have the character forced out of the z axis and although I have set up the triggers and the cameras and switching between them works perfectly I cant get lerpz to walk down the z axis after the camera switch has taken place.
So here is the current rigidbody script.
//variables containing the cameras
var CubeFaceFront : Camera;
var CubeFaceRight : Camera;
//variable setting player movement speed
var speed = 10;
//Function to activate front camera and disable the rest at game launch
function start ()
{
CubeFaceFront.enabled = true;
CubeFaceRight.enabled = false;
}
//Function to detect camera switch trigger collision. And disable/enable appropriate camera.
function OnControllerColliderHit (hit : ControllerColliderHit)
{
if (hit.gameObject.tag == "camswitchR")
{
CubeFaceFront.enabled = false;
CubeFaceRight.enabled = true;
}
}
//Function that moves the player in the proper direction according to which camera and cube face is active.
function Update ()
{
if (CubeFaceFront == true && Input.GetKey ("d"))
{
rigidbody.AddForce(Vector3.right * speed * Time.deltaTime);
}
if (CubeFaceFront == true && Input.GetKey ("a"))
{
rigidbody.AddForce(Vector3.right * -speed * Time.deltaTime);
}
if (CubeFaceRight == true && Input.GetKey ("d"))
{
rigidbody.AddForce(Vector3.forward * speed * Time.deltaTime);
}
if (CubeFaceRight == true && Input.GetKey ("a"))
{
rigidbody.AddForce(Vector3.forward * -speed * Time.deltaTime);
}
}
this is setup so that when the rigidbody collides with the camswitchR object the player will turn and, "a" and "d" will move him down the z axis instead of the x axis. however it wont work the character either doesnt move or he goes through the camswitchR object.
Thanks for any help, If this doesnt make any sense I will try to explain the concept more. If someone knows or can help me figure out how to edit the tutorial platform controller script that would be the most optimal but i have been trying to figure out how to edit it for a few days and none of my ideas work. I can post the tutorial platform controller script if that would help. (the original and the edited version with my attempts to liberate the Z axis.
Let me know what can help.
sincerely
pmain
Please try to properly format any (and all) code that you post. If people have a hard time reading your question, they don't tend to give it a second glance.
i dont know how to do that. I click the button it says enter code here i do but it still posts un formatted.
Answer by Meater6 · Jun 15, 2011 at 11:22 PM
Try using the camera's direction. For example, when you press the "d" key, you move right from your current camera's perspective. Once you switch, it then uses the second camera's perspective. You can do this by setting up a Vector3 like this:
Vector3(Input.GetAxis("Horizontal"), 0, 0);
Then you transform the direction to the cameras perspective like this
cameraObject.transform.TransformDirection(/*The Vector Above*/)
then you move your character using that vector * speed. I personally have not tried this, but that is how 3rd person controllers work and in theory, works in this scenario.
Your answer
Follow this Question
Related Questions
Play audio on keyboard click? 1 Answer
How do i lock out a axis 1 Answer
Object won't rotate z axis on second click? 2 Answers
Images won't import as sprites? 1 Answer
Need help with rotation script. 1 Answer