質問

I'm making a football-penalty like game and I need to move the ball on mobile too. I'm using this code now:

float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
rigidbody.AddForce(movement * speed * Time.deltaTime);

How can i make a movement System For mobile?

Also, there is someother codes that control the ball (like when you pass a line (just like bowling) you can only change it's place 1/10). But they are based on this code... Game script is c#...

役に立ちましたか?

解決

Switching this from using axis to touch will be a bit difficult. I recommend reading about Touchscript (http://interactivelab.github.io/TouchScript/) and how the API works in general. What you're going to need to do is when the player touches the screen, you cache that touch and while they're touching you measure the direction of the movement by comparing their first touch to their current position. By doing this you can get swipe directions.

Something like this could get you started:

public void OnTouchBegin( object sender, TouchEventArgs e ){
    foreach( TouchPoint point in e.TouchPoints ){
        firstTouchPosition = new Vector2( point.Position.x, point.Position.y );
    }
}

public void OnTouchContinue( object sender, TouchEventArgs e ){
    foreach( var point in e.TouchPoints ){
        secondTouchPosition  = new Vector2( point.Position.x, point.Position.y );
        currentTouchMovement = new Vector2( ( secondTouchPosition.x - firstTouchPosition.x ),
        ( secondTouchPosition.y - firstTouchPosition.y ));
        currentTouchMovement.Normalize( );
    }
}

public void OnTouchEnd( object sender, TouchEventArgs e ){
    hasTouchEnded = true;
}

And you'll need some custom code to determine the X and Y axis direction corresponds with the touch input. Also be careful about screen coordinates and in-game coordinates as they differ quite a bit. This looks daunting at first, but with some work I promise its not too bad! Hope this helps!

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top