2d Basic Movement Unity
Solution 1:
For the Jump action, what you need is a Button. GameObject->UI->Button. Replace the Image with your own image and click "Set Native Size". Re-size the button to the size that's good for your game.
Attach the Button to the Jump Button slot script below.
public Button jumpButton;
voidjumpButtonCallBack()
{
// rigidbody2D.AddForce (transform.up * force);
GetComponent<Rigidbody2D>().AddForce(transform.up * force);
jumpTime = jumpDelay;
animator.SetTrigger("jump");
jumped = true;
}
voidOnEnable(){
//Un-Register Button
jumpButton.onClick.AddListener(() => jumpButtonCallBack());
}
voidOnDisable(){
//Un-register Button
jumpButton.onClick.RemoveAllListeners();
}
For the Left and Right Movement buttons,you should not use Button
component for them like the jump button. You have to implement Virtual JoyStick to make it look realistic. Unity have Assets called CrossPlatformInputManager that can do that. You have to import it and modify it a little bit in order to use it. Watch this to understand how to import it.
Now, you can replace your Input.GetAxis
and Input.GetAxisRaw
functions with CrossPlatformInputManager.GetAxis("Horizontal")
and CrossPlatformInputManager.GetAxisRaw("Horizontal")
.
If you get it to work, then can use below to make your code comptible with both mobile and desktop.
#if UNITY_EDITOR || UNITY_STANDALONE || UNITY_WEBGL//put your Input.GetAxis` and `Input.GetAxisRaw` code here#elif UNITY_ANDROID || UNITY_IOS //Put your `CrossPlatformInputManager.GetAxis("Horizontal")` and `CrossPlatformInputManager.GetAxisRaw("Horizontal")`. here#endif
Solution 2:
I think you should use EventTrigger OnPointerDown and OnPointerUp events:
http://docs.unity3d.com/ScriptReference/EventSystems.EventTrigger.html
I hope it helps you
Post a Comment for "2d Basic Movement Unity"