注册会员 登录
Project1 返回首页

柳之一的6R微博 https://rpg.blue/?53316 [收藏] [复制] [分享] [RSS] 发表rm和论坛的琐事

日志

4/8方向主角控制,原型

已有 292 次阅读2017-6-15 17:39 |个人分类:unity2D

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


鸡蛋

鲜花

评论 (0 个评论)

facelist doodle 涂鸦笔

您需要登录后才可以评论 登录 | 注册会员

拿上你的纸笔,建造一个属于你的梦想世界,加入吧。
 注册会员
找回密码

站长信箱:[email protected]|手机版|小黑屋|无图版|Project1游戏制作

GMT+8, 2024-4-24 16:56

Powered by Discuz! X3.1

© 2001-2013 Comsenz Inc.

返回顶部