- Home /
How to move a character from the middle of one box to the other?
So basically we are creating a game which requires the character to move on top of boxes that are kept together like a 3d plane. we got the character moving in fluid motion but that's not what we want. So how to make the character move from middle of one box to another in pixelated sort of movement. Thanks
How do you want to initiate movement? Do you want instant movement or movement over time? Can you show us your current script for movement?
instant movement with the arrow keys, but the movement has to be like a frame by frame effect where we refer to different positions of the character, which are flat .png files as our camera angle is fixed in a orthographic view (so there's no need for a 3D character).
/// This script moves the character controller forward
/// and sideways based on the arrow keys.
/// It also jumps when pressing space.
/// $$anonymous$$ake sure to attach a character controller to the same game object.
/// It is recommended that you make only one call to $$anonymous$$ove or Simple$$anonymous$$ove per frame.
var speed : float = 6.0;
var jumpSpeed : float = 8.0;
var gravity : float = 20.0;
private var moveDirection : Vector3 = Vector3.zero;
function Update() {
var controller : CharacterController = GetComponent(CharacterController);
if (controller.isGrounded) {
// We are grounded, so recalculate
// move direction directly from axes
moveDirection = Vector3(Input.GetAxis("Horizontal"), 0,
Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Input.GetButton ("Jump")) {
moveDirection.y = jumpSpeed;
}
}
// Apply gravity
moveDirection.y -= gravity * Time.deltaTime;
// $$anonymous$$ove the controller
controller.$$anonymous$$ove(moveDirection * Time.deltaTime);
}
Answer by robertbu · Apr 16, 2013 at 01:49 PM
You are using a character conroller for movement. For instant movement you only have to translate the character. I would also explicitly use the arrow keys if that is what you want rather than GetAxis(). So you will need four 'if' statements (one for each direction.) Here is an example of one direction:
if (Input.GetKeyDown(KeyCode.UpArrow) && transform.position.z < max_z_value) {
transform.position += Vector3.forward;
}
This code assumes the blocks are 1 unit on a side.
I don't know what a "frame by frame effect" is. I guessing you want to change the texture of the character based on the direction of the key pressed. You can do that by having an array of textures and setting the mainTexture. See Material.mainTexture.
hi there, I understand the coding bit you have presented. But I might need some help on how to use it in my movement javascript. where do i include this coding? I am new to coding. Thanking you in advance. and the coding is giving an error with the max_z_value.. so confused..
max_z_value is a variable you need to define and initialize that is the maximum value you want to allow the character to move. It will be at or slightly under the center position of the block most forward. For a 10 x 10 grid centered on the origin, the value would be 4.9. For example at the top of the file:
private var max_z_value = 4.9;
private var $$anonymous$$_z_value = -4.9;
private var max_x_value = 4.9;
private var $$anonymous$$_x_value = -4.9;
And then the code inline would be:
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.UpArrow) && transform.position.z < max_z_value) {
transform.position += Vector3.forward;
}
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.DownArrow) && transform.position.x > $$anonymous$$_z_value) {
transform.position += Vector3.back;
}
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.RightArrow) && transform.position.x < max_x_value) {
transform.position += Vector3.Right;
}
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.LeftArrow) && transform.position.x < $$anonymous$$_x_value) {
transform.position += Vector3.Left;
}
If your question is answered, pick the best answer and click on the check mark to close out the question.
Answer by anniyan137 · Apr 22, 2013 at 01:41 PM
I am assuming you're very new to Unity. (and programming too?)
That said, my suggestion would be to learn a bit of coding. Go through the tutorials on the Unity website.
Apart from that, what you would need to do is skip using the CharacterController and just do the calculations by hand. It should look something like this.
var x_offset : float = <some value>; // By how much the character should move in the X direction
var y_offset : float = <some value>; // By how much the character should move in the Y direction
var newPos : Vector3;
.
.
.
if( Input.GetKeyDown(KeyCode.RightArrow) )
{
newPos = new Vector3(transform.position.x + x_offset, transform.position.y, transform.position.z);
transform.Translate( newPos );
}
if( Input.GetKeyDown(KeyCode.LeftArrow) )
{
newPos = new Vector3(transform.position.x - x_offset, transform.position.y, transform.position.z);
transform.Translate( newPos );
}
if( Input.GetKeyDown(KeyCode.UpArrow) )
{
newPos = new Vector3(transform.position.x, transform.position.y + y_offset, transform.position.z);
transform.Translate( newPos );
}
if( Input.GetKeyDown(KeyCode.DownArrow) )
{
newPos = new Vector3(transform.position.x, transform.position.y + y_offset, transform.position.z);
transform.Translate( newPos );
}
Your answer
Follow this Question
Related Questions
character(ball) rolls with less speed when I build it 2 Answers
Character Control - Grid like movement 2 Answers
Grid-based movement: How to face forward 2 Answers
How does character selection in endless runner works? 0 Answers
simple character movement 0 Answers