我正在使用c#编程和我的其他朋友使用Unity设置一个宇宙飞船游戏。我们的游戏是2d的,玩家的视野是从上到上。我们需要做两个十字光标:一个是鼠标光标的替换(完成),宇宙飞船的y轴跟随鼠标光标旋转,这样飞船就会跟随鼠标光标的十字光标转动一段时间。在这个延迟时间内,宇宙飞船不会准确地瞄准鼠标十字准线,因为旋转不是立即的,所以我们想要做第二个十字准线,沿着宇宙飞船的Y轴,让玩家了解何时飞船的武器将准确地瞄准光标位置。第二个十字准线必须沿船舶的y轴滚动,以便船舶与鼠标光标之间的距离与辅助取景器在Y轴上的距离相同。在这里你可以找到一个游戏的例子:https://imgur.com/Uk7pW4x
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轴上,但他不会像鼠标光标那样从船上移动,所以两个十字准线不会完全对齐。
发布于 2019-09-28 00:24:30
将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
}
}
}
}“”“
https://stackoverflow.com/questions/58132752
复制相似问题