- Edited
title says all, I dont know how to add video to show problem
It say 'Uploading files of this type is not allowed' ( type is mp4 )
so I just going to leave my code below
Edit)
forum 'insert code' doesn't work well in C# too
`
using Godot;
using System;
public partial class Pointy : CharacterBody2D
{
[Export]
float moveSpeed;
[Export]
float gravity;
int direction;
int direction
{
get
{
return direction;
}
set
{
direction_ = value;
Vector2 halfSize = rectSize * 0.5f;
Vector2 min = collision.Position - (halfSize);
Vector2 max = collision.Position + (halfSize);
Vector2 left = new Vector2(min.X, min.Y);
Vector2 right = new Vector2(max.X, min.Y);
raycast.Position = (direction_ == 1) ? left : right;
sprite.FlipH = (direction_ == 1) ? true : false;
}
}
AnimatedSprite2D sprite;
RayCast2D raycast;
CollisionShape2D collision;
Vector2 rectSize;
Vector2 velocity;
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
rectSize = Vector2.Zero;
velocity = Vector2.Zero;
var rayNode = GetChild<RayCast2D>();
if(rayNode != null)
{
raycast = GetNode<RayCast2D>(rayNode.GetPath());
}
var colNode = GetChild<CollisionShape2D>();
if(colNode != null)
{
collision = GetNode<CollisionShape2D>(colNode.GetPath());
}
var spriteNode = GetChild<AnimatedSprite2D>();
if(spriteNode != null)
{
sprite = GetNode<AnimatedSprite2D>(spriteNode.GetPath());
}
RectangleShape2D rectangle = collision.Shape as RectangleShape2D;
rectSize = rectangle.Size;
direction = 1;
}
public override void _PhysicsProcess(double delta)
{
velocity.Y += gravity;
if(IsOnWall())
{
direction *= -1;
}
/*
* fix if remove this comment
if(IsOnFloor())
{
velocity.Y = 0;
}
*/
velocity.X = direction * moveSpeed;
Velocity = velocity;
MoveAndSlide();
}
Node GetChild<T>()
{
var childrenNodes = GetChildren();
Node result = null;
for (int i = 0; i < childrenNodes.Count; i++)
{
var child = childrenNodes[i];
if (child is T)
{
result = child.GetNode(child.GetPath());
break;
}
}
return result;
}
`