using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PM : MonoBehaviour {
// Use this for initialization
///角色控制脚本
public float m_speed=1; //这个是定义的玩家的移动速度 之所以Public是因为为了方便对其进行调节 (public的属性和对象会在Unity中物体的脚本选项中显示出来 前提是你把脚本挂在了物体上)
private int dir=2;//角色面朝向3:上 0:下 1:左 2:右
public bool mapscrollswitch = true;//地图移动开关
public bool playermove=true;//主角允许移动
void Update () //这个是刷新的意思 以帧为单位的大概每刷新一次1/20秒
{
float movex = 0; //这个代表的是玩家在x轴上的移动
float movey = 0; //这个代表的是玩家在z轴上的移动
// 获取Player变量指定的对象的三围坐标
Vector3 player_postion = this.transform.position;
Vector3 screenPos = this.transform.localPosition;
// 获取X,Y,Z值
float x = player_postion.x;
float y = player_postion.y;
float z = player_postion.z;
//获取屏幕滚动视窗
float x1,x2,y1,y2;
Animator an = GetComponent<Animator> ();
GameObject gameObject = GameObject.Find ("Main Camera");//控制镜头的摄像机
GameObject gameObject1 = GameObject.Find ("1611_2015");//地图图片
if (playermove) {
// ifififif 连用就是把方向行走 if elseif elseif 连用就是4方向
if (Input.GetKey (KeyCode.W)) { //这个意思是"当按下W键时"//角色面朝向3:上 0:下 1:左 2:右
dir = 3;
an.SetInteger ("direction", dir);
movey += m_speed * Time.deltaTime; //物体获得在z轴方向上的增量 也就是向前
//movey+=m_speed *0.1f;
}
if (Input.GetKey (KeyCode.S)) { //按下S键时
dir = 0;
an.SetInteger ("direction", dir);
movey -= m_speed * Time.deltaTime; //后
}
if (Input.GetKey (KeyCode.A)) { //A键
dir = 1;
an.SetInteger ("direction", dir);
movex -= m_speed * Time.deltaTime; //左
}
if (Input.GetKey (KeyCode.D)) { //D键
dir = 2;
an.SetInteger ("direction", dir);
movex += m_speed * Time.deltaTime; //右
}
}
/////////////////////////////////////////////////////////////////////
if (mapscrollswitch){
gameObject.transform.Translate(new Vector3(movex,movey,0));
this.transform.Translate(new Vector3(movex,movey,0));
}
else
{
Vector3 pos = Camera.main.WorldToScreenPoint(player_postion);
//Debug.Log(player_postion.x+"#"+player_postion.y+"#"+pos.x);
//镜头随角色移动
x1=0f;x2=55f;y1=2.4f;y2=30.9f;
if((player_postion.x>=x1)&&(player_postion.x<=x2)&&(player_postion.y>=y1)&&(player_postion.y<=y2)){
gameObject.transform.Translate(new Vector3(movex,movey,0));
this.transform.Translate(new Vector3(movex,movey,0));
}
else if(((player_postion.x<x1)||(player_postion.x>x2))&&((player_postion.y>=y1)&&(player_postion.y<=y2)))
{
//Debug.Log ("d"+dir);
if ((dir == 0) || (dir == 3))
{
gameObject.transform.Translate (new Vector3 (movex, movey, 0));
this.transform.Translate (new Vector3 (movex, movey, 0));
}
else if ((dir==2)||(dir==1))
{
this.transform.Translate(new Vector3(movex,movey,0));
}
}
else if(((player_postion.y<y1)||(player_postion.y>y2))&&((player_postion.x>=x1)&&(player_postion.x<=y2)))
{
//Debug.Log ("d"+dir);
if ((dir == 0) || (dir == 3))
{
this.transform.Translate (new Vector3 (movex, movey, 0));
}
else if ((dir==2)||(dir==1))
{
gameObject.transform.Translate (new Vector3 (movex, movey, 0));
this.transform.Translate (new Vector3 (movex, movey, 0));
}
}
else {
//Debug.Log ("d"+dir);
//gameObject.transform.Translate(new Vector3(movex,movey,0));
this.transform.Translate(new Vector3(movex,movey,0));
}
}
//
if (Input.GetKeyUp (KeyCode.A) ) {
an.SetInteger ("direction", 5);
an.Play("idleleft");
}
if (Input.GetKeyUp (KeyCode.D) ) {
an.SetInteger ("direction", 5);
an.Play("idleright");
}
if (Input.GetKeyUp (KeyCode.W) ) {
an.SetInteger ("direction", 5);
an.Play("idleup");
}
if (Input.GetKeyUp (KeyCode.S) ) {
an.SetInteger ("direction", 5);
an.Play("idledown");
}
}
}