1.让cube沿着矩形四个点运动

using System.Collections;
using System.Collections.Generic;
using UnityEngine; public class cube : MonoBehaviour { // Use this for initialization
private Vector3 vec;
float time = ;
void Start () {
vec = transform.position;//存取坐标
}
int speed = ;
float speed2 = ;
// bool b = true;
// Update is called once per frame
void Update () {
//time += Time.deltaTime;//计时器
//移动 transform.localScale = new Vector3(,,);
transform.Translate(new Vector3(0.02f * speed, , speed2));
if (transform.position.x >=)
{
speed = ;
speed2 = -0.02f;
}
if (transform.position.z<- )
{
speed = -;
speed2 = ; }
if (transform.position.x < -)
{
speed = ;
speed2 = 0.02f;
}
if (transform.position.z >)
{
transform.position = vec;
speed = ;
speed2 = ;
} //transform.Rotate(0, speed, 0);
}
}

2.cube之按键操作

//transform.Rotate(Vector3.right,50,Space.World);
自转// transform.RotateAround(Vector3.right,5);//只有世界坐标下的运动
transform.RotateAround(Vector3.zero,Vector3.up,);//围绕别人转
//if (Input.GetKey("w"))
//{s
// transform.Translate(Vector3.forward);//局地坐标(相对坐标)(0,0,1)
//}
//if (Input.GetKey("s"))
//{
// transform.Translate(Vector3.back);//世界坐标,绝对坐标(0,0,-1)
//}
//if (Input.GetKey("a"))
//{
// transform.Rotate(Vector3.down);//(0,-1,0)
//}
//if (Input.GetKey("d"))
//{
// transform.Rotate(Vector3.up);//(0,1,0)
//}
// transform.Translate(Vector3.right);//(1,0,0)
// transform.Translate(Vector3.left);//(-1,0,0) //if (Input.GetKey(KeyCode.W))
//{
// Debug.Log("摁下了W键");
//}
//if (Input.GetKeyDown(KeyCode.W))
//{
// Debug.Log("Down了一下");
//}
//if (Input.GetKeyUp(KeyCode.W))
//{
// Debug.Log("Up了一下");
//}
//if (Input.GetKey(KeyCode.Space))
//{
// Debug.Log("摁下了空格键");
//}
//if (Input.GetKeyDown(KeyCode.Space))
//{
// Debug.Log("Down了一下");
//}
//if (Input.GetKeyUp(KeyCode.Space))
//{
// Debug.Log("Up了一下");
//}
//if (Input.GetMouseButton(0))
//{
// print("持续摁下了鼠标左键");
//}
//if (Input.GetMouseButtonDown(0))
//{
// print("摁下了鼠标左键");
//}
//if (Input.GetMouseButtonUp(0))
//{
// print("抬起了鼠标左键");
//}
//if (Input.GetMouseButton(1))
//{
// print("摁下了鼠标右键");
//}
//if (Input.GetMouseButton(2))
//{
// print("摁下了鼠标中键");
//}

3.射线检测

Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;//检测信息
if (Physics.Raycast(ray,out hit))
{
print(hit.point);
if (hit.collider.name == "Cube")
{
hit.collider.gameObject.GetComponent<MeshRenderer>().material.color = Color.red;
} }

4.cube岁鼠标移动

public GameObject obj;
Vector3 pos;
bool b = false;
// Use this for initialization
void Start () { } // Update is called once per frame
void Update ()
{
// transform.LookAt(obj.transform);
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;//检测信息
if (Physics.Raycast(ray, out hit))
{
if (Input.GetMouseButtonDown())
{
pos = hit.point;
transform.LookAt(new Vector3(pos.x, pos.y + 0.5f, pos.z));//lookat
b = true;
}
}
if (b)
{
transform.Translate(,,*Time.deltaTime);
if (Vector3.Distance(transform.position,pos)<1f)
{
b = false;
print();
}
}
}

5.

点击cube随机变颜色,掉下去
using System.Collections;
using System.Collections.Generic;
using UnityEngine; public class RayDiao : MonoBehaviour {
GameObject obj;
// Use this for initialization
void Start () { }
Color[] gameclor = { Color.black, Color.blue, Color.cyan, Color.green, Color.red, Color.yellow };//定义颜色数组
//Random num = new Random();
// Update is called once per frame
void Update () {
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;//检查信息
if (Physics.Raycast(ray,out hit))
{
if (Input.GetMouseButtonDown())//判断是否点击了鼠标左键
{ if (hit.collider.tag.Equals("cube"))//判断是否点击的是cube
{
hit.collider.gameObject.GetComponent<MeshRenderer>().material.color = gameclor[Random.Range(, gameclor.Length)];//随机添加颜色 //hit.collider.gameObject.GetComponent<Rigidbody>().useGravity = true;//使添加的刚体激活
hit.collider.gameObject.AddComponent<Rigidbody>();//增加刚体
// obj= hit.collider.gameObject; }
}
}
//if (obj != null)//不是空
//{
// obj.transform.Translate(0, -1*Time.deltaTime, 0);//以(-1*Time.deltaTime)的速度下降
//} }
}

6.

添加cube的方法,如消消乐
using System.Collections;
using System.Collections.Generic;
using UnityEngine; public class creatCube : MonoBehaviour {
//public GameObject cube;
// Use this for initialization Color [] c = { Color.black, Color.blue, Color.clear, Color.cyan, Color.green, Color.red, Color.yellow };
void Start () {
/* GameObject obj = Instantiate(Resources.Load("Cube")) as GameObject;*///另一种生成cube方法 //GameObject obj = Instantiate(cube);//第二种方式生成cube,需要预制体
//GameObject obj = GameObject.CreatePrimitive(PrimitiveType.Cube);//生成cube
// obj.transform.position = Vector3.one;//one表示坐标(1,1,1)
//obj.transform.rotation = Quaternion.Euler(0, 45, 0);//角度设置
// obj.transform.localScale = new Vector3(2, 2, 2);//大小设置
//int index =obj.name.IndexOf("(");//字符串截取
//obj.name=obj.name.Substring(0, index); for (int i = ; i<; i++)
{
for (int j = ; j < ; j++)
{
GameObject obj = GameObject.CreatePrimitive(PrimitiveType.Cube);//生成cube
obj.transform.position = new Vector3(i, j, );//生成位置
obj.transform.localScale = new Vector3(0.9f, 0.9f, 0.9f);//生成大小
obj.GetComponent<MeshRenderer>().material.color = c[Random.Range(, c.Length)];//随机颜色
}
}
}
// Update is called once per frame
void Update () { }
}

7.

鼠标点击变大
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
if (hit.collider.gameObject.tag == "cube" &&Input.GetMouseButtonDown())
{
cubeObj= hit.collider.gameObject;
cubeObj.transform.localScale = new Vector3(,,);
}
}
if (Input.GetMouseButtonUp()&&cubeObj)
{
cubeObj.transform.localScale = new Vector3(0.9f, 0.9f, 0.9f);
}

8.

消消乐--单消————列表方法
using System.Collections;
using System.Collections.Generic;
using UnityEngine; public class Raydaxiao : MonoBehaviour {
GameObject obj;
public GameObject cube;//为预制体生成cube做准备
//Color[] c = { Color.black, Color.blue, Color.clear, Color.cyan, Color.green, Color.red, Color.yellow };
public Material[] color;//材质数组
Vector3 vec;
List<GameObject> cubelist = new List<GameObject>();
//int num = 0;
// Use this for initialization
void Start () {
for (int i = ; i < ; i++)
{
for (int j = ; j < ; j++)
{
GameObject obj = Instantiate(cube);//需要预制体式添加cube
obj.transform.position = new Vector3(i, j, );
obj.transform.localScale = new Vector3(0.9f, 0.9f, 0.9f);
//obj.GetComponent<MeshRenderer>().material.color = c[Random.Range(0, c.Length)];//上颜色
obj.GetComponent<MeshRenderer>().material = color[Random.Range(, color.Length)];//随机材质
obj.tag = "cube";//定义名字为cube
//obj.AddComponent<Rigidbody>();//添加刚体
//num++;
//obj.name = "Cube" + num;//生成不同名字的cube
cubelist.Add(obj);
}
} } // Update is called once per frame
void Update () {
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if(Physics.Raycast(ray,out hit))
{
if (hit.collider.gameObject.tag=="cube"&&Input.GetMouseButtonDown())
{
obj = hit.collider.gameObject;
obj.transform.localScale = new Vector3(, , );
vec = obj.transform.position;//获取当前cube的坐标
//Destroy(obj);//销毁cube
Destroy(obj);
cubelist.Remove(obj);
foreach (GameObject item in cubelist)
{
if (item.transform.position.x == vec.x && item.transform.position.y > vec.y)
{
//item.transform.position -= new Vector3(0, 1, 0);//另一种
item.transform.position = new Vector3(vec.x, item.transform.position.y - , vec.z);
}//自动补齐
}
GameObject newobj = Instantiate(cube);//重新生成cube
newobj.GetComponent<MeshRenderer>().material = color[Random.Range(, color.Length)];//添加颜色
newobj.transform.position = new Vector3(vec.x, , vec.z);//newcube的坐标
newobj.transform.localScale = new Vector3(0.9f, 0.9f, 0.9f);
//newobj.AddComponent<Rigidbody>();//添加刚体
//newobj.GetComponent<Rigidbody>().freezeRotation = true;
//newobj.GetComponent<BoxCollider>().size = new Vector3(1.1f, 1.1f, 1.1f); cubelist.Add(newobj);
}
//if (Input.GetMouseButtonUp(0)&&obj)
//{ // obj.transform.localScale = new Vector3(0.9f,0.9f,0.9f);
//}
}
}
}

9.

消消乐--单消--数组方法
using System.Collections;
using System.Collections.Generic;
using UnityEngine; public class TextScripts : MonoBehaviour
{
// List<GameObject> list = new List<GameObject>();
GameObject[,] array = new GameObject[,];
bool b = false;
GameObject cube;
Color[] c = { Color.red,Color.blue,Color.yellow,Color.green};
public Material[] m;
// Use this for initialization
void Start()
{
for (int i = ; i < ; i++)
{
for (int j = ; j < ; j++)
{
GameObject obj = GameObject.CreatePrimitive(PrimitiveType.Cube);
obj.transform.position = new Vector3(i, j, );
obj.transform.localScale = new Vector3(0.9f, 0.9f, 0.9f);
obj.tag = "cube";
obj.GetComponent<MeshRenderer>().material = m[Random.Range(,)];
array[i, j] = obj;
}
}
} // Update is called once per frame
void Update()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray,out hit))
{
if (hit.collider.tag=="cube"&&Input.GetMouseButtonDown())
{
cube = hit.collider.gameObject;
Vector3 pos = cube.transform.position;
int a = (int)pos.x;
int b = (int)pos.y;
Destroy(cube);
for (int i = b+; i < ; i++)//从销毁的上边第一个开始
{
array[a, i].transform.position -= new Vector3(,,);/到比销毁物体坐标的y大的物体把其y-
array[a, i - ] = array[a,i];//坐标统一减去1,导致索引对应物体改变
} GameObject obj = GameObject.CreatePrimitive(PrimitiveType.Cube);
obj.transform.position = new Vector3(pos.x, , );
obj.transform.localScale = new Vector3(0.9f, 0.9f, 0.9f);
obj.tag = "cube";
obj.GetComponent<MeshRenderer>().material = m[Random.Range(, )];
// list.Add(obj);
array[a, ] = obj;
}
} }
}

10.

技能冷却:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI; public class CoolSkill : MonoBehaviour
{
public Image image;
float time;
float f;
public Text text;
bool b = false;
bool bb = true;
// Use this for initialization
void Start()
{
image = image.GetComponent<Image>();
text = text.GetComponent<Text>();
image.fillAmount = ;//默认可以发出技能
}
public void GetBool()
{
if (bb)//限制技能开启后才能使用
{
b = true;
bb = false;
}
}
// Update is called once per frame
void Update()
{
if (b)
{
time += Time.deltaTime;
if (time <= )//技能控制在5秒冷却
{
f = ( - time);//5秒倒计时
image.fillAmount = (f) / ;//image也在360度递减
text.text = (f).ToString();//文本输出倒计时
if (f < 0.1f && f >= )/控制在0.1秒以内结束时才可以重新开启技能
{
bb = true;//重新开启技能Button可以点击了
}
}
else
{
time = ;//超过5秒后时间置零
b = false;/tton点击后又可以计时了
} } }
}

unity之让obj旋转自转等操作的更多相关文章

  1. Eigen库实现简单的旋转、平移操作

    本来课程要求用GUI界面来实现Eigen的旋转.平移操作的,但是接触GUI编程时间太短,虽然要求很简单,但是做了几天还是没有完成.就把命令行下面的简单的贴一下吧. main.cpp #include ...

  2. Unity摄像机围绕物体旋转两种实现方式

    第一种,使用Transform 函数 RotateAround. 代码如下: public Transform target;//获取旋转目标 private void camerarotate() ...

  3. 【转】Unity 之 移动设备的触控操作

    http://blog.csdn.net/anyuanlzh/article/details/18367941 这篇博文将简单的记录,如何用unity处理在移动设备上的触控操作.    iOS和And ...

  4. maya和Unity中的坐标系旋转

    maya软件是用的右手坐标系,默认旋转顺序是ZYX,即先绕Z轴旋转,再绕Y轴旋转,最后绕X轴旋转. 比如在maya软件中,右侧的旋转顺序是可选的,默认的选择是“XYZ”,其实物体旋转顺序是倒着念,即上 ...

  5. Android Bitmap 缩放 旋转 水印 裁剪操作

    在android当中,Bitmap代表一个图片,里面封装了图片相关的信息. 一.将图片进行缩放操作 1.获得Bitmap对象 Bitmap bitmap = BitmapFactory.decodeR ...

  6. unity还原three之旋转

    http://www.360doc.com/content/16/0829/14/12282510_586760119.shtml unity使用左手坐标系,另外在做旋转的时候必须弄清楚旋转坐标轴和旋 ...

  7. unity鼠标拖动物体旋转

    using System.Collections; using System.Collections.Generic; using UnityEngine; /// <summary> / ...

  8. 运用Unity结合PolicyInjection实现拦截器[结合操作日志实例]

    上一篇文章我们通过Unity自身Unity.InterceptionExtension.IInterceptionBehavior实现一个有系统关异常日志记录:解决代码中到处充满的异常记录的代码: 本 ...

  9. FairyGUI编辑器的和unity里的Obj对应关系

    1.在FairyGUI官网上下载好unity的工程,用FairyGUI编辑器打开它的官方案例 2.在FairyGUI编辑器和Unity中,从一个最简单的示例"Bag"着手.     ...

随机推荐

  1. Nginx配置跨域请求 Access-Control-Allow-Origin *

    当出现403跨域错误的时候 No 'Access-Control-Allow-Origin' header is present on the requested resource,需要给Nginx服 ...

  2. piano class 12

    1,不要记谱子,眼睛要一直看着谱子,手指凭感觉找琴键 2,弹的时候一定要按照谱子上标出来的指法弹奏,很重要 3,两只手要会跷跷板弹奏 4,八分音符,一般第二个会比第一个弱一点,但是要看自己感觉 5,慢 ...

  3. [math][mathematica] mathematica入门

    快速入门手册: 只找到了个中文的快速入门: https://www.wolfram.com/language/fast-introduction-for-programmers/zh/?source= ...

  4. 转:环绕通知返回值 object 类型

    遇到 AOP 环绕通知报错  “return value from advice does not match primitive return type for: public boolean” 百 ...

  5. 转:EL表达式的11个内置对象

    原文地址:https://blog.csdn.net/qq_17045385/article/details/54799998 EL是JSP内置的表达式语言 JSP2.0开始,不让再使用Java脚本, ...

  6. 转:SAX解析的characters方法被多次调用

    原文地址:https://blog.csdn.net/liuxiaoddd/article/details/28885177 android SAX解析的characters方法被多次调用 同理 试用 ...

  7. Java之旅_高级教程_Java Mysql连接(1)

    工具:Eclipse .mysql5.7 MySQL连接驱动:mysql-connector-java-5.1.27.jar    获取地址:https://dev.mysql.com/downloa ...

  8. 【PyQt5-Qt Designer】QSpinBox-微调框

    QSpinBox-微调框 QSpinBox类提供了一个微调框小部件. QSpinBox被设计为处理整数和离散值集合(例如,月份名称):使用QDoubleSpinBox 可以作为浮点数的调整. QSpi ...

  9. u-boot 编译,启动流程分析,移植

    分析u-boot-1.1.6 的启动流程 移植u-boot 2012.04版本到JZ2440开发板 源码百度云链接:https://pan.baidu.com/s/10VnxfDWBqJVGY3SCY ...

  10. oracle中in和exists的区别

    IN适合于外表大而内表小的情况:EXISTS适合于外表小而内表大的情况. 性能上的比较 比如Select * from T1 where x in ( select y from T2 ) 执行的过程 ...