Jump to content
  • Advertisement
Sign in to follow this  
pch93

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

This topic is 574 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!