- Home /
UI 4.6 left right button movement problem
HI...Please help me with this.Would be very thankful I have added buttons in my game.(New UI 4.6) I want my character to jump and move left and right. Jump is working fine. But facing problem with movement buttons(left/right).Player is moving little distance with single mouse click. I want it to be like if button is pressed player will continue moving in direction.and when released player will stop. I am using this below script.Please help me.
using UnityEngine; using System.Collections; using UnityEngine.UI;
public class PlayScene : MonoBehaviour { // movement config public float gravity = -25f; public float runSpeed = 8f; public float groundDamping = 20f; // how fast do we change direction? higher means faster public float inAirDamping = 5f; public float jumpHeight = 3f; public static bool flip;
public Button jump_puppy;
public Button left_puppy;
public GameObject dog;
private bool canDoubleJump = false;
[HideInInspector]
private float normalizedHorizontalSpeed = 0;
private CharacterControl _controller;
private Animator _animator;
private RaycastHit2D _lastControllerColliderHit;
private Vector3 _velocity;
void Awake()
{
_animator = GetComponent<Animator>();
_controller = GetComponent<CharacterControl>();
// listen to some events for illustration purposes
_controller.onControllerCollidedEvent += onControllerCollider;
_controller.onTriggerEnterEvent += onTriggerEnterEvent;
_controller.onTriggerExitEvent += onTriggerExitEvent;
}
void Start ()
{
dog = GameObject.FindWithTag ("Player");
}
#region Event Listeners
void onControllerCollider( RaycastHit2D hit )
{
// bail out on plain old ground hits cause they arent very interesting
if( hit.normal.y == 1f )
return;
// logs any collider hits if uncommented. it gets noisy so it is commented out for the demo
//Debug.Log( "flags: " + _controller.collisionState + ", hit.normal: " + hit.normal );
}
void onTriggerEnterEvent( Collider2D col )
{
Debug.Log( "onTriggerEnterEvent: " + col.gameObject.name );
}
void onTriggerExitEvent( Collider2D col )
{
Debug.Log( "onTriggerExitEvent: " + col.gameObject.name );
}
#endregion
// the Update loop contains a very simple example of moving the character around and controlling the animation
public void jumpa()
{
_velocity = _controller.velocity;
if( _controller.isGrounded )
_velocity.y = 0;
if(_controller.isGrounded)
{
canDoubleJump = false;
_velocity.y = Mathf.Sqrt( 2f * jumpHeight * -gravity );
_animator.Play( Animator.StringToHash( "Jump" ) );
}
else {
if(!canDoubleJump)
{
canDoubleJump = true;
_velocity.y = Mathf.Sqrt( 2f * jumpHeight * -gravity );
}
}
// apply horizontal speed smoothing it
var smoothedMovementFactor = _controller.isGrounded ? groundDamping : inAirDamping; // how fast do we change direction?
_velocity.x = Mathf.Lerp( _velocity.x, normalizedHorizontalSpeed * runSpeed, Time.deltaTime * smoothedMovementFactor );
// apply gravity before moving
_velocity.y += gravity * Time.deltaTime;
_controller.move( _velocity * Time.deltaTime );
}
public void lefta(){
// grab our current _velocity to use as a base for all calculations
_velocity = _controller.velocity;
if( _controller.isGrounded )
_velocity.y = 0;
flip = false;
normalizedHorizontalSpeed = -1;
if( transform.localScale.x > 0f )
transform.localScale = new Vector3( -transform.localScale.x, transform.localScale.y, transform.localScale.z );
if( _controller.isGrounded )
_animator.Play( Animator.StringToHash( "Run" ) );
// apply horizontal speed smoothing it
var smoothedMovementFactor = _controller.isGrounded ? groundDamping : inAirDamping; // how fast do we change direction?
_velocity.x = Mathf.Lerp( _velocity.x, normalizedHorizontalSpeed * runSpeed, Time.deltaTime * smoothedMovementFactor );
// apply gravity before moving
_velocity.y += gravity * Time.deltaTime;
_controller.move( _velocity * Time.deltaTime );
}
void Update()
{
// grab our current _velocity to use as a base for all calculations
_velocity = _controller.velocity;
if( _controller.isGrounded )
_velocity.y = 0;
if( Input.GetKey( KeyCode.RightArrow ) )
{
flip = true;
normalizedHorizontalSpeed = 1;
if( transform.localScale.x < 0f )
transform.localScale = new Vector3( -transform.localScale.x, transform.localScale.y, transform.localScale.z );
if( _controller.isGrounded )
_animator.Play( Animator.StringToHash( "Run" ) );
}
else if( Input.GetKey( KeyCode.LeftArrow ) )
{
flip = false;
normalizedHorizontalSpeed = -1;
if( transform.localScale.x > 0f )
transform.localScale = new Vector3( -transform.localScale.x, transform.localScale.y, transform.localScale.z );
if( _controller.isGrounded )
_animator.Play( Animator.StringToHash( "Run" ) );
}
else
{
normalizedHorizontalSpeed = 0;
if( _controller.isGrounded )
_animator.Play( Animator.StringToHash( "Idle" ) );
}
// we can only jump whilst grounded
if(_controller.isGrounded && Input.GetKeyDown( KeyCode.UpArrow ) )
{
canDoubleJump = false;
_velocity.y = Mathf.Sqrt( 2f * jumpHeight * -gravity );
_animator.Play( Animator.StringToHash( "Jump" ) );
}
else {
if(!canDoubleJump && Input.GetKeyDown( KeyCode.UpArrow )){
canDoubleJump = true;
_velocity.y = Mathf.Sqrt( 2f * jumpHeight * -gravity );
}
}
// apply horizontal speed smoothing it
var smoothedMovementFactor = _controller.isGrounded ? groundDamping : inAirDamping; // how fast do we change direction?
_velocity.x = Mathf.Lerp( _velocity.x, normalizedHorizontalSpeed * runSpeed, Time.deltaTime * smoothedMovementFactor );
// apply gravity before moving
_velocity.y += gravity * Time.deltaTime;
_controller.move( _velocity * Time.deltaTime );
}
}
Answer by SnStarr · Feb 06, 2015 at 07:36 AM
Your Input.GetKey should be Input.GetKeyDown in your case, also the normalizedhorizontal speed variable is set to 1 every frame within the if block statement. Change that to a higher number and also for practices sake, multiply it by time.deltatime. I won't feed you a script but together we can teach you how to script. If you have anymore questions feel free to inbox me.
hey thanksa lot...i got it sorted out...Actually I was not familiar with 4.6 UI. Had to add event sytem and further bool of pressed and unpressed and then event triggers on button...but thanks anyways again!!cheers!