more efficient ray cast position code
This commit is contained in:
@@ -3,10 +3,9 @@ using System;
|
|||||||
|
|
||||||
public partial class player : CharacterBody2D
|
public partial class player : CharacterBody2D
|
||||||
{
|
{
|
||||||
[Export]
|
[Export] public string playerName;
|
||||||
public string playerName;
|
[Export] public int speed = 400;
|
||||||
[Export]
|
[Export] public int rayCastLength = 64;
|
||||||
public int speed = 400;
|
|
||||||
public Vector2 movement;
|
public Vector2 movement;
|
||||||
public AnimatedSprite2D animatedSprite;
|
public AnimatedSprite2D animatedSprite;
|
||||||
|
|
||||||
@@ -27,11 +26,8 @@ public partial class player : CharacterBody2D
|
|||||||
public override void _Process(double delta)
|
public override void _Process(double delta)
|
||||||
{
|
{
|
||||||
//set ray_cast target position
|
//set ray_cast target position
|
||||||
int raylength = 64;
|
Vector2 rayCastPosition = new Vector2((float)Math.Round(movement.x), (float)Math.Round(movement.y)) * rayCastLength;
|
||||||
if (Input.IsActionJustPressed("move_right")) GetNode<RayCast2D>("ray_cast_2d").TargetPosition = new Vector2(raylength, 0);
|
if (rayCastPosition.Length() != 0) GetNode<RayCast2D>("ray_cast_2d").TargetPosition = rayCastPosition;
|
||||||
if (Input.IsActionJustPressed("move_left")) GetNode<RayCast2D>("ray_cast_2d").TargetPosition = new Vector2(-raylength, 0);
|
|
||||||
if (Input.IsActionJustPressed("move_down")) GetNode<RayCast2D>("ray_cast_2d").TargetPosition = new Vector2(0, raylength);
|
|
||||||
if (Input.IsActionJustPressed("move_up")) GetNode<RayCast2D>("ray_cast_2d").TargetPosition = new Vector2(0, -raylength);
|
|
||||||
//call event in raycasted object
|
//call event in raycasted object
|
||||||
if (Input.IsActionJustPressed("ui_accept") && GetNode<RayCast2D>("ray_cast_2d").IsColliding())
|
if (Input.IsActionJustPressed("ui_accept") && GetNode<RayCast2D>("ray_cast_2d").IsColliding())
|
||||||
GetNode<RayCast2D>("ray_cast_2d").GetCollider().Call("OnInteraction", playerName);
|
GetNode<RayCast2D>("ray_cast_2d").GetCollider().Call("OnInteraction", playerName);
|
||||||
|
Reference in New Issue
Block a user