I'm Making Health Reduce Consecutively While Interacting With An Object, But It Only Reduces Once You Touch The Object - unreal-engine4

I'm trying to make health reduce while interacting with an object. I'm using Unreal Engine Blueprint. I want it so as soon as you interact with the object your health goes down, but once you stop interacting your health stops reducing.
Here's my blueprint:

I would do this in three parts:
On begin overlap, cast the overlapping object to the player pawn and if the cast succeeds, stash the player pawn in a member variable.
Run a repeating timer that deals damage every x seconds to the pawn stashed in the member variable if it's not null.
On end overlap, set the member variable to null.
This way if something other than the player comes in contact with the object, it's ignored, but when the player makes contact it starts repeatedly dealing damage until the player ends contact.

make private bool variable in the class.
set it to true when begin overlap.
set it to false when end overlap.
in Tick function reduce health when variable is true.
to be very simple...

fallow cpp way and make changes as in Tick function like the person above said ...
make private bool variable in the class. set it to true when begin
overlap. set it to false when end overlap.
in Tick function reduce health when variable is true.

Related

Why does this cast always fail? UE4

I have an ai spawner BP and inside I want to call an event by timer to increase the characters walk speed after 10s, however the cast always fails. I have tried casting to the enemy and also tried to make an enemy ref object variable but still no luck. Any idea why I cant get it?
GetPlayerCharacter returns the current "Player Character" Object; this is the object the player's PlayerController is controlling, not an spawned AI Zombie.
The cast fails because the object going in is not of the type you are trying to cast it to.
Casting doesn't "Get" you an object that you cast to. It can refine the code context for an item that is of that type already.
If you want to get a reference to the zombie to change its walk speed, you need to hold onto a reference of it after spawning to run it into this code.

Pause SKPhysicsWorld in Sprite Kit

Here is my pause method:
- (void)togglePaused
{
self.paused = !self.paused;
self.physicsWorld.speed = !self.paused;
}
...where self is the SKScene.
The reason I include the second line is that I noticed that when unpausing, all my physics-enabled nodes were jumping forward immediately, as if the physics has continued running in the background though the scene was paused.
Unfortunately, that does not solve the problem.
How can you pause an SKScene and the SKPhysicsWorld such that, when unpaused, the physics simulation begins where it left off?
Turns out the second line is in fact not necessary. The problem is that the documentation for didSimulatePhysics is incorrect; it states:
called exactly once per frame, so long as the scene is presented in a view and is not paused
When in fact, it is called regardless of paused status.
Therefore, the basic pause method is correct. Instead, if you are applying modifications to your physics simulation such as I was on every update cycle, then you should check for paused status first like so:
- (void)didSimulatePhysics
if ( !self.paused ) {
[parachute.physicsBody applyForce:CGVectorMake(0, 45) atPoint:CGPointMake(parachute.size.width/2, parachute.size.height)];
}
}
(Unless of course there is a more canon way of applying continuous physics, in which case, the incorrect documentation still stands)

unity jumping collisions - how enemy is damaged

I am making a platformer in Unity using unityscript.
I have a Player parent object with a character controller and various child objects. I have a similar enemy with a box collider. I'm struggling to differentiate between the collision happening when the player walks into the enemy and when the the player jumps and collides with it from above.
I've tried tagging the child objects but they don't have colliders. If I add colliders to the child objects, it messes up my character movement. I've also tried to test the position of the player:
if(col.transform.position.y >= transform.position.y){ killThyself(); }
But this doesn't work either - should I add the height of the enemy? If so how do I do that?
Any suggestions happily received.
Mmmm, I would use a boolean variable. Make it true when you press the jump button, and false once the jump has finished (when the player touches the floor). Declare the boolean as a public variable.
After that, in the OnCollision method, check the variable value.
I recommend you to read this to understand how to do that:
http://docs.unity3d.com/Documentation/ScriptReference/index.Accessing_Other_Game_Objects.html
Well, in your case I would assign the "otherScript" (the player script) doing this in the enemy control script:
var player : GameObject;
var playerScript: PlayerScript;
//I'm assuming "PlayerScript" is the name of the control script of your player,
//where you defined the "jump" variable.
function Start()
{
player = GameObject.FindGameObjectWithTag("Player");
//You also can use GameObject.Find("PlayerName")
playerScript = player.GetComponent(PlayerScript);
//Get the script from the player
}
function OnCollisionEnter(col : Collision)
{
if (playerScript.jump == true)
{
killThyself();
}
}
I'm finding the player on the function Start, to find it only ONCE, in the level load. Now, I assign the player script into a variable, so, when the player hit the enemy, this enemy will check if the player is jumping using his script. Remember to declare this variable as a public variable or this won't work.
Please comment if you have any problem using this method =)
I would use a second collider at the upper part of the enemy let's called it headCollider. Set isTrigger = true and maybe use a special physics material. If this headCollider is the first one to get triggered you know that the player chararacter is jumping on the enemy or is falling on it from above.
In OnTriggerEnter (Collider other) you can prepare some status variables (and maybe a timer for resetting the status). Also the current jump status like in V_Programmer's answer suggested should be useful for evaluation.
As Unity does not allow you to attach 2 colliders of the same kind, you have to use box and sphere or create an empty child and use that one.

Stop body from moving. Chipmunk on iPhone

I have a cpBody with single cpShape that floats in my scene colliding with other bodies et c. How can I easily make this body stay in one place and act kind like a static obstacle staying in one place so it's not longer moving but still colliding with other bodies.
I just want to stop body from moving when user taps on it. That's why I'm asking. I'm not an expert in Chipmunk but I think it must be easy.
The way you'd do this with the public API is to remove the body and shape from the space. Create a new static body with the same position/rotation as the old dynamic body. Use cpShapeSetBody() to change the body to the new static one, and then readd the shape to the space.
You can call cpBodySetMass to INFINITY, and force the object to sleep with cpBodySleep. This is how a static object is implemented internally (at least about the mass).
EDIT
I am not sure whether you need to call cpBodySleep after this or not, but I don't think it hurts to call.
Modify cpBody.h and put #define CP_ALLOW_PRIVATE_ACCESS 1 at the beginning. Then from cpBody*, access ->node.idleTime and set it to INFINITY.
EDIT 2
The above solution is a working solution, but not very good in term of SE practice. It is better to define a function that makes the object static or dynamic so that you can call without disabling private property for the whole object.

Creating pointer Attributes in cocos2d iPhone

I am working on a game. There are balls that fall from the top of the screen, and the player has to catch them, as the are caught they stack ontop of one another. I have a method that creates each new ball and adds it to an Array that i use to move the sprites. Problem is that the after they collide I need them to stop moving, since the array is called on to move them they all move. And if i try to make them stop they all stop. So I was hoping to create a pointer attribute if ther is such a think, for example "sprite.position" I need a new attribute that i can check like a boolean. I was hoping to create a attribute like sprite.hasCollided and if it returns YES then the ball should no longer move. Is this possible or is there a better way to do it?
Thanks
Tanner
I would suggest you create a ball object. And add the boolean as as part of the object.
CCNodes (and, by inheritence, CCSprites) have a userData property, which is a void*. You can use this to relate a custom object to a cocos2d object. Keep in mind if you use the userData option, you will, in most cases, need to allocate any memory when you create/assign the sprite, and release it when you are done.
int* myInt = (int*)malloc(sizeof(int));
*myInt = 0;
sprite.userData = myInt;
//some time later, when you are done with the sprite
free(sprite.userData);
As an improvement on the userData property, you can do what xuanweng suggests and create a ball object containing various game-related properties for the balls, and assign an instance of this to each of your ball CCSprites using the method above.