纳金网

标题: 相机跟随/OnTrigger和List使用/控制物体移动和旋转 [打印本页]

作者: 狂风大尉    时间: 2014-6-24 10:34
标题: 相机跟随/OnTrigger和List使用/控制物体移动和旋转

Unity 相机跟随
  1. <span style="font-family:SimSun;font-size:14px;">using UnityEngine;
  2. using System.Collections;
  3. public class look : MonoBehaviour {
  4. public Transform target;
  5. // Use this for initialization
  6. void Start () {

  7. }

  8. // Update is called once per frame
  9. void Update () {

  10.   transform.LookAt (target.position);
  11. }
  12. }
  13. </span>
复制代码
Unity OnTrigger和List使用
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. public class Ontriggle : MonoBehaviour {

  5. List<Color> colorlist;
  6. // Use this for initialization
  7. void Start () {

  8. }

  9. // Update is called once per frame
  10. void Update () {

  11. }
  12. void OnTriggerEnter(Collider other)
  13. {
  14.   colorlist=new List<Color>();
  15.   for(int i=0;i<other.renderer.materials.Length;i++)
  16.   {
  17.    colorlist.Add(other.renderer.materials[i].color);
  18.    other.renderer.materials[i].color=Color.red;
  19.   }
  20. }
  21. void OnTriggerStay(Collider other)
  22. {
  23.   Debug.Log ("-------stay----");
  24. }
  25. void OnTriggerExit(Collider other)
  26. {
  27.   for(int i=0;i<other.renderer.materials.Length;i++)
  28.   {
  29.    other.renderer.materials[i].color=colorlist[i];
  30.   }
  31. }
  32. }
复制代码
Unity 控制物体移动和旋转
  1. <span style="font-size:18px;">using UnityEngine;
  2. using System.Collections;
  3. public class transform : MonoBehaviour {
  4. public float speed;
  5. public float rotspeed;
  6. // Use this for initialization
  7. void Start () {

  8. }

  9. // Update is called once per frame
  10. void Update () {
  11.   if(Input.GetKey(KeyCode.W)){
  12. // transform.Translate(Vector3.*Time.deltaTime*speed);
  13.    transform.Translate(Vector3.right*Time.deltaTime*speed);
  14.   }
  15.   else if(Input.GetKey(KeyCode.S)){
  16.    transform.Translate(Vector3.left*Time.deltaTime*speed);
  17.   }
  18.   if(Input.GetKey(KeyCode.A)){
  19.    transform.Rotate(Vector3.up*Time.deltaTime*(-rotspeed));
  20.   }
  21.   else if(Input.GetKey(KeyCode.D)){
  22.    transform.Rotate(Vector3.up*Time.deltaTime*(rotspeed));
  23.   }
  24. }
  25. }
  26. </span>
复制代码

作者: hyui    时间: 2014-6-24 17:53
THanks for sharing your experience!
作者: HIDEOKOJIMA    时间: 2014-6-25 00:14
THanks for sharing
作者: wucnj    时间: 2014-6-25 09:13
感谢分享!!!
作者: shenyg77    时间: 2014-8-12 21:03
Thanks for sharing !




欢迎光临 纳金网 (http://old.narkii.com/club/) Powered by Discuz! X2.5