首页 > 解决方案 > ScreenPointtoRay 为鼠标跟随脚本创建问题

问题描述

我很困惑为什么我的游戏光标跟随脚本似乎没有根据角色的位置而有所不同。如果玩家将鼠标悬停在角色的手上并单击并拖动,我希望玩家按下“E”,手臂将跟随鼠标位置上下移动。

该脚本一直在工作,直到我沿 X 轴移动我的角色,发现即使角色、他的手臂和相机移开,鼠标必须单击和拖动的位置仍保持在手臂的原始位置。有人知道为什么吗?我已经粘贴了用于查找鼠标位置的脚本和用于让我的游戏对象复制鼠标位置的脚本。

我还上传了一些在 YouTube 上工作的脚本视频 https://www.youtube.com/watch?v=grhnqckOfc0 https://www.youtube.com/watch?v=ZlLLgnU8yMc

谢谢你的时间!

public static class GameCursor

{

 public static LayerMask mask;


 public static Vector2 WorldPosition
 {
     get
     {
         float distance;
         int mask = 1<<9; //setting layer mask to layer 9
         mask = ~mask; //setting the layer mask to ignore layer(~)
             //adding in the layermask to raycast will ignore 9th layer



         var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         Debug.DrawRay(ray.origin, ray.direction * 10000f, Color.black);
         if (_gamePlane.Raycast(ray, out distance))
             return ray.GetPoint(distance);

返回 Vector2.zero;

     }
 }
 private static Plane _gamePlane = new Plane(Vector3.forward,0);

}

 public GameObject ArmR;


 public static Vector2 OffsetPoint;

 public Vector2 OffsetVec2;

 void Start ()

 {

 }


 private void Update()
 {

     if (Input.GetKey (KeyCode.E)) {


         Follow ();
     }

     if (Input.GetKeyUp (KeyCode.E)) {


     }

     //print (transform.eulerAngles.z);
     //Offset.transform.position = OffsetVec2;
     OffsetPoint = GameCursor.WorldPosition + OffsetVec2;

     Vector3 clampedRotation = transform.eulerAngles;
     // Clamp the z value
     if (clampedRotation.z > 90.0f) clampedRotation.z = 180.0f - clampedRotation.z;{
         clampedRotation.z = Mathf.Clamp (clampedRotation.z, 0, 90);
         clampedRotation.z = Mathf.Clamp (clampedRotation.z, -90, 180);

     }

     // assign the clamped rotation
     ArmR.transform.rotation = Quaternion.Euler(clampedRotation);


     //if(ArmR.transform.eulerAngles.z >= 350 || transform.eulerAngles.z <= 270){

         //Debug.Log ("no");



     //}

     if(ArmR.transform.eulerAngles.z == 0){

         //Debug.Log ("AT0");

     }

     if(ArmR.transform.eulerAngles.z == 90){

         //Debug.Log ("AT90");




     }


     }

无效跟随(){

     ArmR.transform.up = -GameCursor.WorldPosition + OffsetVec2;
 }

 private void OnDrawGizmos()
 {
     Gizmos.DrawIcon(GameCursor.WorldPosition, "wallll", true);

 }

}

标签: c#unity3d

解决方案


推荐阅读