Sign in to follow this  
pch93

Unity how to apply dual joystick on player movement that contain multiple sprite movement script?

Recommended Posts

I got my player movement script, joystick movement script and also joystick movement script. The question is i got no idea how to change the keyboard movement script to the virtual joystick script.The main solution i face is my player character got 4 view which is left, right, front, back and 3 state which is idle, death, and walk? i want my joystick movement input same as the keyboard movement as below. I really try so hard, but still cant get any clue about that. can someone teach me that?

 

 

using UnityEngine;

using System.Collections;

public class TaoistMovement : MonoBehaviour
{
//Animator anim
bool left direction;
bool right direction;
bool front direction;
bool back direction;

// Use this for initialization
void Start()
{

}


void Update()
{
float MoveSpeed = 4.5f;

string charactersname = "taoistmoveobject";
string charactername = "taoist";

//change the string to my parameter
string charactersfrontwalk = "FrontWalk";
string charactersbackwalk = "BackWalk";
string charactersleftwalk = "LeftWalk";
string charactersrightwalk = "RightWalk";
string charactersfrontidle = "FrontIdle";
string charactersbackidle = "BackIdle";
string charactersrightidle = "RightIdle";
string charactersleftidle = "LeftIdle";

GameObject characters = GameObject.Find(charactersname);
GameObject theanim = GameObject.Find(charactername);



//something
character.transform.localPosition = new Vector3(character.transform.localPosition.x, character.transform.localPosition.+ 0.05f * MoveSpeed, character.transform.localPosition.z);
character.transform.localPosition = new Vector3(character.transform.localPosition.- 0.05f * MoveSpeed, character.transform.localPosition.y, character.transform.localPosition.z);


if (!Input.GetKey("up") && !Input.GetKey("down") && !Input.GetKey("left") && !Input.GetKey("right"))
{
if (FrontDirection == true)
{
theanim.GetComponent<Animator>().SetBool(charactersfrontwalk, false);
theanim.GetComponent<Animator>().SetBool(charactersbackwalk, false);
theanim.GetComponent<Animator>().SetBool(charactersleftwalk, false);
theanim.GetComponent<Animator>().SetBool(charactersrightwalk, false);
theanim.GetComponent<Animator>().SetBool(charactersfrontidle, true);
theanim.GetComponent<Animator>().SetBool(charactersbackidle, false);
theanim.GetComponent<Animator>().SetBool(charactersrightidle, false);
theanim.GetComponent<Animator>().SetBool(charactersleftidle, false);
}
if (BackDirection == true)
{
theanim.GetComponent<Animator>().SetBool(charactersfrontwalk, false);
theanim.GetComponent<Animator>().SetBool(charactersbackwalk, false);
theanim.GetComponent<Animator>().SetBool(charactersleftwalk, false);
theanim.GetComponent<Animator>().SetBool(charactersrightwalk, false);
theanim.GetComponent<Animator>().SetBool(charactersfrontidle, false);
theanim.GetComponent<Animator>().SetBool(charactersbackidle, true);
theanim.GetComponent<Animator>().SetBool(charactersrightidle, false);
theanim.GetComponent<Animator>().SetBool(charactersleftidle, false);
}
if (LeftDirection == true)
{
theanim.GetComponent<Animator>().SetBool(charactersfrontwalk, false);
theanim.GetComponent<Animator>().SetBool(charactersbackwalk, false);
theanim.GetComponent<Animator>().SetBool(charactersleftwalk, false);
theanim.GetComponent<Animator>().SetBool(charactersrightwalk, false);
theanim.GetComponent<Animator>().SetBool(charactersfrontidle, false);
theanim.GetComponent<Animator>().SetBool(charactersbackidle, false);
theanim.GetComponent<Animator>().SetBool(charactersrightidle, false);
theanim.GetComponent<Animator>().SetBool(charactersleftidle, true);
}
if (RightDirection == true)
{
theanim.GetComponent<Animator>().SetBool(charactersfrontwalk, false);
theanim.GetComponent<Animator>().SetBool(charactersbackwalk, false);
theanim.GetComponent<Animator>().SetBool(charactersleftwalk, false);
theanim.GetComponent<Animator>().SetBool(charactersrightwalk, false);
theanim.GetComponent<Animator>().SetBool(charactersfrontidle, false);
theanim.GetComponent<Animator>().SetBool(charactersbackidle, false);
theanim.GetComponent<Animator>().SetBool(charactersrightidle, true);
theanim.GetComponent<Animator>().SetBool(charactersleftidle, false);
}
}





if (Input.GetKey("up") || Input.GetKey("down") || Input.GetKey("left") || Input.GetKey("right"))
{
if (Input.GetKey("up") && !(Input.GetKey("down") || Input.GetKey("left") || Input.GetKey("right")))
{
theanim.GetComponent<Animator>().SetBool(charactersfrontwalk, false);
theanim.GetComponent<Animator>().SetBool(charactersbackwalk, true);
theanim.GetComponent<Animator>().SetBool(charactersleftwalk, false);
theanim.GetComponent<Animator>().SetBool(charactersrightwalk, false);
theanim.GetComponent<Animator>().SetBool(charactersfrontidle, false);
theanim.GetComponent<Animator>().SetBool(charactersbackidle, false);
theanim.GetComponent<Animator>().SetBool(charactersrightidle, false);
theanim.GetComponent<Animator>().SetBool(charactersleftidle, false);
LeftDirection = false;
RightDirection = false;
BackDirection = true;
FrontDirection = false;

characters.transform.localPosition = new Vector3(characters.transform.localPosition.x, characters.transform.localPosition.y, characters.transform.localPosition.+ 0.05f * MoveSpeed);
}
if (Input.GetKey("down") && !(Input.GetKey("up") || Input.GetKey("left") || Input.GetKey("right")))
{
theanim.GetComponent<Animator>().SetBool(charactersfrontwalk, true);
theanim.GetComponent<Animator>().SetBool(charactersbackwalk, false);
theanim.GetComponent<Animator>().SetBool(charactersleftwalk, false);
theanim.GetComponent<Animator>().SetBool(charactersrightwalk, false);
theanim.GetComponent<Animator>().SetBool(charactersfrontidle, false);
theanim.GetComponent<Animator>().SetBool(charactersbackidle, false);
theanim.GetComponent<Animator>().SetBool(charactersrightidle, false);
theanim.GetComponent<Animator>().SetBool(charactersleftidle, false);
LeftDirection = false;
RightDirection = false;
BackDirection = false;
FrontDirection = true;
characters.transform.localPosition = new Vector3(characters.transform.localPosition.x, characters.transform.localPosition.y, characters.transform.localPosition.- 0.05f * MoveSpeed);
}
if (Input.GetKey("left") && !(Input.GetKey("down") || Input.GetKey("up") || Input.GetKey("right")))
{
theanim.GetComponent<Animator>().SetBool(charactersfrontwalk, false);
theanim.GetComponent<Animator>().SetBool(charactersbackwalk, false);
theanim.GetComponent<Animator>().SetBool(charactersleftwalk, true);
theanim.GetComponent<Animator>().SetBool(charactersrightwalk, false);
theanim.GetComponent<Animator>().SetBool(charactersfrontidle, false);
theanim.GetComponent<Animator>().SetBool(charactersbackidle, false);
theanim.GetComponent<Animator>().SetBool(charactersrightidle, false);
theanim.GetComponent<Animator>().SetBool(charactersleftidle, false);
LeftDirection = true;
RightDirection = false;
BackDirection = false;
FrontDirection = false;
characters.transform.localPosition = new Vector3(characters.transform.localPosition.- 0.05f * MoveSpeed, characters.transform.localPosition.y, characters.transform.localPosition.z);
}
if (Input.GetKey("right") && !(Input.GetKey("down") || Input.GetKey("up") || Input.GetKey("left")))
{
theanim.GetComponent<Animator>().SetBool(charactersfrontwalk, false);
theanim.GetComponent<Animator>().SetBool(charactersbackwalk, false);
theanim.GetComponent<Animator>().SetBool(charactersleftwalk, false);
theanim.GetComponent<Animator>().SetBool(charactersrightwalk, true);
theanim.GetComponent<Animator>().SetBool(charactersfrontidle, false);
theanim.GetComponent<Animator>().SetBool(charactersbackidle, false);
theanim.GetComponent<Animator>().SetBool(charactersrightidle, false);
theanim.GetComponent<Animator>().SetBool(charactersleftidle, false);
LeftDirection = false;
RightDirection = true;
BackDirection = false;
FrontDirection = false;
characters.transform.localPosition = new Vector3(characters.transform.localPosition.+ 0.05f * MoveSpeed, characters.transform.localPosition.y, characters.transform.localPosition.z);
}


}

}

 

 

 

 

 

 

 

 

Share this post


Link to post
Share on other sites

Use the Input Manager (edit->Project Settings->Input) to set up Axes and Buttons with alternative positive/negative inputs, then call Input.GetAxis(string axisName) or Input.GetButton(string buttonName) instead of Input.GetKey()

Share this post


Link to post
Share on other sites

Instead of doing a whole lot of this:

if (Input.GetKey("right") && !(Input.GetKey("down") || Input.GetKey("up") || Input.GetKey("left")))

 

Calculate your axis and switch() on the result.

Edited by Khatharr

Share this post


Link to post
Share on other sites

i try using the enum to change the animation of the game, but i have some error which is "Find is not allowed to be called from a MonoBehaviour constructor (or instance field initializer), call it in Awake or Start instead. Called from MonoBehaviour 'Taoistmovement1'." and below is my script.

 

 

 

using UnityEngine;
using System.Collections;
 
public class Taoistmovement1 : MonoBehaviour
{   
 
    enum DirectionEnum
    {
        forward,
        back,
        left,
        right,
     //   leftidle,
    //    rightidle,
    //    backidle,
        frontidle
    }
 
    float MoveSpeed = 4.5f ;
 
    public string charactersname = "taoistmoveobject";
    public string charactername = "taoist";
 
    // change the string to your parameter     
 
    string charactersfrontwalk = "FrontWalk";
    string charactersbackwalk = "BackWalk";
    string charactersleftwalk = "LeftWalk";
    string charactersrightwalk = "RightWalk";
    string charactersfrontidle = "FrontIdle";
    string charactersbackidle = "BackIdle";
    string charactersrightidle = "RightIdle";
    string charactersleftidle = "LeftIdle";
 
    GameObject characters = GameObject.Find("taoistmoveobject");
    GameObject anim = GameObject.Find("taoist");
 
    Animator theAnimator;
    DirectionEnum currentDirection1;
    //   DirectionEnum currentDirection2;
    //  DirectionEnum currentDirection3;
    // DirectionEnum currentDirection4;
 
 
  
    void Awake()
    {
        theAnimator = anim.GetComponent<Animator>();
        currentDirection1 = DirectionEnum.frontidle;
      //   currentDirection2 = DirectionEnum.rightidle;
       //  currentDirection3 = DirectionEnum.backidle;
       //  currentDirection4 = DirectionEnum.frontidle;
    }
 
    void Update()
    { 
 
    characters.transform.localPosition = new Vector3(characters.transform.localPosition.x + 0.05f * MoveSpeed, characters.transform.localPosition.y, characters.transform.localPosition.z);
 
    }
 
    public void updateDirection(float h, float v)
    {
        if (h != 0)
        {
            if (h > 0)
            {
                changeDirection(DirectionEnum.left);
                
            }
            else
            {
                changeDirection(DirectionEnum.right);
               
            }
               
        }
        else if (v != 0)
        {
            if (h > 0)
            {
               
                changeDirection(DirectionEnum.forward);
            }
 
            else
            {
                
                changeDirection(DirectionEnum.back);
            }
                
        }
        else
        {   
                //changeDirection(DirectionEnum.rightidle);
               // changeDirection(DirectionEnum.backidle);
                changeDirection(DirectionEnum.frontidle);
            }
        }
    
 
    private void changeDirection(DirectionEnum dir)
    {
        if (dir == DirectionEnum.left)
            theAnimator.SetBool(charactersleftwalk, true);
        else if (dir == DirectionEnum.right)
            theAnimator.SetBool(charactersrightwalk, true);
        else if (dir == DirectionEnum.forward)
            theAnimator.SetBool(charactersfrontwalk, true);
        else if (dir == DirectionEnum.back)
            theAnimator.SetBool(charactersbackwalk, true);
        else if (dir == DirectionEnum.frontidle)
            theAnimator.SetBool(charactersfrontidle, true);
 
    }
 
  
}

Share this post


Link to post
Share on other sites
but i have some error which is "Find is not allowed to be called from a MonoBehaviour constructor (or instance field initializer), call it in Awake or Start instead.

...and have you tried moving the call(s) to Find to either the Awake or Start functions? 

Edited by Lactose

Share this post


Link to post
Share on other sites
Find is not allowed to be called from a MonoBehaviour constructor (or instance field initializer), call it in Awake or Start instead. Called from MonoBehaviour 'Taoistmovement1'."

Well, the error seems pretty clear. If you look for "Find", there are two calls:

GameObject characters = GameObject.Find("taoistmoveobject");
GameObject anim = GameObject.Find("taoist");

They are similar, and apparently, you cannot find things while creating the object. So instead, just declare the variables without assigning anything, or assign "null" or whatever it is called in Mono/C#. Then do the 'Find' in one of the suggestions that the error says you should use(check the documentation which one is called first, I never used Mono, so I cannot tell).

 

What happens then, is that the object is constructed, but "characters" and "anim" have no valid value yet. When the object is "started" or "woken" (check the documentation to see what that means), you assign the correct value, and it should all be running (for as far as these two variables are concerned).

 

It has nothing to do with your enum, as far as I can see.

 

 

As for future posts, please use the "code" property to paste code in a post. You do that by 1. Paste the code in the post. 2. Select all code  3. Click on the 'code' property, the "<>" symbol in the bottom row of the editor. It makes code so much more readable if you preserve indenting.  Thank you.

Edited by Alberth

Share this post


Link to post
Share on other sites

You do that by 1. Paste the code in the post. 2. Select all code  3. Click on the 'code' property, the "<>" symbol in the bottom row of the editor. It makes code so much more readable if you preserve indenting.  Thank you.

 

The more complex problem you solved and got right in the main post, but the simple issue of pasting code...

you got the order wrong, should be 

1. select and copy your code

2. click <>

3. paste your code

 

posting the above would have been unnecessary but i posted for sake of @[member='pch93']

 

Still I upvoted you as for your insightful main post

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Announcements

  • Forum Statistics

    • Total Topics
      628354
    • Total Posts
      2982231
  • Similar Content

    • By koto
      Hello,

      I'm developing a roguelike game. Currently it's in a playable state and I think it's a high time to show it and hopefully get some feedback.

      The game has following features:
      - turn based
      - procedurally generated levels
      - tons of loot
      - spells system
      - trading system
      - some quests
      - some limited crafting
      - door/key/lever mechanics
      - quite a lot of different (look/behaviour) monsters

      Trailer:
      Demo: http://kotogames.com/wp-content/uploads/2017/11/OuaD_demo.zip
      Regards,
      Tom
       
    • By Afambus
      Hello,
      I have a full experienced team that looking for a composer and another 3d designer, im expecting as much from you there are other people in this server that can help you we have a determined team, we have done many things in our game there is more information in the discord about everyone and there skills there region and about the game, if you join the discord pm asap
      https://discord.gg/b2teN3m
    • By BAG Labs
      Mobile SoS

      Platform: Android
      Genre: Board
      Link: Google Play
       
      This games sharpen memory and test your strategies to place S-O-S pattern within time limit and serve 3 difficulties as Easy, Normal, and Hard.

      Goals of the game is to put S-O-S words in patterns (Horizontal, Vertical, and Diagonal) alternately with enemy.
       
      Features:
      Single Player Multiplayer Achievements Leaderboards  
       
      Screenshot:

       

       

       
      Link: Google Play
       
      Please help us improve this game with review
    • By abarnes
      Hello!
      I am a game development student in my second year of a three year program and I would like to start building my portfolio. I was thinking of creating some games to show what I can do to potential employers since I wont have any work related experience when I graduate. But as I'm sure you all know there are tons of ways to approach developing/designing a game and I'm curious if anyone had any insight as to any "standards" that come with this? Is it okay to use game engines like Unity, Unreal, Game Maker etc? Or would it be better to make a game from scratch to better show case your skills? Any and all advice will be greatly appreciated!
    • By Hilster
      Hello 2D Artists,
      I've started making a 2D Puzzle Adventure game for mobile and I'm looking for someone who would want in on creating assets for the game. The core of the programming is pretty much complete, you can walk within the grid laid out and push boxes, when there is an object on top of a pressure pad it will activate the linked objects or if there is one object with multiple linked pressure pads it requires you to activate all points for the object to become active. 

      The level iteration for the game is quick and simple, a Photoshop file that is made of individual pixels that represents objects is put into the game and it creates the level out of those pixels with the assigned objects.
      The objects that need sprites created so far is the character, box, pressure pad, door, trap door, the walls, the stairs and the tiled background.
      I intend to add more objects so the amount I'd like to add will be extended.
      My motivations for posting here is to have something that looks nice to be able to display on my portfolio, so if you're looking for a working game that you can place your art into and improve the look of your portfolio then we're in business.
      Please reply with a few past examples of your art below and I'll be in touch!
  • Popular Now