Making objects Chase and "attack" players

gamedev

#1

I apologize for any confusion from the initial post. I have posted code and my intended question below.


(Hans-Gerd Claßen) #2

you’re requesting a rough version of a playable game … not more not less :slight_smile:
you may split it up to parts with concrete questions and provide hypedocuments showing your task …


(Mark Hunte) #3

Have a look at

@h_classen Han’s Pacman.

But Hans is correct if you do come back with questions please be specific.


#4

I apologize. I should have been more specific and posted some code my partner and I have been working with.

Alright @h_classen and @MarkHunte. What I want to do is create some code that will work inside my initialize function that starts when the scene loads, that makes it so when the player controlled character steps within a designated distance it will begin pursuit and, stop and attack within a distance as well, then drop pursuit after the player makes it a certain distance away.

Here is some existing code I have used in a unity project

var target : Transform; //the enemy's target

var moveSpeed = 3; //move speed
var rotationSpeed = 3; //speed of turning
var attackThreshold = 3; // distance within which to attack
var chaseThreshold = 10; // distance within which to start chasing
var giveUpThreshold = 20; // distance beyond which AI gives up
var attackRepeatTime = 1; // delay between attacks when within range

private var chasing = false;
private var attackTime = Time.time;

var myTransform : Transform; //current transform data of this enemy

function Awake()
{
myTransform = transform; //cache transform data for easy access/preformance
}

function Start()
{
target = GameObject.FindWithTag(“Player”).transform; //target the player
}

function Update () {

// check distance to target every frame:
var distance = (target.position - myTransform.position).magnitude;
if (chasing) {
//rotate to look at the player
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
//move towards the player
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
// give up, if too far away from target:
if (distance > giveUpThreshold) {
chasing = false;
}
// attack, if close enough, and if time is OK:
if (distance < attackThreshold && Time.time > attackTime) {
// Attack! (call whatever attack function you like here)
attackTime = Time.time + attackRepeatTime;
}
} else {
// not currently chasing.
// start chasing if target comes close enough
if (distance < chaseThreshold) {
chasing = true;
}
}

All I wanted to know is if something like this will work, Also if possible if there is an existing tutorial for it. The other part about randomly spawning at locations was something extra I liked but my partner figured out how to write that and we just need to translate it into hype api. We will set up a different post for that though.

Again sorry for not being clear and specific, and especially not linking code.


(Hans-Gerd Claßen) #5

hello,

hype is not a gamedeveloping tool like Unity. so there no eventsystem for collisiondetection, no prebuilt methods regarding gamedev, it’s not possible to create hypeelements programmatically on the fly … this all would be up to you.

Hype offers a set of methods in its API but that does not cover all settings from the UI.
As example: movement, rotateZ, opacity, background-image … can be set via the API.

there’s an onsceneload-behaviour so any script can be triggered on ths event.

Additional it’s possible to integrate most third party libraries which may help.