首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >有没有一种方法可以让十字准线固定在玩家的Y轴上(2d上图),并在Y轴上移动到鼠标光标的距离?

问有没有一种方法可以让十字准线固定在玩家的Y轴上(2d上图),并在Y轴上移动到鼠标光标的距离?
EN

Stack Overflow用户
提问于 2019-09-27 18:30:16
回答 1查看 330关注 0票数 0

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

代码语言:javascript
复制
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轴上,但他不会像鼠标光标那样从船上移动,所以两个十字准线不会完全对齐。

EN

回答 1

Stack Overflow用户

发布于 2019-09-28 00:24:30

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

‘C#

代码语言:javascript
复制
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
                }                   
                }
            }
        }

“”“

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58132752

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档