1

我有gameobject charTransform = GameObject.FindGameObjectWithTag ("Character");

而且我正在使用代码获得世界边界 rightHorizontalBound = camera.ViewportToWorldPoint (new Vector3 (1,0, camera.nearClipPlane)).x;

问题:如何获得与游戏对象相同的类型rightHorizontalBound?所以我可以比较他们的价值观?

所有不好的事情都发生在if(charTransform1.position.x >= rightHorizontalBound),因为charTransform1.position.x不是一个 WorldPoint 作为一个rightHorizontalBound

需要我主持的功能,所以字符不能通过右边框,这里是代码

using UnityEngine;
using System.Collections;

public class Gameplay : MonoBehaviour 
{

 float leftHorizontalBound;
 float rightHorizontalBound;

 public Transform charTransform1;

 void Start () 
 {
  charTransform1 = GameObject.FindGameObjectWithTag ("Character").transform;
  leftHorizontalBound = camera.ViewportToWorldPoint (new Vector3 (0,0, camera.nearClipPlane)).x;
  rightHorizontalBound = camera.ViewportToWorldPoint (new Vector3 (1,0, camera.nearClipPlane)).x;
  //
 }


 void Update ()
 {
  MovementRight ();
  MovementLeft ();


  if (charTransform1.position.x <= leftHorizontalBound) {
     charTransform1.position = new Vector3 (leftHorizontalBound + 1f, charTransform1.position.y, charTransform1.position.z);
     return;
  } 

  if(charTransform1.position.x >= rightHorizontalBound)
  {
   charTransform1.position = new Vector3(rightHorizontalBound - 1f,charTransform1.position.y,charTransform1.position.z);
   return;
  }

 }

 void MovementRight()
 {
  charTransform1.transform.Translate (Vector2.right  300f  Time.deltaTime);
 }
 void MovementLeft()
 {
  charTransform1.transform.Translate (Vector3.left  00f  Time.deltaTime);
 }
}
4

0 回答 0