- Home /
character turning problem!
ok so i've been following this tutorial to make my own character controller for my character http://www.youtube.com/watch?v=7hXSbA4sr-Q and so far it's been working fine up untill 29 mins in when he pastes a chunk of coding for making the character turn left right and back when it moves. The original coordinates didn't quite work for my character so i made a few edits to the code and this is what i have so far,
function LateUpdate(){
if(Input.GetAxis("Vertical") == 0){
if(Input.GetAxis("Horizontal") > 0){
body.localEulerAngles.y = 90;
}else if(Input.GetAxis("Horizontal") < 0){
body.localEulerAngles.y = -90;
}
}else if(Input.GetAxis("Vertical") > 0){
if(Input.GetAxis("Horizontal") > 0){
body.localEulerAngles.y = 45;
}else if(Input.GetAxis("Horizontal") < 0){
body.localEulerAngles.y = 135;
}
}else if(Input.GetAxis("Vertical") < 0){
if(Input.GetAxis("Horizontal") > 0){
body.localEulerAngles.y = -45;
}else if(Input.GetAxis("Horizontal") < 0){
body.localEulerAngles.y = -135;
}
}
}
the character will turn right, up and diaganal right/up aka all the positive positions are fine except right/down but its getting it to turn negatively eg left and down and so on thats the issue it just won't it only slides when i press those keys :/ i thought the negative code would be laid out the same as the possitive just all the values and numbers would be opposite and negative but apparently its not that simple >.< anyone have any ideas or could you fix my code for me and explain what i am doing wrong would be much apreciated :) x
Please update this post by highlighting your code and pressing the 10101/code button
I formatted your code, but without proper indentation, it is difficult to read. I recommend you fix the indentation and repost your code. It may very well be that the issue is in the structure of your 'if' and 'else' statements. If so, proper indentation will likely highlight the problem.
The else if statement at line 15 will never be evaluated, as any time "Vertical" == 0 the compiler will not progress beyond the if group at lines 3-9.
At line 7 you'd want what you have at line 17. Line 12 should have a less-than symbol. Line 22 should have a greater-than symbol. Remove lines 15-20.
Hope that helps!
@Quillicit that has almost fixed my problem thanks :) it still cannot face down though how ever it now goes left and diagonally left/down, but right/down and left/up seem to be opposite to each other eg if i press the up arrow key and the left arrow key the character faces right/down whilst moving up/left and vice versa >.< i'm very new to JavaScript so i'm not great at this i am affraid but i have updated my code on this post with what you have given me so far, also sorry for those that couldnt read this and thanks to @robertbu for laying it out correctly i am new to unity and didn't realise it would look as horrid as it did when i posted but i went to bed shortly after so didnt have to time to tidy it lol x
You can't face down because you have no state for when you're not pressing a horizontal button. Between lines 9 and 10, try:
body.localEulerAngles.x = 35;
And between 15 and 16:
body.localEulerAngles.x = -35;
I'm not really sure why the tutorial has you using localEulerAngles, honestly. A better bet is transform.Rotate(x, y, z). You'll also want to get a $$anonymous$$athf.Clamp in the action too, to limit the up and down rotation.