0 votes

I basically want a way for my character too have his head like this.


and for the charatcer too be behind the leaves as currently its like this


and if the character is behind the tree for the tree to lower opacity so the player can see the character, thats if this is all possible



in Engine by (12 points)

1 Answer

+1 vote

When the player's X coordinate is in the range of the tree (Xtree, Xtree + tree.length.x) then the tree should have a lower opacity.

by (173 points)

How would I go about implementing this into my code as I’m new too gdscript, also how would I fix the issue if player not being able to go behind the tree but still allow him too he in front of it.

Do you want to add the possibility for the player to move behind and also in front of the tree ?

Exactly that, I would love for the player to be able too walk infront and behind the tree with the tree lowering in opacity when the player goes behind it. I’ve implemented a sinilar feature in game maker studios but that was many moons ago and I can’t exactly remember how and not exactly sure how to convert game maker script into gd script. If you know how too make all of this work you would be a life saver. And if you do code certain snippets too make it work would you please add comments explaining what you did so I can learn for the future.



Mmh 3D features in a 2D game... Do you program a lot ? And did you create your script in gamemaker yourself ? Cause this is going to be hard for you to understand otherwise...

I dont code alot but I feel like I would understand it after giving it a read, I didnt create the script myself i looked about and just mixed and match scripts for the thing i wanted and it just worked so i didnt really need to think of it in depth. Im sorry if im not being much use.

First you can reduce the player's size whenever he presses the "up arrow key" for exemple.
When he presses this "up arrow key" you can increment a var called "player_z" for exemple. And if this "z" variable is below a certain value then the player's sprite is moved behind the tree. And when he's behind it the tree's opacity decreases.

Im really sorry for how dumb I'm being it must be really easy for you too understand but since im working on vector2 am I able to change the z cord of the player and how would i go about writing it in GDscript. If you want i can show you my player script and the tree only has a staticBody2D and its child is Collision shape2D of the rectangle shape.

My GDscript for the player is this:

extends KinematicBody2D

Get player sprite

var sprite_node

Player speed

export (int) var speed = 200
var velocity = Vector2()

Camera follow player

var look_direction = Vector2(1, 0)

func ready():
node = get_node("playerSprite")

Movement with keyboard

func get_input():

velocity = Vector2()

if Input.is_action_pressed('right'):
    velocity.x += 1
if Input.is_action_pressed('left'):
    velocity.x -= 1
if Input.is_action_pressed('up'):
    velocity.y -= 1
if Input.is_action_pressed('down'):
    velocity.y += 1

velocity = velocity.normalized() * speed

if Input.is_action_pressed('right') or Input.is_action_pressed('left') or Input.is_action_pressed('up') or Input.is_action_pressed('down'):

func physicsprocess(delta):

# Calls get_input function
# Moves the character

Its probably really inefficient the method i have done it but as i staated before im new too this engine and honestly I'm really bad at coding my mind set is not that great but I'm trying to push myself too actually learn to code because making games is pretty fun, especially sandbox adventure games just seeing it grow is really the only reason im making it.



Also you wouldnt know any people or places that are good to teach me how to learn GDscript do you? So far I've been using the learn section on the Godot website.

Don't worry about learning Godot. The languages syntaxes are always easy to understand. But you first of all have to understand algorithm basics.

Do you really want to implement "3D" features in a 2D game ? I think it would be easier for u to ignore those details (tree opacity for exemple) or you could simply use 3D features. Cause I could give u some code snippets to make your game working fine for the moment but it would be even harder in the future when you would like to add more objects, properties to your game cause what you're asking for is kinda long and hard to implement.

Ok I see what you mean, so let’s say scrap the opacity change how would I possible be able to go about with the player in front and going behind the tree because I really need too implement that into my game as otherwise it looks really bad.



  • Easiest way :
    Only two "z" positions are available : FRONT or BEHIND
    That means the player is "teleported" from an axis to an other when he presses "up" and "down" keys
  • Really much more difficult way :
    The player can also move between those two axes. And you would definitely need to use 3D features.

Which one do you choose ?

Let’s say the easiest one, once I get more attuned with godot I’ll look into the 3D aspects of the engine. Honestly thank you so much for the help and I’m sorry if I’m being a bit inept.



Your welcome ^^

Ok first let's create a var player_in_front = true at the top of your script.

  • When the player presses the "up_key", player_in_front = false and player.set_scale(0.5)
  • When the player presses the "down_key", player_in_front = true and player.set_scale(1)

This should change the size of your player depending of the "z" axis he's on.

Thank you but I’m currently out and will be for a few hours so I will implement it when I get home.



ok no problem :)

I'm back home now and I have implemented the following lines of code that you have given me and it produces an error due to the fact that there is no identifier of which is named Player.

Also what else would I have to add to the script?



Ok so my only issue at the moment is the fact that it does not have a var for player because that var is basically empty, if it changes the scale of the sprite then i have a var for that already if its changing the something in the kinematic2D node then i dont know how to call for that since the script is already on the node and lastly im not sure if it would be on collision2D node.



Ok so I have thought of the most inefficent thing ever but i think it might work so basically have two collisionShape2D one named treeBase and one named treeUpper, when player collides with treeUpper it will place the player behind the tree and maybe in the future I can mess around with the opacity some how. It sounds like a crazy plan but i feel like it might work.

mmh what a strange idea. But not that bad actually ;)

"player" was just a placeholder. Replace it by your object (player) name

Hmm now the only issue is how the heck do i go around creating what i just explained, see this is my main issue i can think of stuff like this but dont know how to actually make it practically.



Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.