• 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 /
  • Help Room /
avatar image
0
Question by Budyw · Apr 07, 2016 at 06:35 PM · c#animationmovingswipedefault

script to move the character to the left, right, jump and slide .... with your finger sliding on the phone screen (swipe)

I'm trying to create a script for a character running to move left, right, jump and slide your finger sliding on the phone screen (swipe), but I'm in trouble !!

 private float ponteiroX, ponteiroY, novaPosicX;
 private int indicePosic;
 private bool podeMover, estaNoChao, pulouR;
 private Vector3 posicInicial;
 [Range(0.01f, 1)]
 public float TempoParaMover = 0.15f;
 [Range(1, 5)]
 public int QuantoMover = 1;
 [Range(1, 20)]
 public float forcaDoPulo = 5.0f;
 public LayerMask LayersNaoIgnoradas = -1;
 private Rigidbody corpoRigido;

 void Start()
 {
     corpoRigido = GetComponent<Rigidbody>();
     corpoRigido.constraints = RigidbodyConstraints.FreezeRotation;
     posicInicial = transform.position;
     novaPosicX = posicInicial.x + indicePosic * QuantoMover;
     indicePosic = 0;
     pulouR = false;
     podeMover = true;
 }

 void Update()
 {
     estaNoChao = Physics.Linecast(transform.position, transform.position - Vector3.up, LayersNaoIgnoradas);
     if (podeMover)
     {
         DetectarMovimento();
     }
 }

 IEnumerator EsperarParaMover(float tempo)
 {
     yield return new WaitForSeconds(tempo);
     podeMover = true;
 }
 IEnumerator EsperarParaPular(float tempo)
 {
     yield return new WaitForSeconds(tempo);
     pulouR = false;
 }

 void DetectarMovimento()
 {
     podeMover = false;
     StartCoroutine(EsperarParaMover(TempoParaMover));

     ponteiroX = ponteiroY = 0;
     if (Input.GetMouseButton(0))
     {
         ponteiroX = Input.GetAxis("Mouse X");
         ponteiroY = Input.GetAxis("Mouse Y");
     }
     if (Input.touchCount > 0)
     {
         ponteiroX = Input.touches[0].deltaPosition.x;
         ponteiroY = Input.touches[0].deltaPosition.y;
     }
     //DETECTAR EIXO X
     if (ponteiroX > 0 && indicePosic < 1)
     {
         indicePosic++;
         novaPosicX = posicInicial.x + indicePosic * QuantoMover;
     }
     else if (ponteiroX < 0 && indicePosic > -1)
     {
         indicePosic--;
         novaPosicX = posicInicial.x + indicePosic * QuantoMover;
     }
     //DETECTAR EIXO Y
     if (ponteiroY > 0.1f)
     {
         Pular();
     }
 }

 void FixedUpdate()
 {
     Vector3 proximaPosic = new Vector3(novaPosicX, transform.position.y, transform.position.z);
     transform.position = Vector3.Lerp(transform.position, proximaPosic, Time.deltaTime * 5);
 }

 void Pular()
 {
     if (estaNoChao == true && pulouR == false)
     {
         corpoRigido.AddForce(Vector3.up * forcaDoPulo, ForceMode.Impulse);
         pulouR = true;
         StartCoroutine(EsperarParaPular(0.5f));
     }
 }

}

Comment
Add comment · Show 4
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 Dave-Carlile · Apr 07, 2016 at 06:36 PM 1
Share

There is no question here. No indication of what your "trouble" is. No direction given for anyone to even being trying to help you. Add more details. Refer to the FAQ and User Guide linked on the right for help on asking better questions.

avatar image Budyw Dave-Carlile · Apr 07, 2016 at 06:45 PM 0
Share

The character moves off in the direction "Z", I wanted to add a script in character so that when sliding your finger on the mobile screen to the right it moves to the right, and do the same thing to the left.

avatar image Dave-Carlile Budyw · Apr 07, 2016 at 07:16 PM 1
Share

That's the expected behavior. What behavior are you actually seeing in your script? Have you used Debug.Log to look at various values? Have you used the debugger to step through the code to find out what is happening?

Show more comments

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How to i go about playing a animation when Left mouse is clicked? C# 0 Answers

The name animator does not exist in the current context... Help? 1 Answer

Moving Object faster depending on resolution 0 Answers

Animation play not working C# 0 Answers

Trying to animate using Breadcrumb(Asset) 0 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