GODOT - How can I stop the player from hitting the default walls in GODOT and instead keep going - game-engine

I am making a platformer game and I dont know how to make the player to dont collide with those default walls! Ive already setted the camera follow but now my player is getting stuck on those default walls!
HereĀ“s the player movement code:
extends KinematicBody2D
const GRAVITY = 600
const WALK_SPEED = 200
const JUMP_FORCE = 350
var velocity = Vector2()
var screen_size
func _ready():
screen_size = get_viewport_rect().size
func _physics_process(delta):
velocity.y += delta * GRAVITY
if Input.is_action_pressed("ui_left"):
velocity.x = -WALK_SPEED
elif Input.is_action_pressed("ui_right"):
velocity.x = WALK_SPEED
else:
# velocity.x = 0
# smoothen the stop
velocity.x = lerp(velocity.x, 0, 0.1)
if Input.is_action_pressed("ui_up") and is_on_floor():
velocity.y = -JUMP_FORCE
velocity = move_and_slide(velocity, Vector2.UP)
# prevent player going out of screen
position.x = clamp(position.x, 0, screen_size.x)
position.y = clamp(position.y, 0, screen_size.y)
Please help!
The player just gets blocked as shown in the image!
Like I cant advance more from that position idk why. Sorry my english is bad.
Please help, thanks in advance!
My Player gets stuck when he reaches that blue line when the game is running. I just want to desactivate that. Do you know any way?

I think its because the player can only move to the size of the screen, because when I put the game in FullScreen, I can move much further.
Right there in the code is what you describe:
# prevent player going out of screen
position.x = clamp(position.x, 0, screen_size.x)
position.y = clamp(position.y, 0, screen_size.y)
If you don't want to limit the player to the screen, remove that code.
On a similar note, make sure you have a camera following the player. The simplest way to do it is to put a Camera2D as child of the player and set its current property to true.

Related

Hold to jump higher function not properly working

I'm currently making a game and I made this simple script that checks when a player holds down the up arrow key so that the longer you hold the button, the further upwards you jump. It works fine and all, until you decide to jump again mid-air, then the gravity of the is_action_just_pressed is added into the player's total gravity which is kind of a problem.
I'm kind of new to GDScript and programming in general, so a little help would really be appreciated!
Here's my (bad) code:
var gravity : int = 1800
var vel : Vector2 = Vector2()
func _physics_process(delta):
vel.x = 0
vel = move_and_slide(vel, Vector2.UP)
vel.y += gravity * delta
if Input.is_action_just_pressed("jump") and is_on_floor():
vel.y -= jumpforce
gravity -= 500
if Input.is_action_just_released("jump"):
gravity += 500
There isn't anything in godot for checking if the player is holding down a button and I'm not sure what is wrong with my code, so I have no idea what is causing this issue or how to fix it.
There isn't anything in godot for checking if the player is holding down a button
Instead of using is_action_just_pressed and is_action_just_released which tell you if the action was pressed or released since the last frame, use is_action_pressed which tell you if the action is currently pressed.
It works fine and all, until you decide to jump again mid-air, then the gravity of the is_action_just_pressed is added into the player's total gravity which is kind of a problem.
The issue is that you are not keeping track if player jumped. For example:
if Input.is_action_just_pressed("jump") and is_on_floor():
jumped = true
vel.y -= jumpforce
gravity -= 500
if Input.is_action_just_released("jump") and jumped:
jumped = false
gravity += 500
With jumped declared at the top of the script, similar to gravity and vel.

Tile Collision In GML

I am making a game based on the game AZ on the website Y8, and I am having problems with tile collisions.
the player moves basically by giving it speed when up is pressed, then rotating left or right.
direction = image_angle;
if(keyForward)
{
speed = 2;
}
else speed = 0;
// rotate
if(keyRotateLeft)
{
image_angle = image_angle + 5;
}
if(keyRotateRight)
{
image_angle = image_angle - 5;
}
then I said when the player collides with the tile speed = 0. But the player gets stuck and can't move anymore. is there a better way to do this.
A simple approach would be as following:
Attempt to rotate
Check if you are now stuck in a wall
If you are, undo the rotation.
A more advanced approach would be to attempt pushing the player out of solids while rotating.
Alternatively, you may be able to get away with giving the player a circular mask and not rotating the actual mask (using a user-defined variable instead of image_angle).

Bound camera to sprite bounds

I have a scene camera following a unit (sprite's position), this unit is standing on a terrain sprite, This camera not only follows it, it also zooms in and out using the pinch gesture. Everything works well, except that I want to limit the camera's lower bound to be limited by the lower bound of the terrain sprite. In other words, I don't want the camera to display anything below the terrain sprite. And by this I mean that instead the camera could shift upwards and preserve the scale.
Any swift, obj-c or even pseudo-code answers would help. Thanks!
This is my attempt
let halfTerrain = terrain.size.height/2
let halfViewport = scene!.size.height/2
let distanceFromCenter = halfTerrain - cameraPosition.y
let scaledDistanceFromCenter = distanceFromCenter/cameraScale
let gapSize = halfViewport - halfTerrain/cameraScale
let isOverlapping = scaledDistanceFromCenter < halfViewport
if isOverlapping {
camera?.runAction(SKAction.moveTo(CGPoint(x: cameraPosition.x,
y: -gapSize * cameraScale), duration: movingSpeed))
} else {
camera?.runAction(SKAction.moveTo(cameraPosition,
duration: movingSpeed))
}
camera?.runAction(SKAction.scaleTo(cameraScale,
duration: scaleSpeed))

Using spritekit, how can you achieve a scrolling, randomly sloped floor in a side scrolling game?

I'm thinking of something akin to Ski safari, as seen the link below.
Creating a side scrolling effect with a static sprite is simple enough with SKActions, but if I wanted to use that same sprite (which is a rectangle) in random hills, would the sprite need to be transformed every frame to create new hills? If so, how can you transform sprites from within SpriteKit? I feel like when running on a mobile platform this would cause a large frame rate drop
If not, does that mean that you would have to create slopes yourself and program them in randomly?
For completeness sake, here is how I am generating a simple scrolling floor:
func makeBackground(){
var backgroundTexture = SKTexture(imageNamed: "sprite-floor")
var shiftBackground = SKAction.moveByX(-backgroundTexture.size().width, y: 0, duration: 1.4)
var replaceBackground = SKAction.moveByX(backgroundTexture.size().width, y: 0, duration: 0)
var movingAndReplacingBackground = SKAction.repeatActionForever(SKAction.sequence([shiftBackground, replaceBackground]))
for(var i: CGFloat = 0; i < 5; i++){
let background=SKSpriteNode(texture: backgroundTexture)
background.anchorPoint = CGPointZero
background.size.height = self.frame.height / 10
background.position = CGPoint(x: backgroundTexture.size().width/2 + (backgroundTexture.size().width * i), y: background.size.height)
background.runAction(movingAndReplacingBackground)
self.addChild(background)
}
}
(for some reason the SO image uploader isn't working, so here is the link to my example:http://www.unwinnable.com/wp-content/uploads/2012/04/Ski.jpg )
The following tutorial might be able to help.
http://www.raywenderlich.com/32954/how-to-create-a-game-like-tiny-wings-with-cocos2d-2-x-part-1
It deals with replicating the level creation from Tiny Wings which had procedurally generated hills to land on. Should be a good starting place for you.

THREEJS: Rotating the camera while lookingAt

I have a moving camera in camera container which flies arond the scene on giving paths like an airplane; so it can move to any position x,y,z positive and negative. The camera container is looking at his own future path using a spline curve.
Now I want to rotate the camera using the mouse direction but still keeping the general looking at position while moving forward with object. You could say, i want to turn my head on my body: while moving the body having the general looking at direction, i am turning my head around to lets say 220 degree up and down. So i can't look behind my body.
In my code the cameraContainer is responsible to move on a pline curve and to lookAt the moving direction. The camera is added as a child to the cameraContainer responsible for the rotation using the mouse.
What i don't get working properly is the rotation of the camera. I guess its a very common problem. Lets say the camera when moving only on x-axes moves not straight, it moves like a curve. Specially in different camera positions, the rotation seems very different. I was tryiing to use the cameraContainer to avoid this problem, but the problem seems nothing related to the world coordinates.
Here is what i have:
// camera is in a container
cameraContainer = new THREEJS.Object3D();
cameraContainer.add(camera);
camera.lookAt(0,0,1);
cameraContainer.lookAt(nextPositionOnSplineCurve);
// Here goes the rotation depending on mouse
// Vertical
var mouseVerti = 1; // 1 = top, -1 = bottom
if(window.App4D.mouse.y <= instance.domCenterPos.y) // mouse is bottom?
mouseVerti = -1;
// how far is the mouse away from center: 1 most, 0 near
var yMousePerc = Math.abs(Math.ceil((instance.domCenterPos.y - window.App4D.mouse.y) / (instance.domCenterPos.y - instance.domBoundingBox.bottom) * 100) / 100);
var yAngleDiffSide = (instance.config.scene.camera.maxAngle - instance.config.scene.camera.angle) / 2;
var yRotateRan = mouseVerti * yAngleDiffSide * yMousePerc * Math.PI / 180;
instance.camera.rotation.x += yRotateRan; // rotation x = vertical
// Horizontal
var mouseHori = 1; // 1 = top, -1 = bottom
if(window.App4D.mouse.x <= instance.domCenterPos.x) // mouse is left?
mouseHori = -1;
// how far is the mouse away from center: 1 most, 0 near
var xMousePerc = Math.abs(Math.ceil((instance.domCenterPos.x - window.App4D.mouse.x) / (instance.domCenterPos.x - instance.domBoundingBox.right) * 100) / 100);
var xAngleDiffSide = (instance.config.scene.camera.maxAngle - instance.config.scene.camera.angle) / 2;
var xRotateRan = mouseHori * xAngleDiffSide * xMousePerc * Math.PI / 180;
instance.camera.rotation.y += xRotateRan; // rotation y = horizontal
Would be really thankful if someone can give me a hint!
I got the answer after some more trial and error. The solution is to simply take the initial rotation of y in consideration.
When setting up the camera container and the real camera as child of the container, i had to point the camera to the frontface of the camera container object, in order to let the camera looking in the right direction. That lead to the initial rotation of 0, 3.14, 0 (x,y,z). The solution was to added 3.14 to the y rotation everytime i assigned (as mentioned by WestLangley) the mouse rotation.
cameraReal.lookAt(new THREE.Vector3(0,0,1));
cameraReal.rotation.y = xRotateRan + 3.14;