Ambiera Forum

Discussions, Help and Support.

Ambiera Forum > CopperCube > Programming and Scripting
How do I make 3D object face towards player?

dekon_17
Registered User
Quote
2021-08-30 19:09:56

So, I am trying to make a turrets in my game, but oh no - I don't really know how to make them look at the character!
I tried to make something based on Sven's "Test_fileV2", but didn't really got any good result.
So-o... Can someone help me with... this?


VP
Guest
Quote
2021-08-30 21:42:29

Add a "game character behaviour" to the turret, then add a every few seconds>turn towards "player" action.


dekon_17
Registered User
Quote
2021-08-31 14:44:53

I think there is no such action in coppercube. Rotation can be by vector or absolute, but there is no way to make it like that.


dekon_17
Registered User
Quote
2021-08-31 18:46:03

Since there isn't much replies, I'll try to make it more clear... by giving my code that I have now:
/*
<behavior jsname = "behavior_TurretAI" description = "AI for turrets because why not">
<property name = "Turret" type = "scenenode"/>
<property name = "ToAttack" type = "scenenode"/>
<property name = "Radius" type = "float"/>
<property name = "DetectionRadius" type = "float"/>
<property name = "OnAttack" type = "action"/>
<property name = "AttackCD" type = "float"/>
</behavior>
*/

behavior_TurretAI = function()
{this.LastTime = null;};

behavior_TurretAI.prototype.onAnimate = function(node, timeMs)
{
if (this.LastTime == null)
{
this.LastTime = timeMs;
return false
}

var tp = ccbGetSceneNodeProperty(this.ToAttack, "Position");
var p = ccbGetSceneNodeProperty(this.Turret, "Position");

var an = Math.atan2(p.y - tp.y, p.x - tp.x);
var deg_an = an * 180 / Math.Pi;

ccbSetSceneNodeProperty(this.Turret, "Rotation", deg_an);

return true;
}

Maybe this will let someone better understand what do I mean.


Aiming bullets
Guest
Quote
2021-08-31 22:19:19

You should always have a look at https://neophyte.cf for additional actions there is recently 2 actions for the same job has been uploaded there.


VP
Guest
Quote
2021-08-31 23:00:04

wrote:
I think there is no such action in coppercube. Rotation can be by vector or absolute, but there is no way to make it like that.



There's a "look at" action that can be downloaded. Requires "Game Character" behaviour and then simply define a "target" to point towards. Use an "every few seconds" action to make it continually point at the target - definitely works, I use it in my game to make the gun turret point at the enemies.


just_in_case
Moderator
Quote
2021-09-01 05:34:58

Yup there are 2 actions available on my website look at the target and rotate towards in neophyte.cf , @VP I don't think there is need to add the game behavior it will work just fine without the behavior.

You can download the extensions from here https://neophyte.cf/ccb_action_r...

It is good that you have created your own extension, it is always good and makes you happy when you create things by your own.

Go with any method either provided by @VP or use your extension or use the one from the site.😇


dekon_17
Registered User
Quote
2021-09-01 10:55:15

So, I took some stuff from the mentioned script, and after a few minutes of "coding" I made this working. But on the second test launch, it shows this in log:
🔎︎


I don't really understand what is the problem here. Yet again, I'll drop my code here, so, maybe someone knows what is wrong with this:
/*
<behavior jsname = "behavior_TurretAI" description = "AI for turrets because why not">
<property name = "Turret" type = "scenenode"/>
<property name = "ToAttack" type = "scenenode"/>
<property name = "Radius" type = "float"/>
<property name = "DetectionRadius" type = "float"/>
<property name = "OnAttack" type = "action"/>
<property name = "AttackCD" type = "float"/>
</behavior>
*/

behavior_TurretAI = function()
{this.LastTime = null;};

behavior_TurretAI.prototype.onAnimate = function(node, timeMs)
{
if (this.LastTime == null)
{
this.LastTime = timeMs;
return false
}
var me = this;
this.registeredFunction = function() { me.agressor32(); };

var Tur_Pos = ccbGetSceneNodeProperty(this.Turret, "Position");
var Plr_Pos = ccbGetSceneNodeProperty(this.ToAttack, "Position");

if (ccbGetCollisionPointOfWorldWithLine(Tur_Pos.x, Tur_Pos.y, Tur_Pos.z, Plr_Pos.x, Plr_Pos.y, Plr_Pos.z) == null && )

{
var position = ccbGetSceneNodeProperty(this.Turret, "Position");
var rotation = ccbGetSceneNodeProperty(this.Turret, "Rotation");
var pos_target = ccbGetSceneNodeProperty(this.ToAttack, "Position");

var radians_Horizontal = Math.atan2(pos_target.x - position.x,pos_target.z - position.z);
var degree_Horizontal = (radians_Horizontal * (180 / Math.PI));

ccbSetSceneNodeProperty(this.Turret,"Rotation",rotation.x, degree_Horizontal, rotation.z);
};

return true;
}



dekon_17
Registered User
Quote
2021-09-01 10:58:19

Okay, it looks like I found the reason. And I fixed it already, so, sorry.


dekon_17
Registered User
Quote
2021-09-01 11:34:48

Okay, it looks like I kinda finished this "AI", but it has the problem ismilar to that I mentioned before. I'll leave the script here, if someone understand this better than me (I don't understand this mess). Feel free to modify or use:
/*
<behavior jsname = "behavior_TurretAI" description = "AI for turrets because why not">
<property name = "Turret" type = "scenenode"/>
<property name = "ToAttack" type = "scenenode"/>
<property name = "Radius" type = "float"/>
<property name = "DetectionRadius" type = "float"/>
<property name = "OnAttack" type = "action"/>
<property name = "AttackCD" type = "float"/>
</behavior>
*/

behavior_TurretAI = function()
{this.LastTime = null;};

behavior_TurretAI.prototype.onAnimate = function(node, timeMs)
{
if (this.LastTime == null)
{
this.LastTime = timeMs;
return false
}
var me = this;
this.registeredFunction = function() { me.agressor32(); };

var Tur_Pos = ccbGetSceneNodeProperty(this.Turret, "Position");
var Plr_Pos = ccbGetSceneNodeProperty(this.ToAttack, "Position");
var dist=Math.sqrt( Math.pow( (Tur_Pos.x-Plr_Pos.x) ,2) + Math.pow( (Tur_Pos.y-Plr_Pos.y) ,2) + Math.pow( (Tur_Pos.z-Plr_Pos.z) ,2) );

if (ccbGetCollisionPointOfWorldWithLine(Tur_Pos.x, Tur_Pos.y, Tur_Pos.z, Plr_Pos.x, Plr_Pos.y, Plr_Pos.z) == null && dist <= this.DetectionRadius)

{
//I took the rotation script part from the "action_rotatetowardstarget" by just_in_case
var position = ccbGetSceneNodeProperty(this.Turret, "Position");
var rotation = ccbGetSceneNodeProperty(this.Turret, "Rotation");
var pos_target = ccbGetSceneNodeProperty(this.ToAttack, "Position");

var radians_Horizontal = Math.atan2(pos_target.x - position.x,pos_target.z - position.z);
var degree_Horizontal = (radians_Horizontal * (180 / Math.PI));

ccbSetSceneNodeProperty(this.Turret,"Rotation",rotation.x, degree_Horizontal, rotation.z);

if (dist <= this.Radius)
{
ccbRegisterOnFrameEvent(this.registeredFunction);
};
else
{
ccbUnregisterOnFrameEvent(this.registeredFunction);
};

return true;
}

behavior_TurretAI.prototype.agressor32 = function()
{
var me = this;
var now = (new Date()).getTime();

if (Was == null)
{
var Was = 1;
ccbInvokeAction(this.OnAttack)
};
if (now > me.endTime)
{var Was == null};
}



dekon_17
Registered User
Quote
2021-09-01 15:59:07

Yet another update. So, most of the things work as they should, but it appears that the turret invokes OnAttack action every frame, which isn't really great.
/*
<behavior jsname = "behavior_TurretAI" description = "AI for turrets because why not">
<property name = "Turret" type = "scenenode"/>
<property name = "ToAttack" type = "scenenode"/>
<property name = "Radius" type = "float"/>
<property name = "DetectionRadius" type = "float"/>
<property name = "OnAttack" type = "action"/>
<property name = "AttackCD" type = "float"/>
</behavior>
*/

behavior_TurretAI = function()
{
this.LastTime = null;
var me = this;
this.registeredFunction = function() { me.angerFunc();};
};

behavior_TurretAI.prototype.onAnimate = function(node, timeMs)
{
if (this.LastTime == null)
{
this.LastTime = timeMs;
return false
}

var Tur_Pos = ccbGetSceneNodeProperty(this.Turret, "Position");
var Plr_Pos = ccbGetSceneNodeProperty(this.ToAttack, "Position");
//I took the distance calculation script part from "behavior_Test_fileV2" by Sven
var dist=Math.sqrt( Math.pow( (Tur_Pos.x-Plr_Pos.x) ,2) + Math.pow( (Tur_Pos.y-Plr_Pos.y) ,2) + Math.pow( (Tur_Pos.z-Plr_Pos.z) ,2) );

if (ccbGetCollisionPointOfWorldWithLine(Tur_Pos.x, Tur_Pos.y, Tur_Pos.z, Plr_Pos.x, Plr_Pos.y, Plr_Pos.z) == null && dist <= this.DetectionRadius)
{
//I took the rotation script part from the "action_rotatetowardstarget" by just_in_case
var position = ccbGetSceneNodeProperty(this.Turret, "Position");
var rotation = ccbGetSceneNodeProperty(this.Turret, "Rotation");
var pos_target = ccbGetSceneNodeProperty(this.ToAttack, "Position");

var radians_Horizontal = Math.atan2(pos_target.x - position.x,pos_target.z - position.z);
var degree_Horizontal = (radians_Horizontal * (180 / Math.PI));

ccbSetSceneNodeProperty(this.Turret,"Rotation",rotation.x, degree_Horizontal, rotation.z);
};

if (dist <= this.Radius && was == null)
{
ccbRegisterOnFrameEvent(this.registeredFunction);
var was = 1;
}
else if (dist >> this.Radius)
{
ccbUnregisterOnFrameEvent(this.registeredFunction);
var was = null;
};
return true;
}

behavior_TurretAI.prototype.angerFunc = function()
{
var now = (new Date()).getTime();
if (CD == null)
{
var CD = 1;
ccbInvokeAction(this.OnAttack);
this.startTime = (new Date()).getTime();
this.endTime = this.startTime + this.AttackCD;
var now = (new Date()).getTime();
};
if (now > this.endTime && CD == 1)
{
var CD = null;
};
}



Create reply:


Posted by: (you are not logged in)


Enter the missing letter in: "In?ernational" (you are not logged in)


Text:

 

  

Possible Codes


Feature Code
Link [url] www.example.com [/url]
Bold [b]bold text[/b]
Image [img]http://www.example.com/image.jpg[/img]
Quote [quote]quoted text[/quote]
Code [code]source code[/code]

Emoticons


   






Copyright© Ambiera e.U. all rights reserved.
Privacy Policy | Terms and Conditions | Imprint | Contact