unity初写游戏(三)人物的瞬移

这次的脚本主要写得是鼠标左键右键的功能,子弹的发射和人物的瞬移(并不是完美)功能。


 public GameObject man;
 public bool move;
 public GUIText movetime;
 public float h;
 public float op;
 public int T = 3;
 private System.Timers.Timer time = new System.Timers.Timer (5000);//设定时间经过多少秒后触发

 void Awake() {
  man = GameObject.Find ("player");
  time.Elapsed += new System.Timers.ElapsedEventHandler (run);//设定触发的函数
  time.Start();//设定开启
 }

 void FixedUpdate () {
  movetime.text = "瞬移次数:" + T;
  if (T > 0) {
   if (Input.GetMouseButtonDown (1)) {//如果调用鼠标右键执行
    op = man.transform.position.x;
    move = true;
    T -= 1;
   }
  }
  if (move) {//具体的瞬移方式
   h = Input.GetAxis ("Horizontal");//获得现在的人物朝向
   man.rigidbody2D.AddForce (new Vector2 (h * 1000, 0), ForceMode2D.Force);//加力来让人物进行移动
   if (Mathf.Abs (op - man.transform.position.x) > 5) {//设定移动的距离。
    move = false;//关闭函数
   }
  } 
 }

 void run(object source, System.Timers.ElapsedEventArgs args) {//通过时间来回复瞬移次数
  if (T < 3) {
   T += 1;
  }
 }

这个脚本存在着比较多的问题:
1.这个人物在碰撞到物体之后不会停止力的给,因为它并没有达到距离,所以产生了这个问题。但是我用碰撞触发来解决发现并不好使,因为如果要触发的话就得设为Is Trigger这样的话人物就会穿过其他物体。
2.如果我及时改变方向会到另外一边去,距离也会多,毕竟是瞬移这样不好,但是如果我改成定点变位置的话,还有点生硬。



版权声明:本文为yangshbb原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。