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 TheBloop · Nov 20, 2020 at 11:55 PM · movementmobilecontrols

Mobile Movement won't work

Hello I have a script for my player's movement but for some reason it only works on PC. I tried SO many methods to make the player move on mobile (including joysticks, Raycasters and CrossPlatform) but nothing works. Here is my current script, would love if anybody could help me figure out why it won't work:

 using System.Collections;
 using System.Collections.Generic;
 using UnityStandardAssets.CrossPlatformInput;
 using UnityEngine;
 
 public class Movement : MonoBehaviour
 {
     //variables    
     public Animator animator;
 
     public float moveSpeed = 0.9f;
     public GameObject character;
     private Rigidbody2D characterBody;
     private float ScreenWidth;
     // Use this for initialization    
     void Start()
     {
         gameObject.tag = "Player";
         ScreenWidth = Screen.width;
         characterBody = character.GetComponent<Rigidbody2D>();
     }
     // Update is called once per frame    
     void Update()
     {
         int i = 0;
         //loop over every touch found    
         while (i < Input.touchCount)
         {
             if (Input.GetTouch(i).position.x > ScreenWidth / 2)
             {
                 //move right    
                 RunCharacter(0f);
             }
             if (Input.GetTouch(i).position.x < ScreenWidth / 2)
             {
                 //move left    
                 RunCharacter(0f);
             }
             ++i;
         }
     }
     void FixedUpdate()
     {
         animator.SetFloat("Horizontal", CrossPlatformInputManager.GetAxis("Horizontal"));
 #if UNITY_EDITOR
         RunCharacter(Input.GetAxis("Horizontal"));
 #endif
     }
     private void RunCharacter(float horizontalInput)
     {
         //move player    
         //characterBody.AddForce(new Vector2(horizontalInput * moveSpeed * Time.deltaTime, 0f));
         characterBody.velocity = new Vector2(horizontalInput * moveSpeed * Time.deltaTime, 0.0f);
 
         //animator
         if (CrossPlatformInputManager.GetAxisRaw("Horizontal") == 1 || CrossPlatformInputManager.GetAxisRaw("Horizontal") == -1)
         {
             animator.SetFloat("LastMove", CrossPlatformInputManager.GetAxisRaw("Horizontal"));
         }
     }
 }


also note: this script used to work on my early drafts. Not sure what changed since I only added an animator to it.

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
Best Answer

Answer by Llama_w_2Ls · Nov 21, 2020 at 07:01 PM

In both your touch inputs (in your update), you call the function RunCharacter(0f). This takes in the parameter HorizontalInput and multiplies the characterBody.velocity by it and other factors. If the HorizontalInput is 0, your character will never move!! @TheBloop

Also I didnt know you could do ++i instead of i++ to increment by 1.

Comment
Add comment · Show 2 · 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 TheBloop · Nov 21, 2020 at 08:40 PM 0
Share

Yup, you were right! feel kinda silly for just missing that. Thank you! However now the animator won't work, not sure how to fix it...

avatar image Llama_w_2Ls TheBloop · Nov 22, 2020 at 04:12 PM 0
Share

Try not to use animator.SetFloat, instead you can just make the animator play an animation clip like animator.Play("AnimationClipName"). Sorry i guess. Cant really help much

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

259 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 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 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 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 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 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 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 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 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 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 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 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 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

2.5D Mobile joystick rotation problem 0 Answers

How to move a game object by moving a device? 2 Answers

How do I create WASD controls for a fps? 3 Answers

Is WebGL compatible with mobile controls/platform? 0 Answers

Rotation of an object to match phone rotation with torgue 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