Ambiera Forum

Discussions, Help and Support.

Ambiera Forum > CopperCube > Help with CopperCube
Help with a simple ship project of mine

admiralsmith
Registered User
Quote
2023-03-22 04:00:37

Okay, I'm an amateur at 3D modeling in sketchup but have managed to make some cool naval warships that I can easily port to Blender whenever needed and what I want to do is make a very simple and bare bones game like Modern Warships (which is on the play store and app store) for my own personal use to experiment with my creations.

I've downloaded and fiddled around with CopperCube for a few days now and I'm quite impressed with how much even I can do with it. Thus far, I already have a small world with a seabed, ocean, and a ball that has a "bobbing" animation (sort of).

However, despite reading and watching many tutorials, I have yet been able to figure out how to apply keyboard controls to the item I wish to move (which is a small sphere in place of a warship at the moment).

What I wish to do next is tap the W key and the item will move forward at a set speed, then if I tap it again, it will move forward at a set increased speed without needed to hold the W key down.
Same with the reverse but only one speed setting. But with moving left or right, holding the A or D keys down would be necessary.

If you could help me out in this regard, I would be most thankful as I am currently unable to figure out how to perform such via tutorials.


Aiming_bullets
Guest
Quote
2023-03-22 04:36:13

You can use behavior "Object or Person Controlled by Keyboard" and then set the variable "On key pressed do something" to use the special variable "player.movementspeed" to change the player to the name of the player whose speed you want to change. You can set the speed to whatever value you want.


Guest
Guest
Quote
2023-03-22 04:46:48

How do you plan on stopping the boat?


admiralsmith
Registered User
Quote
2023-03-22 05:00:10

Guest wrote:
How do you plan on stopping the boat?

By tapping the S key two times so that the ship comes to a stop.


admiralsmith
Registered User
Quote
2023-03-22 05:01:56

Aiming_bullets wrote:
You can use behavior "Object or Person Controlled by Keyboard" and then set the variable "On key pressed do something" to use the special variable "player.movementspeed" to change the player to the name of the player whose speed you want to change. You can set the speed to whatever value you want.


I know this, however, it doesn't allow me to do the task I desire. Thats moreso for other game characters like racing or shooting games.


Guest
Guest
Quote
2023-03-22 05:44:19

All right. I'll look into it.


Guest
Guest
Quote
2023-03-22 07:45:21

Here's the code for what you asked:


/*
<behavior jsname="behavior_Boat" description="Move like a boat">
<property name="MinForwardSpeed" type="float" default="0.1" />
<property name="MaxForwardSpeed" type="float" default="0.2" />
<property name="ReverseSpeed" type="float" default="0.1" />
<property name="ActionOnMinForward" type="action" default="" />
<property name="ActionOnMaxForward" type="action" default="" />
<property name="ActionOnReverse" type="action" default="" />
<property name="Action" type="action" default="" />
</behavior>
*/

behavior_Boat = function () {
this.mode = null;
this.keyA = false;
this.keyB = false;
this.lastTime = Date.now();
};

behavior_Boat.prototype.onAnimate = function (node) {
var now = Date.now();
var delta = now - this.lastTime;
var pos = ccbGetSceneNodeProperty(node, "Position");

switch (this.mode) {
case 1:
pos.x += this.MinForwardSpeed * delta;
ccbSetSceneNodeProperty(node, "Position", pos);
ccbInvokeAction(this.ActionOnMinForward);
break;
case 2:
pos.x += this.MaxForwardSpeed * delta;
ccbSetSceneNodeProperty(node, "Position", pos);
ccbInvokeAction(this.ActionOnMaxForward);
break;
case -1:
pos.x -= this.ReverseSpeed * delta;
ccbSetSceneNodeProperty(node, "Position", pos);
ccbInvokeAction(this.ActionOnReverse);
break;
case -2:
ccbInvokeAction(this.Action);
break;
default:
// Do nothing
break;
}
this.lastTime = now;
}

behavior_Boat.prototype.onKeyEvent = function (key, pressed) {

if (key == 87 && pressed) {
if (!this.keyA) {
this.mode = 1;
}
if (this.keyA) {
this.mode = 2;
}
}
if (key == 83 && pressed) {
if (!this.keyB) {
this.mode = -1;
}
if (this.keyB) {
this.mode = -2;
}
}

if (key == 87 && !pressed) {
this.keyA = true;
this.keyB = false;
}
if (key == 83 && !pressed) {
this.keyB = true;
this.keyA = false;
}
}


Copy the code, paste it into a text file, and save it as behavior_Boat.js. Restart CC and you should be good to go. I assume you're planning on rotating the world around the boat with A and D? You didn't mention anything about that so I just did what was in your post. Let us know if you need any more help. Cheers.


Guest
Guest
Quote
2023-03-22 07:50:00

Oops. Make sure you the put the behavior_Boat.js in your CC documents/extension folder. Then restart CC. Enjoy.


veganpete
Registered User
Quote
2023-03-22 08:05:43

You can do it with default actions/behaviours (without coding):

When scene starts, before first drawing (and on reload):
Set variable "Speed" = "0"

When "W" is Released:
Set variable "Speed" +1;

When "S" is Released:
Set variable "Speed" -1;

Every few seconds (20ms):
If "Speed" > "2", Set Variable "Speed" = "2";
If "Speed" < "-2", Set Variable "Speed = "-2";

Every few seconds (20ms):
If "Speed" = "0", change relative position of "ship" (0,0,0)
If "Speed" = "-1", change relative position of "ship" (-1,0,0)
If "Speed" = "-2", change relative position of "ship" (-2,0,0)
If "Speed" = "1", change relative position of "ship" (1,0,0)
If "Speed" = "2", change relative position of "ship" (2,0,0)

*Basically this method is the same as aiming_bullets suggestion - but rather than using the variables to change the "player.movementspeed", you're using the variable to physically change position of the ship, every few milliseconds instead.

The reason I'd use "on key release", instead of "on key press" is so it will increment the speed (up or down) on each "W" or "S" key depress - if you use "on key press" instead, windows has an annoying key-repeat feature - so if you hold a key down long, Windows repeats the keypress multiple times (which screws things up).

The second part limits the variables to a range of "-2" to "+2", so you'll never get a speed of "3" or "-8" for example.

As you can see, it looks like code, so you (or someone else) could probably easily use this template to convert it to code if you prefer.

It looks a bit complicated, so I can make a ccb file to demonstrate it for you (if you don't get a better solution in the replies).


veganpete
Registered User
Quote
2023-03-22 08:06:45

Sorry @Guest, we posted at the same time.


Guest
Guest
Quote
2023-03-22 08:14:00

@veganpete

No worries. It's probably helpful OP learns both ways.


Guest
Guest
Quote
2023-03-22 08:38:18

@admiralsmith

Just noticed part of my code got blocked by the forum. There is supposed to be an "Action_On_Stop" without the underscores as the last property in the xml header. I will just upload the behavior file itself here:

https://files.catbox.moe/qrxlhy....


admiralsmith
Registered User
Quote
2023-03-22 16:35:14

Guest wrote:
@admiralsmith

Just noticed part of my code got blocked by the forum. There is supposed to be an "Action_On_Stop" without the underscores as the last property in the xml header. I will just upload the behavior file itself here:

https://files.catbox.moe/qrxlhy....

Thank you so much for your assistance!
It's not perfect but its definitely a start!

Would you happen to know what settings I need to apply to the ship behavior to get it to function correctly?

Also I've tested it out and it seems tha the 3rd person camera is acting wacky.
It should follow behind the object rather than act as if its attracted to said object (like a magnet I suppose).


Guest
Guest
Quote
2023-03-22 16:39:06

@admiralsmith

In your 3rd person camera behavior change "FollowSmoothingSpeed" to 0. That should get rid of it. You're welcome.


admiralsmith
Registered User
Quote
2023-03-22 16:54:38

Guest wrote:
@admiralsmith

In your 3rd person camera behavior change "FollowSmoothingSpeed" to 0. That should get rid of it. You're welcome.


Ah thx. That seems to have done the trick.
However, I have it set to follow behind the target, however to doesn't seem to do so.
It behaves more like a cinematic camera than the camera I need.


Create reply:


Posted by: (you are not logged in)


Enter the missing letter in: "Internatio?al" (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