20240人加入学习
(121人评价)
Unity初级案例 - 贪吃蛇(Unity2017.2.0)

制作于2017.12.16

价格 免费

其实 GetKeyDown 比 GetKey 更符合逻辑,因为转向是一次性的,玩家不可能通过持续按一个键来完成连续转向。

[展开全文]

分析:

蛇每次走多少,用一个 step值

 定义两个私有变量 下 x y,就是蛇下一步走多少的增量

 public float velocity  = 0.36f

public int step;

private int x;

private int y;

private Vector3 headPos;

 

 要调用move

InvokeRepeating("Move",0,velocity);//重复调用

x=step; y=0;//一开始就向右走

 

 

移动按键

void update{

if(Input.GetKey(KeyCode.W))

{

x=0; y=step;

}

 

if(Input.GetKey(KeyCode.S)){

x=0; y=-step;

}

if(Input.GetKey(KeyCode.A))

x=-step; y= 0;}

if(Input.GetKey(KeyCode.D)){
x= step; y =0 ;}

}

void Move(){
headpos =gameObject.transform.localPosition;

gameObject.transform.localPosition=new Vector3(headPos.x+x,headPos.y+y,headPos.z);

[展开全文]

蛇头及其移动

UGUI image 45,45 做成预制体

蛇头脚本

变量: 每格走的距离1   增量值 头坐标 间隔速度

move的方法. transform.localPosition(UGUI专用)直接赋值坐标

注意Vector3大写

监听 写在Update里!

if(不用getkeydown因为可以一直摁着

反复调用Move 再放进Start方法里!InvokeRepeating 只要传字符串即可 (方法,延时,间隔)间隔直接视作速度

Ad: 一上来就会动 设置xy初始值 start里

说明这里的update只管转向 我们在start里不断重复作step运动

 

[展开全文]

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SnakeHead_text : MonoBehaviour
{
    public float Velocity = 0.35f;
    public int steep;
    private int x;
    private int y;
    private Vector3 headPos;
    private void Start()
    {
        InvokeRepeating("Move", 0, Velocity);//重复调用,按照“Velocity”频率,在0秒后立即调用;
        x = steep;y = 0;//在开始时就向右移动
    }
    private void Update()
    {
        if (Input .GetKey (KeyCode.W ))
        {
            x = 0;y = steep;
        }
        if (Input.GetKey(KeyCode.S ))
        {
            x = 0; y = -steep;
        }
        if (Input.GetKey(KeyCode.A ))
        {
            x = -steep ; y = 0;
        }
        if (Input.GetKey(KeyCode.D ))
        {
            x = steep ; y = 0;
        }
    }
    void Move()
    {
        headPos = gameObject.transform.localPosition;
        gameObject.transform.localPosition = new Vector3(headPos.x + x, headPos.y + y, headPos.z);
    }
}
 

[展开全文]

按键按下一直赋值,invokerepeating控制调用频率

在start中,设置初始移动方向

[展开全文]

制作蛇头移动

image

创建脚本:snakehead

public float  vlocity=0.35f;//速度

public int step;

private vecto3 headpos;

private int x; 

private int y;//下一步要去的增量

void start()

{

invokerepete("传入要调用的函数方法",隔多久调用,速度)//重复调用

}

void update ()

{

}

 

void  move()

{

gameObect.transform.localpostion=new vector3( );

 

 }

 

 

[展开全文]

setNativesize. 添加到image后,调整为自身合适的大小。

全部yonglocationPosition,本地位置。

invokeRepeating.   

[展开全文]

1.这是一个纯UGUI游戏,所以蛇头会用UGUI来做

 

2.创建一个Image作为蛇头,做成Prefab

 

3.创建一个脚本SnackHead

注意:脚本名如果写错,删掉重建,否则既要改脚本名,又要改脚本内类名

 

4.删掉所有函数

  代码如下


  public float velocity;
  public int step;//蛇一格走多少
  private int x,y;//蛇头下一次的移动向量
  private Vector3 headPos;//蛇头的位置

  void Move{
      //把蛇头移动一个向量
      headPos =  gameObject.transform.localPosition
      gameObject.transform.localPosition = new Vector(headPos.x+x,head.y+y,headPos.z);

      } 
  void Update()
  {
    if(Input.GetKey(KeyCode.W)) 
       {x=0;y=step;}
    if(Input.GetKey(KeyCode.S))
       {x=0;y=-step;}
    if(Input.GetKey(KeyCode.A))
       {y=0;x=-step;}
    if(Input.GetKey(KeyCode.D))
       {y=0;x=step;}


}
  void start
  { 
     invokeRepeading("move",0,velocity);//每隔一段时间执行这个函数
     x=step;y=0;
  } 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  

[展开全文]

在Canvas里建Image,重命名为Sh,把sh02拖入场景,    Pos X:0,Pos Y:0 Width:45 Height:45,新建Prefabs,将Sh拖入该文件,点击Apply。新建Scripts文件,在该文件下建SnaKeHead,双击打开,敲代码:

 {

public float velocity=0.35f;

public int step;

private int x;

private int y;

private Vector3 headPos;

void Start()

{

  InvokeRepeating("Move",0,velocity);

x=step;y=0;

}

void Update()

{

if(Input.GetKey(KeyCode.W))

{

 x=0;y=step;

 }

if(Input.GetKey(KeyCode.S))

{

 x=0;y=-step;

 }

if(Input.GetKey(KeyCode.A))

{

 x=-step;y=0;

 }

if(Input.GetKey(KeyCode.D))

{

 x=step;y=0;

 }

 

 void Move() 

 {

headPos=gameObject.transform.localPosition;

gameObject.transform.localPosition=new Vector3(headPos.x+x,headPos.y+y)

}

}

保存代码,然后将代码挂上,点击Apply,调整Step:30。

[展开全文]

 localPosition是根据父节点为中心,如果没有父节点,localpositon和position是没有区别的

[展开全文]

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SnakeHead : MonoBehaviour {
    public float velocity = 0.35f;
    public int step;
    private int x;
    private int y;
    private Vector3 headPos;

    void Start()
    {
        InvokeRepeating("Move", 0, velocity);
        x = step;y = 0;
    }
    void Update()
    {
        if (Input.GetKey(KeyCode.W))
        {
            x = 0;y = step;
        }
        if (Input.GetKey(KeyCode.S))
        {
            x = 0; y = -step;
        }
        if (Input.GetKey(KeyCode.A))
        {
            x = -step;y = 0;
        }
        if (Input.GetKey(KeyCode.D))
        {
            x = step;y = 0;
        }
    }
    void Move()
    {
        headPos = gameObject.transform.localPosition;
        gameObject.transform.localPosition = new Vector3(headPos.x + x, headPos.y + y, headPos. z.);
            }

}
}

贪吃蛇头移动

[展开全文]

InvokeRepeating()每隔多久调用一次

[展开全文]

transform.position和transform.localPosition区别

position是根据世界原点为中心

localPosition是根据父节点为中心,如果没有父节点,localpositon就等同position

这里的sh的父级为Canvas

[展开全文]

每次多少步

x,y增量

localPosition

Update方法中

Input.Getkey()

InvokeRepeating("方法名",执行多久之后调用,每隔多久调用一次);

[展开全文]
  • UGUI 移动需要用localPosition,因为Canvas相对于世界坐标具有偏移量
  • Input.GetKey(KeyCode.W)
  • InvokeRepeating("Move",0,velocity)
[展开全文]

 InvokeRepeating("Move", 0, velocity);

重复调用Move方法,什么时候开始,重复的频率

Input.GetKey(KeyCode.W)

按下键盘的w建

[展开全文]

public int step;

private int x;

private int y;//增量值

private Vector3 headPos;

void Move()//移动代码

if(Input.GetKey(KeyCode.w))

{

}

 

{

headPos = gomeObject.transform.

localPosition;
  gameObject.transform.localPositio

on = new Vector3()
}

 

InvokeRepeating重复调用(调用的方法,多久调用, )

[展开全文]

gameObejct.transform

如果是一个ugui对象通过这样来调用,它返回的是一个RectTransform对象。

移动一个ugui对象,直接调用localPosition赋值就行了,因为position是世界坐标,而ugui的canvas又被默认设置了一个scalse所放置,大概是0.000003,所以不能用世界坐标来移动

[展开全文]

step的值设的太小的话,设为1的时候,蛇移动的速度非常的慢,会令人以为蛇动不了,可以把值设的大些如20  。

[展开全文]

授课教师

SiKi学院讲师

课程特色

图文(1)
视频(23)
下载资料(1)