0
\$\begingroup\$

I got my player movement script, joystick movement script, and keyboard movement script. The question is I have no idea how to change the keyboard movement script to the virtual joystick script. The main solution I face is my player character has 4 views which is left, right, front, back and 3 states which is idle, dead, and walking. I want my joystick movement input same as the keyboard movement as below. I got some error in my code which is

"Find is not allowed to be called from a MonoBehaviour constructor (or instance field initializer), call it in Awake or Start instead. "

Can someone teach me why I am getting this error?

using UnityEngine;
using System.Collections;

public class TaoistMovement : 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);

}

This is the left joystick

public class JoystickMove : MonoBehaviour ,IDragHandler,IEndDragHandler
{


    public static JoystickMove instance=null;

    public float _speed=6f;

    [Tooltip("the joystick radius ")]
    public float R=50f;

    private float _r;

    private Vector2 centerPos;

    private float _h;
    private float _v;

    public float H {
        get{return _h;}
    }
    public float V {
        get{return _v;}
    }

    void Awake() {

        if(instance!=null) {
            Destroy(this.gameObject);
        } else {
            instance=this;
        }



    }
    void Start() {

        _r=1f*Screen.width/960f*R; //this to calculate the scale of screen

        centerPos=GetComponent<RectTransform>().position;

    }

    void SetHAndF(Vector2 pos){ // Horizontall and Vertical axes

        Vector2 diff=pos-centerPos;
        float distance=diff.magnitude;


        if(distance>_r) {
            pos=centerPos+diff/distance*_r;
        }
        GetComponent<RectTransform>().position=pos;


        Vector2 move=pos-centerPos;

        _h=move.x;
        _v=move.y;

    }

    public void OnDrag(PointerEventData data)
    {   

        Vector2 newPos =new Vector2(data.position.x-30f,data.position.y-30f);



        SetHAndF(newPos);



    }

    public void OnEndDrag(PointerEventData data){

        Debug.Log("End Drag"+centerPos);
        GetComponent<RectTransform>().position=centerPos;
        SetHAndF(centerPos);


    }

}

This is the rotate class joystick

  using UnityEngine;
  using System.Collections;
  using UnityEngine.EventSystems;



public class JoystickRotate : JoystickMove
{

    public static JoystickRotate instance=null;



    void Awake(){

        if(instance!=null){
            Destroy(this.gameObject);
        }else{
            instance=this;
        }

    }

this is for fire

    public class JoystickFire :  MonoBehaviour,IPointerDownHandler,IPointerUpHandler 
    {

        public static JoystickFire instance=null;

        private bool _fire=false;
        public bool Fire{
            get{return _fire;}
        }


        void Awake(){

            if(instance!=null){
                Destroy(this.gameObject);
            } else {
                instance=this;
            }

        }



        public void OnPointerDown(PointerEventData data){
            _fire=true;
        }

        public void OnPointerUp(PointerEventData data){
            _fire=false;
        }
    }
\$\endgroup\$
10
  • \$\begingroup\$ I tried to edit your post but there are a lot of mistake here and there in the code. Please read your post again and fix the formatting. \$\endgroup\$
    – Stud
    Commented Nov 28, 2016 at 16:46
  • \$\begingroup\$ @Stud i already edited my formatting. sorry for the wrong formatting.. \$\endgroup\$
    – 9031313
    Commented Nov 29, 2016 at 0:38
  • \$\begingroup\$ @stud can i have ur fb id or email address?so that for me to easy contact you regards to my question. \$\endgroup\$
    – 9031313
    Commented Nov 29, 2016 at 5:14
  • \$\begingroup\$ I'm not sure to understand your question. Do you want to be able to control your character with a keyboard and a joystick concurrently and you're wondering how to mix that? \$\endgroup\$
    – Stud
    Commented Nov 29, 2016 at 8:01
  • \$\begingroup\$ i just want to delete the keyboard element in the code and link with the joystick to do the shooting and movement. but i got no idea how to link it. \$\endgroup\$
    – 9031313
    Commented Nov 29, 2016 at 8:25

2 Answers 2

1
\$\begingroup\$

The error is because you are using GameObject.Find in the constructor section of your class:

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

You should have:

GameObject characters;
GameObject anim;

And then in Awake():

characters = GameObject.Find("taoistmoveobject");
anim = GameObject.Find("taoist");
\$\endgroup\$
0
\$\begingroup\$

I read your code again and it looks like what you want is to update your animation based on the value received from the joysticks?

From what I see, you only have 4 directions. The first thing you should do is translate the analogical values from the stick into a direction that you can use to chose between your 4 animations. The easiest way to do this is to discard one of the inputs that you receive and use the sign of the one you keep to determine the direction. For example, if h = 0.2 and v = -0.8, you'll have direction = (0, -0.8) which means vertical negative.

When you have your direction, you can set the correct Boolean on the animator and move your character accordingly. Instead of a lot of Boolean, you should consider an enum to switch between directions.

Here is some code to show how you could do this. It is poorly written but it is clear I think.

  enum DirectionEnum {
    forward,
    back,
    left,
    right,
    idle
  }

  Animator theAnimator;
  DirectionEnum currentDirection;

  void Awake()
  {
    theAnimator = theanim.GetComponent<Animator>();
    currentDirection = idle;
  }

  void Update()
  {
    character.transform.localPosition = returnSpeedForCurrentDirection();
  }

  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.idle);
    }
  }

  private void changeDirection(DirectionEnum dir)
  {
    theAnimator.SetBool(currentDirection, false);
    currentDirection = dir;
    theAnimator.SetBool(currentDirection, true);
  }

  private Vector3 returnSpeedForCurrentDirection()
  {
    //Compute your speed depending on the current direction
  }
\$\endgroup\$
10
  • \$\begingroup\$ this coding is replace to my character movement script right? \$\endgroup\$
    – 9031313
    Commented Dec 11, 2016 at 8:50
  • \$\begingroup\$ This is more of an example. You should try to understand how it works and modify your code accordingly. I'm not here to do your work, I'm just trying to explain. \$\endgroup\$
    – Stud
    Commented Dec 11, 2016 at 9:41
  • \$\begingroup\$ for the idle part, i got 4 different animation for idle. in the enum, i need to declare all side of idle direction? \$\endgroup\$
    – 9031313
    Commented Dec 12, 2016 at 10:08
  • \$\begingroup\$ It depends on how you handle your animations in Unity's animation tool. If you have 4 different animations with 4 different boos to chose between them, then yes, adding the different idles in the enum is one way to do this. \$\endgroup\$
    – Stud
    Commented Dec 12, 2016 at 10:50
  • \$\begingroup\$ means that i need to add if else statement in the changeDirection(DirectionEnum.idle); statement to change different idle side? \$\endgroup\$
    – 9031313
    Commented Dec 12, 2016 at 11:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .