Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Suraj. · Feb 06, 2015 at 07:24 AM · movementbuttonevent triggeringpressed

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 );
 }
 

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Suraj. · Feb 06, 2015 at 08:01 AM 0
Share

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!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to keep an object moving when holding down a button 2 Answers

2D Vector Movement,Vectorel Movement with Buttons 2 Answers

Hey Guys, I am trying to get my player to move when I push this button but it keeps saying that it cannot convert bool to float. Please help me! 1 Answer

how to make my character walk automatically and when pressing a button change the direction 2 Answers

Creating a Tooltip when hovering over a UI Button? 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges