有没有办法让十字准线固定在玩家的 Y 轴(2d 上视图)上,并在与鼠标光标一样远的 Y 轴上移动?



我正在使用 c# 编程与我的其他朋友一起使用 Unity 设置一个宇宙飞船游戏。我们的游戏是2D的,玩家从上到大。我们需要做 2 个十字准线:一个是替换鼠标光标(完成(,飞船 y 轴跟随鼠标光标,因此宇宙飞船跟随鼠标光标十字准线旋转并延迟。在这个延迟时间内,飞船不会精确瞄准鼠标十字准线,因为旋转不是立即的,所以我们想做第二个十字准线,沿着飞船的Y轴,让玩家了解飞船的武器何时会准确地瞄准光标位置。第二个十字准线必须沿飞船的 y 轴滚动,以使飞船与鼠标光标之间的距离在 Y 轴上的辅助取景器之间的距离相同。 在这里你可以找到游戏的例子:https://i.stack.imgur.com/ATTcZ.jpg

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MouseCursor : MonoBehaviour{
public Texture2D cursorTexture;
public Transform aim;
public float aimSpeed = 3.0f;
private Vector2 cursorHotspot;
private Vector2 aim2;
private Vector3 mouseP;
private Vector2 direction;
// initialize mouse with a new texture with the
// hotspot set to the middle of the texture
// (don't forget to set the texture in the inspector
// in the editor)
void Start()
{
cursorHotspot = new Vector2(cursorTexture.width / 2, cursorTexture.height / 2);
Cursor.SetCursor(cursorTexture, cursorHotspot, CursorMode.Auto);
}
// To check where your mouse is really pointing
// track the mouse position in you update function
void Update()
{
//from this the second crosshair code
Vector3 currentMouse = Input.mousePosition;
Ray ray = Camera.main.ScreenPointToRay(currentMouse);
RaycastHit2D hit = Physics2D.Raycast(ray.origin, ray.direction);
Debug.DrawLine(ray.origin, hit.point);
var dir = Input.mousePosition - Camera.main.WorldToScreenPoint(transform.position);
aim2 = new Vector2(dir.x , dir.y);
aim.transform.position = Vector2.MoveTowards(transform.position, aim2, aimSpeed);

}
}

实际上,第二个十字准线保留在Y轴上,但他不会像鼠标光标那样远离飞船,因此两个十字准线都不会完全对齐一个。

将AIM放入播放器中,然后AIM进行玩家轮换,这是代码AIM 的基础

'''C#

public Transform aim;//AIM position "aim image"
public float aimSpeed;//speed AIM
public float zonaMorta;//death zone value where the AIM can't move
private Vector3 target;//position where the AIM need to go
private Vector3 posAim;//AIM position
void Update ()
{
float pr = transform.rotation.z;//take the rotation value from the player
float ty = Camera.main.ScreenToWorldPoint(Input.mousePosition).y;//y axis value of the mouse.
target = new Vector3(0 , ty, 0);//allowance the y axis of the cursor to "target"
posAim = aim.transform.position;//allowance AIM position to "posAim"
posAim = new Vector3(0, posAim.y, 0);//on posAim I keep only y while x, z gate them
if(posAim.y + zonaMorta > target.y && posAim.y - zonaMorta < target.y)//if AIM is between this two value then it can't move, it can't move in the y axis
{
}else{
if(pr > -90f || pr < 90f){//if the rotation of the player is more than -90 degrees or less than 90 degrees then ..
if(posAim.y > target.y)//if y axis of AIM is greater than the y axis of the cursor then..
{
aim.transform.position -= aim.transform.up * aimSpeed;//subtract and allowance at the AIM position a positive velocity in the y axis
}
if(posAim.y < target.y)//if y axis of AIM is lesser than the y axis of the cursor then..
{
aim.transform.position += aim.transform.up * aimSpeed;//sum and allowance at the AIM position a positive velocity in the y axis
}                   
}
if(pr < -90f || pr > 90f){//if the rotation of the player is less than -90 degrees or more than 90 degrees then ..
if(posAim.y > target.y)//if y axis of AIM is greater than the y axis of the cursor then..
{
aim.transform.position -= -aim.transform.up * aimSpeed;//subtract and allowance at the AIM position a negative velocity in the y axis
}
if(posAim.y < target.y)//if y axis of AIM is lesser than the y axis of the cursor then..
{
aim.transform.position += -aim.transform.up * aimSpeed;//sum and allowance at the AIM position a negative velocity in the y axis
}                   
}
}
}

'''

最新更新