# Move Help

everything works fine but i just noticed one tiny problem

i cant move diagonally

i can only move up, down, left and right

but i cant move on an angle how do i fix this?

#pragma strict

var player: Transform;
var speed: float = 5.0;
var jumpSpeed: float = 3;
var playerWithAnimation: Transform;
var rotateSpeed: float =20;
var grounded: boolean = false;
var maxSlope: float = 60;

public var Soundone:AudioClip;

function Update()
{

if ( Input.GetAxis(“Horizontal”) > 0.1)
{
player.position += Vector3(speed*Time.deltaTime,0,0);
player.eulerAngles = Vector3(0,90,0);
}

else if (( Input.GetAxis(“Horizontal”) > 0.1)( Input.GetAxis(“Vertical”) > 0.1))
{
player.position += Vector3(speedTime.deltaTime,0,0);
player.position += Vector3(0,0,speed
Time.deltaTime);
player.eulerAngles = Vector3(0,45,0);
}

else if( Input.GetAxis(“Horizontal”) < -0.1)
{
player.position -= Vector3(speed*Time.deltaTime,0,0);
player.eulerAngles = Vector3(0,-90,0);
}

else if ( Input.GetAxis(“Vertical”) > 0.1)
{
player.position += Vector3(0,0,speed*Time.deltaTime);
player.eulerAngles = Vector3(0,0,0);
}

else if ( Input.GetAxis(“Vertical”) < -0.1)
{
player.position -= Vector3(0,0,speed*Time.deltaTime);
player.eulerAngles = Vector3(0,-180,0);
}

else
{
playerWithAnimation.animation.Play(“idle”);
}

//angled turn

if(Input.GetButtonDown(“Jump”) grounded )
{
player.rigidbody.velocity.y = jumpSpeed;
}
}

function OnCollisionEnter(collision : Collision)

{

if(collision.gameObject.name == “Capsule”) {

audio.PlayOneShot(Soundone);

}

}

function OnCollisionStay(collision : Collision)
{
for(var contact: ContactPoint in collision.contacts) {
if (Vector3.Angle(contact.normal,Vector3.up) < maxSlope)
grounded = true;
}
}

function OnCollisionExit ()
{
grounded = false;

To move diagonally, you should decouple the logic of moving sideways from the logic of moving forwards/backwards.

Here is the basic pseudologic for your problem:

``````if(pressForward)
move forward
else if(press backward)
move backward

if(pressLeft)
moveLeft
else if(pressRight)
moveRight
``````

Else if should be used only when the expressions are mutually exclusive. For instance, in the following code which you had:

``````if ( Input.GetAxis("Horizontal") > 0.1)
{
player.position += Vector3(speed*Time.deltaTime,0,0);
player.eulerAngles = Vector3(0,90,0);
}

else if (( Input.GetAxis("Horizontal") > 0.1)( Input.GetAxis("Vertical") > 0.1))
{
player.position += Vector3(speed*Time.deltaTime,0,0);
player.position += Vector3(0,0,speed*Time.deltaTime);
player.eulerAngles = Vector3(0,45,0);
}
``````

That else if will never execute as it relies on a condition which, if true, will satisfy the condition of the first if block.

so how can i add a diagonal movement?

Do you mind reading my answer? I told you in the very first line, and even wrote out the logical process you should follow.

Just try to replace all those “else if” with “if” .
when you “else if” only one of them could work, when you multi “if”'s they all can work together .

there it worked XD

=]
[=
=]

Please do not replace all else if with if. Seriously, that is actually the worst advice I have ever seen given. You just need to make sure your else if makes sense.

Sigh.

lol, I know your answer is better but accordingly to his question afterward i’v tried to make it simple and working XD

I don’t see how this:

``````if(pressForward)
move forward
else if(press backward)
move backward

if(pressLeft)
moveLeft
else if(pressRight)
moveRight
``````

Can be seen as not simple. He seemed to accept your answer anyway because it gave him the result he desired without any thought. I guess I just don’t understand that philosophy.