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?

2 Answers

2

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..

works like a charm with some modifications.. thanks :D

If your question is answered, pick the best answer and click on the check mark to close out the question.

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 );
}