My 2d platformer has a "time travel" mechanic.
You place down a hologram at a location (actor with no collision and some pretty effects).
While you hold down the "Rewind" button, your character's collision and gravity are turned off, and he slowly moves towards the hologram.
When you release the rewind button, it turns his collision back on.
My tilemap is set to "block" the pawn (so he can walk on it, etc).
If you release the button while he's overlapping the terrain, and it turns his collision back on, he gets stuck.
I'm wondering what to do. Unity automatically "ejected" an object that was overlapping something it shouldn't be.
Since the terrain is set up to block the pawn, I can't fire an overlap event. "On Component Hit" will fire just from him standing or rubbing against the terrain, so that won't do either.
How should I detect that the player is popping up inside the terrain? I can decide what to do later (eject them, kill them, prevent them from appearing, etc). But for right now I just need to differentiate from them touching the tilemap and being inside it.
Any ideas?
So what I wound up doing was duplicating the capsule collider on my player. The new capsule (called 'Terrain Overlap') ignores all collisions, except it overlaps the tilemap.
The component has a begin and end overlap method that sets a boolean on the character.
When the player lets go of the Rewind button, if they're inside the ground (boolean check), I can fire the death method.
Related
I have a node called "SlidingBlock" which is a RigidBody2D, its mode is Character so I can move it with the physics engine but not rotate it. It also has a CollisionShape2D.
I've programmed a kicking action on which I set a value for its linear velocity, like this:
block_touched.linear_velocity.x = BLOCK_SPEED
The block moves until it collides with another RigidBody2D which is in mode Static and, when it collides, it goes one pixel forward and then back. It looks weird in my game which is a somewhat low resolution pixel art game.
I tried creating a Physics Material with Bounce set to 0 with no success. How can I achieve a collision with absolutely no bounce effect?
I just recently began using Unity3D and this is my first game, so if I'm not doing things like I'm supposed to, please tell me.
There is a 2D character that I want to move one step forward using the Animation mechanism. This movement can be splitted in two parts :
Move forward the back leg, don't move the upper body ;
Use this leg to push and make the step, moving the upper body.
My problème is that I can't find a way to make this step permanent. As soon as it start looping on the "Idle" animation, then, it come back to the original position (before the step).
I don't know how to let my character stay in the position it was just between the "Step Forward" animation and the "Idle" animation.
I tried to add "Rigidbody 2D", "Box Collider 2D" and "Physics2D Material" on both feet and on the floor, but it somewhat breaks the IK script ("Simple CCD", from the youtube video "Unite 2014 - 2D Best Practices In Unity"). And I'm sure there is a much simpler way.
EDIT: My question was not clear enough. The idle animation is the default animation, where the character is doing nothing in particular. It's just moving a little while it's staying in position. When it runs "Step Forward", all the sprites are moved (only when the back leg is pushing). If all animations are stopped right now, then the character is indeed in a new position, closer to it's opponent. I can check that by removing the transition from "Step Forward" to "Idle". But from here, I want it to loop on the Idle animation. When the transition from "Step forward" to "Idle" is done, the character is NOT running Idle from it's new position, but from it's old position, before the step, as if it was pulled from backward. I want to run Idle from the new position.
Here is a longer gif when we can see more of the Idle animation :
And my Hierarchy panel looks like :
Main Camera
Karateka (the top GameObject container, with the Animator component)
Torso (the sprites)
Head
Right arm
Right leg
Right lower leg
Right foot
...
IK (the Inverse Kinematics targets)
Left fist target
Left foot target
...
Any suggestion is welcome.
If I get it right, you have the StepForward animation running and it gets cut in the middle back to idle. There, the StepForward animation triggers again but from the start and you wish to restart from where you left off:
You can record the normalized value of your StepForward animation and use it next time:
float normValue = this.animator.GetCurrentAnimatorStateInfo(0).normalizedTime;
then you can use that value:
this.animator.Play("StepForward", 0, normValue);
Other possibility, you are not after that but simply you want to pause the animation without getting to Idle.
Use a parameter. You can add a float parameter (call it direction) in the Animator window (top left). Then select a state in the animator, it will open the animation setting in the inspector. Under speed, open the drop down where your new parameter should show up. And tick it.
Now, your direction float will affect the speed of your animation as such:
this.animator.SetFloat("direction", value);
if you give 0, you pause. Give back 1 to move forward. You can also lerp the value so it is not abrupt. Your way.
I'm making a 2D platformer and I've come across a really annoying problem where if I move my player character GameObject to another location on the scene, my player becomes stuck and the game spazzes out, jumping from the players original location and the position I moved it to.
My player character is made up of many parts, each a separate GameObject. I know the problem is definitely in my animation, because if I disable the animator component, the problem goes away, just I don't have my animations anymore. I believe the problem may be in the player character's Idle Animation and it's position property. There is no script attached where his starting location is hard coded.
How can I make the child Gameobjects move relative to the parent Player GameObject? I cannot move the player in the scene from its original location without the game glitching up.
Here are some screenshots
[Player and it's parts in Hierarchy]http://i67.tinypic.com/bdlc1j.png
[Idle Animation]http://i64.tinypic.com/2gtp99x.png
[Player's original Location, he works if starting here] http://i66.tinypic.com/261jb6c.png
[Player is Moved, game bugs out] http://i67.tinypic.com/292a2c3.png
Try to disable "Apply Root Motion" flag in the Animator component.
The problem should be related to the fact that the animation changes the position values
My guess is that you have animated the different parts by moving then around in the editor and recording that. This meas that the animation is keeping track of the original position at which you did the animation. Try deleting those parts from the animation.
I couldn't fix the spazzy-jittering problem that occurs when I move the player character to another position on the scene, but I did find a way around it. The problem definitely lied within the Animator component and the gameObject's Rigidbody2d.
Instead of moving the player to another position, I instead made a 'Spawn Point' which the player starts from when first playing the scene, and that was able to be moved freely around. Pretty much:
void start(){
transform.position = spawnPoint.transform.position;
}
This never made sense to me. I looked at GLFW's and Three.js' examples and Cinder's implementation which actually has this comment in there:
mark all windows as ready to draw; this really only matters the first
time, to ensure the first update() fires before draw()
All three libraries seem to be doing that and I don't understand why. There really is no point in updating the i.e. position of something that's never been drawn on screen or is there?
Here's what my loop looks like:
Draw the (first) frame
Swap buffers
Update events
Animate (with the input from the events), update logic, ...
Start from the top
This order makes a lot more sense to me but maybe I'm missing something.
I think that it makes perfect sense to update objects first and then draw them.
Imagine you animate a ball going from one side of the screen to the other and back just like a pendulum. Imagine you also want it to actually reflect real time in your computer. If you draw your scene before updating the ball's position, then where your ball will be positioned in the first frame? Unless you set it's initial position manually, it would be at zero point of your scene, which might be completely out of it's intended trajectory. If you decide to initialize it's position before start of animation, it might happen that there is a time gap causing it to be in the wrong position too.
But if you always update it's position before drawing, it will be on the right place right from the first rendered frame.
But to be honest - nobody will probably notice the first frame, so it is more sense of logic doing it that way rather than any practical reason. I just feel it makes more sense to prepare your scene before drawing it, not vice versa.
I am new to unity. I have two animation in .fbx format.They can move..Now i want when both will collide with each other a sound will produce.Is there any idea how i will do this.Thanks in advance
I think you need to read about how Physics work, and then how Trigger-Events and Colission detection is handled.
Read this here, and this. The first one gives you insight on how the Unity engine works. The latter provides a video tutorial on how to do Collision Detection.
If you don't want to do that and just want the code, I found this on a quick Google:
var crashSound : AudioClip; // set this to your sound in the inspector function
OnCollisionEnter (collision : Collision) {
// next line requires an AudioSource component on this gameobject5.
audio.PlayOneShot(crashSound);
}
You can add a MeshCollider to the fbx meshes. Anyway, this is not a good idea because this will cause performance issues.
You can create an empty gameobject for each character, and add to them: the fbx animation and a simple collider (some cube, sphere, capsule, etc). Then, when you use a script for them, you attach it to the parent object and from there you handle the whole thing.
If you want that the collider moves from specific places from the animation (Like the punch movement, or a kick),then you can ask to your 3D animator/modeler to add a simple mesh on that points. For example, a sphere on one punch, which will move with the animation. Then, in Unity, you will hide the mesh of the sphere but add a mesh collider to it. :)
Hope it helps!
Most of the time, if you apply an animation to an object then you'll loose the physics reaction. Don't trust me? See here: http://www.youtube.com/watch?v=oINKQUJZc1Q
Obviously, animation are not part of Unity physics. Think about it... Unity physics decide position and rotation of objects accordingly to Newton and friends laws. How do you think these laws can accord to a keyframe arbitrary animation? They can't: hence the crazy results you get when you try.
How to solve it? Use Unity physics also for animation: learn to master rigidbody.AddForce and all the other stuff described here.
You may always want to keep the physics and the animation separated. That's how you get out of trouble.
If you really want to know: here's my personal experience on how to mediate physics with animation.
Sometimes, even binding a simple parameter to the physics and another
to an animation (or a script which mediates user input) may result in
catastrophic results. I've made a starship: rotation controller by
user mouse (by flagging "block rigidbody rotation"), direction and
speed by physics. It was inside a box collider. Imagine what happens
if a cube, orientated by a few degrees angles, meets a flat ground: it
should fall and rotate until one of the faces lays completely on the
ground. This was impossible, as I blocked any physics interaction with
the rotation of the body: as a result the box wanted to get flat on
the ground but couldn't. This tension eventually made it move forward
forever: something impossible in real world. To mediate this error,
I've made the "block rotation" parameter change dynamically according
to the user input: as the ship is moving the rotation is controlled by
the user but as soon as the user stop controlling the ship the
rotation parameter is given back to the physics engine. Another
solution would be to cast a ray down the collider, check if the ground
is near and avoid collisions if the ship is not moving (this is how
the banshee in Halo Combat Evolved is controlled, I think). When
playing videogames, always have a look at how your user input is
mediated into the physics engine: you may discover things which a
normal player normally wouldn't notice.