Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Multiplatform Input Controller

Discussion in 'Scripting' started by JasonF, Apr 5, 2016.

  1. JasonF

    JasonF

    Joined:
    Mar 19, 2016
    Posts:
    13
    I'm using the Multiplatform Input Controller in Unity 5.x for an Android build. When the player hits the Fire button it throws a dart. The issue I am having is that it works well only when the joystick is not being moved; dart goes right down the middle of the camera view, which is attached to the player. If the joystick is being moved left or right, the dart is thrown from a shifted left or right origin. I can't figure out why. The up/down movement of the joystick does not appear to similarly effect the origin of the rigidbody.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityStandardAssets.CrossPlatformInput;
    4.  
    5. public class ThrowDart : MonoBehaviour {
    6.  
    7.     public Rigidbody dart;
    8.     public float power = 1000f;
    9.     public float moveSpeed = 2f;
    10.  
    11.     public CharacterController characterControl;
    12.     public float pivotSpeed = 15f;
    13.  
    14.     // Use this for initialization
    15.     void Start () {
    16.  
    17.     }
    18.  
    19.     // Update is called once per frame
    20.     void Update () {
    21.         //control with the virtual joystick and fire button
    22.         float y = CrossPlatformInputManager.GetAxis("Vertical");
    23.  
    24.         float x = CrossPlatformInputManager.GetAxis("Horizontal");
    25.  
    26.         bool isFiring = CrossPlatformInputManager.GetButtonDown("Fire");
    27.  
    28.         if(y !=0)
    29.             RotateUpDown(y * pivotSpeed *-1);
    30.         if(x != 0)
    31.             MoveLeftRight(x*moveSpeed);
    32.         if(isFiring)
    33.         {
    34.             Rigidbody instance = Instantiate(dart, transform.position, transform.rotation) as Rigidbody;
    35.             Vector3 fwd = transform.TransformDirection(Vector3.forward);
    36.             instance.AddForce(fwd * power);
    37.         }
    38.     }
    39.  
    40.     public void RotateUpDown(float speed){
    41.         Vector3 rotate = Vector3.right * speed * Time.deltaTime;
    42.         characterControl.transform.Rotate(rotate);
    43.     }
    44.  
    45.     public void MoveLeftRight(float speed){
    46.         Vector3 moveLeftRight = characterControl.transform.right * speed * Time.deltaTime;
    47.         characterControl.Move(moveLeftRight);
    48.     }
    49.  
    50. }
     
  2. Zaflis

    Zaflis

    Joined:
    May 26, 2014
    Posts:
    438
    Are you sure that transform.position is exactly in sync with the character rigidbody position? What's happening in that Move(), and how is character moving per frame?
     
  3. JasonF

    JasonF

    Joined:
    Mar 19, 2016
    Posts:
    13
    Forgive me as I am new to Unity. Reading a couple books and doing their examples is quite different from the first time on your own. The only thing I can think of is if Time.deltaTime is messing me up. If I don't use it though, my understanding is that the game could move significantly faster or slower dependent upon the hardware's frame rate.

    On the other hand, maybe I need to use FixedUpdate(). Then the question is, do I just move the "if (isFiring){...}" statement, or the entire user movement control?
     
  4. JasonF

    JasonF

    Joined:
    Mar 19, 2016
    Posts:
    13
    I guess the community as a whole is as lost as I am on the topic. I'll be trying a few different things with the code and post a solution if I find one, for anybody else that might run into this issue.
     
  5. Zaflis

    Zaflis

    Joined:
    May 26, 2014
    Posts:
    438
    Not sure about the lost part yet, just waiting to see more code :p You didn't show how things move. I don't use unity assets to control my objects (if that's what you're using), i write my own.
     
  6. JasonF

    JasonF

    Joined:
    Mar 19, 2016
    Posts:
    13
    Yes. Using Unity asset, MultiPlatform Controller. You can see how it works here


    I haven't had time to play with the code, as I am preparing for a new class I'm teaching. I'll get to it in the next couple weeks though. The odd thing is when I rotate the first person view left/right, as opposed to moving left/right it works great. As soon as I change it to move left/right is when I see the issue. Of course I can't reproduce it on the PC, because I can't move the control and click the fire button at the same time. I'll have to video it while playing it on the phone. Basically when I am not moving the joystick, works great. Shoots right down the middle of the camera view. Even if I move the FPC, stop moving, then fire, it works great. If I am moving left while hitting fire, the projectile fires from an offset to the right side. If I move right and fire at the same time, the projectile fires from a point offset to the left. Imagine that the character is aming down the center while not moving, shooting from the right hip while moving left, and shottong from the left hip while moving right.

    I imagine it's a frame rate issue. That's why I want to play with FixedUpdate() and see what happens. I am new to the world of game development, so the timing/frame rate issues are new to me.
     
  7. Zaflis

    Zaflis

    Joined:
    May 26, 2014
    Posts:
    438
    I watched parts of the video. If you followed it along the way he did it, at 12:37 he's writing CrossPlatformInputManager.GetButton(). You can add it with || (or) condition to Input.GetButton() and from the top menus Edit -> Project Settings -> Input, set for example Space key for same thing. So then you can test on PC with mouse and keyboard. I imagine the testing becomes faster when you don't always have to build and run it on your mobile device.
     
  8. JasonF

    JasonF

    Joined:
    Mar 19, 2016
    Posts:
    13
    I hadn't even thought of that. As I get more familiar with the development platform, I'm sure this will become second nature. Thank you for the tip. It will certainly help things move faster.
     
  9. JasonF

    JasonF

    Joined:
    Mar 19, 2016
    Posts:
    13
    FixedUpdate() is definitely not working. I'm not sure if it fixed the original problem. I didn't bother building it and installing on the Android, because it causes anywhere from 1 to 3 of the object to be shot at once. I'm not sure why. All I did was move everything from the Update() to FixedUpdate(). I also tried just moving the if (isFiring) block. It had the same effect.
     
    Last edited: Apr 23, 2016
  10. JasonF

    JasonF

    Joined:
    Mar 19, 2016
    Posts:
    13
    The Input.GetButton() works like a charm. Thanks for that tip.
     
  11. Zaflis

    Zaflis

    Joined:
    May 26, 2014
    Posts:
    438
    As far as i understand, Update is called more often than FixedUpdate at least. Update is once per frame, but FixedUpdate happens at constant intervals by default 50 times per second. Detecting inputs will likely work better (less buggy) from Update then, but things like physics movement propably belong to FixedUpdate.
     
  12. JasonF

    JasonF

    Joined:
    Mar 19, 2016
    Posts:
    13
    I think I found a workaround. For some reason having the dart originate from the Main Camera is causing the issue. When I added a game object and attached it to the main camera (all I do is move the main camera when the user moves the joystick), then moved the firing of the dart to another script and attached that to the new game object, it seems to not have the issue.