A better way to detect the cursor?

Nehiso

Goal: Detect when the cursor has entered a defined radius of the player.

Hello, I am in the process of trying to replicate the combat system from a game called CrossCode. The idea is that when the cursor is within a certain radius of the player, I will be able to switch to melee combat, and back to ranged once the cursor leaves this radius.

I have implemented one way I thought it could be done, however it feels slow or unreliable and I just wanted to know if there are any other methods I could possibly look into to achieve a smoother result.

Here is what I've done

attached to the player

void Update()
{
    attackStyleSwitchRadius = colRef.radius;
    playerCenter = colRef.transform.position;

    if(Physics2D.OverlapCircle(playerCenter, attackStyleSwitchRadius, cursor))
    {
        meleeMode = true;
        rangeMode = false;
    }
    else
    {
        meleeMode = false;
        rangeMode = true;
    }

}

And on a small 2D object I have this script so that it follows the cursor position.

void Update()
{
    pos = Input.mousePosition;
    gameObject.transform.position = Camera.main.ScreenToWorldPoint(pos);
}

when the small object enters the overlap circle it changes the bools around.

Lece

You can remove the collision detection overhead by doing something like this instead;

void Update ()
{
    attackStyleSwitchRadius = colRef.radius;
    playerCenter = colRef.transform.position;

    var mouse = Input.mousePosition;
    mouse.z = Vector3.Distance(Camera.main.transform.position, playerCenter);

    var range = Vector2.Distance(Camera.main.ScreenToWorldPoint(mouse), playerCenter);    
    var inside = range < attackStyleSwitchRadius;

    meleeMode = inside;
    rangeMode = !inside;
}

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Is there a better way of comparing dates

分類Dev

Better way to return boolean

分類Dev

A better way to introspect a capture

分類Dev

Is there a better way to do this? BASH

分類Dev

better way to do this in MATLAB?

分類Dev

Is there a better way to loop code?

分類Dev

Is there a better way to define a global variable?

分類Dev

Decorator with Arguments: Would this be a better way?

分類Dev

Is this possible using LEAD or is there a better way?

分類Dev

Is there a way of making discord embeds better?

分類Dev

Is there a better way to do Insertion Sort?

分類Dev

Is there a better way to do this than echo?

分類Dev

Better way to override a function definition

分類Dev

Is there a better way to divide this string into substrings?

分類Dev

Detect if mouse move in circle way

分類Dev

Is there a way to detect cross over of hands?

分類Dev

Is there a better way to re-use plots Matplotlib?

分類Dev

Better way of capturing multiple same tags?

分類Dev

Better way to execute nested for loops to generate a dict?

分類Dev

Better way of error handling in Kafka Consumer

分類Dev

rxJava better way to wait on a specific sequence of values

分類Dev

Which way of setting fields value is better and why?

分類Dev

Which way of setting fields value is better and why?

分類Dev

AngularJS: Is there a better way to achieve this than the one specified?

分類Dev

Better way to write jquery add/remove class

分類Dev

better way to load 2 dropdown in mvc

分類Dev

Better way to check a list for specific elements - python

分類Dev

Better way to pass bool variable as parameter?

分類Dev

What is a better way to write this SQL query?

Related 関連記事

ホットタグ

アーカイブ