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;
}
}