- Home /
Only look at the moveDirection of Z-axis
I'm making a 2.5D sidescroller game, and I need my character to face the direction he's moving. My scene is setup so that Characters move forward and backward along the z axis, and up and down along the y axis.
So, In my current script I have managed to make my Character turn back and forth while he's grounded. But when he jumps, he also turns to face up and down, because of the following piece of code (at least I think so):
if (Input.GetAxis("Horizontal"))
{
transform.LookAt(transform.position + moveDirection);
}
So, what do I need to add here to make the character only look at the moveDirection of z-axis?
Ps. I have a strong feeling that the problem might be that line, but I can post the whole script if you think the problem is elsewhere.
EDIT
So, here's a little pic that shows how my scene is positioned, and how my character turned in the original script, while he was grounded.
http://imageshack.us/photo/my-images/40/kuva1tb.jpg/
The red capsule is the character, and the green thing is his gun, which points at the direction he's facing.
And this is what happens if I jump:
http://imageshack.us/photo/my-images/683/kuva2x.jpg/
I'm trying to make it turn in air just the same way as if it were grounded.
And just as a bonus, here's my whole current script with your editings:
var speed : float = 20.0;
var jumpSpeed : float = 20.0;
var gravity : float = 30.0;
private var moveDirection : Vector3 = Vector3.zero;
function Update()
{
var controller : CharacterController = GetComponent(CharacterController);
var inputZ = Input.GetAxis("Horizontal");
if(Input.GetAxis("Horizontal"))
{
var tempDirection = moveDirection;
tempDirection.z = transform.position.z;
transform.LookAt(transform.position + tempDirection);
}
if (controller.isGrounded)
{
moveDirection = Vector3(0, 0, (Input.GetAxis("Horizontal")));
moveDirection *= speed;
if (Input.GetButton ("Jump"))
{
moveDirection.y = jumpSpeed;
}
}
else
{
moveDirection.z = inputZ * speed;
}
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
}
And what is it you want the character to actually do when he jumps?
I only want him to go upwards, where I can still move him left and right and turn him to face left or right, and then fall down, while still being in control of him. He shouldn't turn sideways when he jumps, but stand "on his feet", just like when he's grounded. :)
Your code makes him act very strangely, he rolls around when he passes the 0 world point. Sorry if I'm being unclear, I don't know all important words, since English is not my number 1 language!
Ahh, I see. Yeah, the LookAt call will basically be trying to make him look at his feet with the way it is set up.
What's the behavior if you take out the entire if( Input.GetAxis("Horizontal") ) chunk of code? Does the player just jump up and down without being able to turn him to face left or right?
Yes, then he can move left and right, but he just faces right always, never being able to turn left. But Bunny's solution seems to be working just perfectly. I'm going to give you an upvote anyway, for your help and patience :)
Thanks JUnityer, sorry I led you down the wrong path for so long! As soon as I saw it I knew Bunny's solution was right. :)
Answer by Bunny83 · Dec 21, 2011 at 06:48 PM
Just set the y-component of your moveDirection to 0 before you use it for LookAt, or since it's a 2.5d game, just use the z axis
// Just use the z axis
transform.LookAt(transform.position + Vector3(0,0,moveDirection.z));
// Or set the y axis to 0
var tempDirection = moveDirection;
tempDirection.y = 0;
transform.LookAt(transform.position + moveDirection);
Thank you! Seems that this was actually too simple for me to realize. I think I have to start studying these transform and Vector3 things a bit more. This saved my project, I'll learn from this!
Answer by Aleron · Dec 21, 2011 at 04:48 PM
EDIT: This answer is incorrect and should not be used for reference in this case! The selected answer is correct (not to mention simpler!).
If I understand correctly, you only want the look at rotation in the x-y plane, ignoring the player's vertical location (z-axis). If that's the case, a simplistic way to use only the x and y components of the movement can be done with something like the following:
if(Input.GetAxis("Horizontal"))
{
var tempDirection = moveDirection;
tempDirection.z = transform.position.z;
transform.LookAt(transform.position + tempDirection);
}
I'm receiving a semicolon error. It points into this line
Vector3 tempDirection = moveDirection;
Can you post the exact error?
And I just noticed another error is my response, I used two arguments in LookAt, when I should be adding the two vectors. I'll edit my response to fix that. Then when I see the error you are getting for the earlier line, hopefully I'll see what's going wrong there. :)
The error was
...blahblah.js(30,16): UCE0001: ';' expected. Insert a semicolon at the end
It points to the line I mentioned before. There are no other errors, only this one.
And the new version doesn't change it anywhere, same error. This really confuses me, it looks like there's nothing wrong with it.
Ahh, javascript. Try my edited version of the code. I also fixed the second line inside the if so that it was using the z component of the transform.position (like it should have been, oops!)
Sorry, but this doesn't make much sense ;) tempDirection and moveDirection are directions. That means they are relative to something, in this case the player. Setting one axis of a direction to an absolute position will screw up the direction. Also the z-axis is the forward axis which is the one he want. He don't want any rotation up and down which is the y-axis ;).
Your answer
Follow this Question
Related Questions
2.5D Melee 0 Answers
Grappling Hook in 2.5d Game 0 Answers
2D or 3D settings for a 2.5D game? 2 Answers
2D 360 degress platformer example needed 0 Answers
2.5D Sprite always face at camera 0 Answers