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

制作于2017.12.16

价格 免费

gameObject.transform.localRotation= Quaternion.Euler(0,0,0);

gameObject.transform.localRotation= Quaternion.Euler(0,0,180);

gameObject.transform.localRotation= Quaternion.Euler(0,0,90);

gameObject.transform.localRotation= Quaternion.Euler(0,0,-90);

 

if(Input.GetKeyDown(KeyCode.Space))

{

CancelInvoke();//取消invoke(调用),先停住,在改数值

InvokeRepeating("Move",0,velocity -0.2f);

}

[展开全文]

UGUI 直接.transform 不需要GetCom。。。

旋转 位置等于四元数 加在转向里(x,y,z,)这里只用控制z轴  注意此时是LocalRotaion

一开始的初始方向要与旋转角度一致

Bug 不能直接180度转 会撞身子

按下加速 抬起恢复

这时的速度是Invoke的间隔参数

我们先用CancelInvoke()取消Invoke

再重写改速度。

[展开全文]

为什么不直接使用Horizontal,Vertical来控制上下左右移动?

float h = Input.GetAxis("Horizontal");      //前后移动
        transform.Translate(Vector3.right * h * Time.deltaTime * 10f);
        float v = Input.GetAxis("Vertical");      //上下移动
        transform.Translate(Vector3.up * v * Time.deltaTime * 10f);
四元数.旋转

gameObect.transform.localRotoin=Quaternion.Euler(0,0,180);//向后

gameObect.transform.localRotoin=Quaternion.Euler(0,0,90);//向右边

gameObect.transform.localRotoin=Quaternion.Euler(0,0,-90);//向左边

代码加速

if(input.Getkeydown(keycode.space)

{

cancelinvoke();

invokerepeat()

}

 

[展开全文]

1、头的方向问题,要与step一致,比如;按W时,step不能是负的

[展开全文]

Qaternion.euler();

全部使用localPosition.

不能立马回头。

控制一个物体自身的旋转最好不要用世界坐标。

自身的localppostion..

还需要先取消invokeReapeating..

 

 

[展开全文]

using UnityEngine;

public class snakehesd : 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=0;y=step;
    }
    void Update()
    {

if(Input.GetKeyDown(KeyCode.Space))

{

CancelInvoke();

InvokeRepeating("Move",0,velocity-0.2f);

}

if(Input.GetKeyUp(KeyCode.Space))

{

CancelInvoke();

InvokeRepeating("Move",0,velocity);

}

        if (Input.GetKey(KeyCode.W)&&y!=-step)
        {

 gameObject.transform.localRotation=Quaternion.Euler(0,0,0);
            x=0;y=step;
        }
        if (Input.GetKey(KeyCode.S)&&y!=step)
        {

 gameObject.transform.localRotation=Quaternion.Euler(0,0,180);
            x=0;y=-step;
        }
        if (Input.GetKey(KeyCode.A)&&x!=step)
        {

 gameObject.transform.localRotation=Quaternion.Euler(0,0,90);
            x=-step;y=0;
        }
        if (Input.GetKey(KeyCode.D)&&x!=-step)
        {

 gameObject.transform.localPlocalRotationosition=Quaternion.Euler(0,0,-90);
            x=step;y=0;
        }
    }
    void Move()
    {
        HeadPos=gameObject.transform.localPosition;
        gameObject.transform.localPosition=new Vector3(HeadPos.x+x,HeadPos.y+y,HeadPos.z);
    }
}

[展开全文]

1.完善蛇头方向朝向代码实现:

  gameObject.transform.localRotation = Quternion.Euler(0,0,90);

//保存Rotation的数据结构是一个四元数,所以想赋值需要把三维向量转换为四元数,用Quternion.Euler即可

//改变z轴,z轴朝内,故逆时针转

 

2.逆转头问题:

每次转头判断是否是逆转头

代码:if (Input.GetKey (KeyCode.W) && y != -Step) 

 

3.蛇头的加速

按下时加速:

抬起时回复:

        if (Input.GetKeyDown (KeyCode.Space))
        {
            CancelInvoke ();

//取消
            InvokeRepeating ("Move", 0, velocity-0.2f);
        }

        if (Input.GetKeyUp (KeyCode.Space))
        {
            CancelInvoke ();
            InvokeRepeating ("Move", 0, velocity);
        }

[展开全文]

 

 {

public float velocity=0.35f;

public int step;

private int x;

private int y;

private Vector3 headPos;

void Start()

{

  InvokeRepeating("Move",0,velocity);

x=0;y=step;

}

void Update()

{

 if(Input.GetKeyDown(KeyCode.Space))

{

CancelInvoke();

InvokeRepeating("Move",0,velocity-0.2f);

}

 if(Input.GetKeyUp(KeyCode.Space))

{

CancelInvoke();

InvokeRepeating("Move",0,velocity);

}

if(Input.GetKey(KeyCode.W)&&y!=-step)

{ gameObject.transform.localRotation=Quaternion.Euler(0,0,0);

 x=0;y=step;

 }

if(Input.GetKey(KeyCode.S)&&y!=step)

{

gameObject.tranform.localRotation=Quaternion.Euler(0,0,180);

 x=0;y=-step;

 }

if(Input.GetKey(KeyCode.A)&&x!=step)

{

gameObject.tranform.localRotation=Quaternion.Euler(0,0,90);

 x=-step;y=0;

 }

if(Input.GetKey(KeyCode.D)&&x!=-step)

{

gameObject.tranform.localRotation=Quaternion.Euler(0,0,-90);

 x=step;y=0;

 }

 

 void Move() 

 {

headPos=gameObject.transform.localPosition;

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

}

}

[展开全文]

quternion.euler(f,f,f);旋转

向上走不能立马向下

 

[展开全文]

蛇头旋转归正

四元数

避免蛇头的错误移动&&

CancelInvoke

[展开全文]
  • CancelInvoke可用于停止InvokeRepeating以修改一些参数
[展开全文]

1、设置一个ugui对象的角度,只能用四元素来设置,而不是一个vector3d,四元素是这样写

gameObject.transform.locatlRotaion = Quaternion .Euler(0,0,0) ;

 

[展开全文]

授课教师

SiKi学院讲师

课程特色

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

学员动态