+1 vote

I asked a similar question earlier but the code did not work.

This is the script for the player so far:

extends KinematicBody2D

export (int) var speed = 200

var velocity = Vector2()

func getinput():
velocity = Vector2()
if Input.is
actionpressed('uiright'):
velocity.x += 1
if Input.isactionpressed('uileft'):
velocity.x -= 1
if Input.is
actionpressed('uidown'):
velocity.y += 1
if Input.isactionpressed('ui_up'):
velocity.y -= 1
velocity = velocity.normalized() * speed

func physicsprocess(delta):
getinput()
velocity = move
and_slide(velocity)

in Engine by (22 points)
edited by

1 Answer

0 votes

THIS ONLY PRINTS WHAT OBJECT YOU COLLIDED WITH BUT ITS CLOSER TO THE RESULT THAN THE ABOVE CODE

extends KinematicBody2D

export (int) var speed = 200

var velocity = Vector2()

func getinput():
velocity = Vector2()
if Input.is
actionpressed('uiright'):
velocity.x += 1
if Input.isactionpressed('uileft'):
velocity.x -= 1
if Input.is
actionpressed('uidown'):
velocity.y += 1
if Input.isactionpressed('ui_up'):
velocity.y -= 1
velocity = velocity.normalized() * speed

func physicsprocess(delta):

get_input()
velocity = move_and_slide(velocity)

var collision = move_and_collide(velocity * delta)
if collision:
    print("I collided with ", collision.collider.name)
by (22 points)

note: im working on finding out how to make the life and damage next, Any more help would be nice.

That's about it

if collision:
    if collision.collider == monster(or whatever name you given to the monster scene):
        health -= 10(assuming you already have a variable for health)

if health <= 0:
    die()

and you don't have to use moveandslide and moveandcollide at the same time.so, delete that moveandslide.

thanks, looks good.

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.