I'm trying to make a projectile shooting system, but the Sweep Result is empty. I need it to see whether or not the projectile hit the bone 'head' and to see the hit location.
I made sure that I have a sweep collision on the projectile movement and the character movement.
Here is the overlapping event with the server event handling the ProjectileDamage:
https://blueprintue.com/blueprint/v9gzb-uf/
(Scroll up to see the OnComponentBeginOverlap Event)
The actual system works, but the sweep result is empty.
Thank you in advance
Physics or Projectile Movement
Use OnComponentHit, not OnComponentBeginOverlap, if using physics or a projectile movement component.
OnComponentHit
Event called when a component hits (or is hit by) something solid. This could happen due to things like Character movement, using Set Location with 'sweep' enabled, or physics simulation. For events when objects overlap (e.g. walking into a trigger) see the 'Overlap' event.
For collisions during physics simulation to generate hit events, 'Simulation Generates Hit Events' must be enabled for this component.
When receiving a hit from another object's movement, the directions of 'Hit.Normal' and 'Hit.ImpactNormal' will be adjusted to indicate force from the other object against this object.
NormalImpulse will be filled in for physics-simulating bodies, but will be zero for swept-component blocking collisions.
OnComponentBeginOverlap
Event called when something starts to overlaps this component, for example a player walking into a trigger. For events when objects have a blocking collision, for example a player hitting a wall, see 'Hit' events.
Projectile Movement
If using Projectile Movement Component, you can also use OnProjectileStop event.
Verify hit is impacting a skeletal mesh
You'll only get a Bone Name in the hit result if you are impacting a skeletal mesh.
Example Project
To see an example of a physics impact and a projectile movement impact hitting a skeletal mesh and returning a bone name, see this example project.
I now figured out how to fix my issue.
I just had to set the the object type, from the character, from 'Pawn' to World Dynamic. Now the projectile also has a Hit Result.
Related
I have been working on my own kinematic character controller for a while, but only recently learned that one should consider updating its position and rotation using Rigidbody.MovePosition and Rigidbody.MoveRotation instead of directly modifying the Transform component, which is what I am doing now. While my controller works fine, and does not cause any noticeable performance impact in my small games, I want to better understand the benefits of manipulating the Rigidbody component, and especially how this all works within FixedUpdate instead of Update.
To be clear about the supposed impacts on performance, Unity's manual on 2D rigidbodies says:
Any Collider 2D component added to the same GameObject or child GameObject is implicitly attached to that Rigidbody 2D. When a Collider 2D is attached to the Rigidbody 2D, it moves with it. A Collider 2D should never be moved directly using the Transform or any collider offset; the Rigidbody 2D should be moved instead. This offers the best performance and ensures correct collision detection.
Right now, my basic controller loop looks like this in the Update method:
Read the user's input velocity
Modify the controller's velocity based on this input (this velocity is distinct from Rigidbody.velocity)
Using Physics queries, sweep the controller's body against the appropriate collision layers by velocity * Time.deltaTime
Pick a collision resolution position based on a set of rules
Set the controller's Transform.position to this resolution position
I also have a method that moves the controller using Transform.Translate if no collision detection is needed.
As you can see, though my controller GameObject has a kinematic rigidbody attached to it, I never actually use it for anything. Unity's documentation on kinematic rigidbodies is conflicting, stating here:
Note: MovePosition is intended for use with kinematic rigidbodies.
but also here:
If isKinematic is enabled...The rigidbody will be under full control of animation or script control by changing transform.position
I am also unsure about how to manually check for collisions within FixedUpdate, as I have read that FixedUpdate can be called more than once per frame. Frankly, I thought FixedUpdate was only supposed to be used with dynamic rigidbodies to simulate real physics, and thus have only used this method for simple manipulation of those kinds of rigidbodies.
And then Rigidbody.MovePosition states that
2D rigidbodies have a fixed limit on how fast they can move therefore attempting to move large distances over short time-scales can result in the rigidbody not reaching the specified position during the next physics update. It is recommended that you use this for relatively small distance movements only.
What if my character needs to move a very large amount? Do I just set Rigidbody.position directly?
I guess I am trying to figure out how I can reconcile all of this logic to conform to what Unity says is best for performance. Perhaps I need to combine Update and FixedUpdate, but ultimately I am just hoping to receive some clarification and guidance regarding all of this.
I have a brand new blueprint derived from the Character class, and I've added a Projectile Movement Component onto it. I'm having trouble making the character Jump.
The reason why I want to do this is I want to use the projectile movements bounce physics while my Character is in the air, but while they are on the ground use the Character Movement Component to move.
If I set the initial speed of the Projectile Component I can spawn with some forward momentum. However if I try to bind any kind of force or change in velocity to some user input - my character refuses to leave the ground!
My blueprint: https://imgur.com/a/UBuzcq3
When I spawn in the game world, I can see "Jumping" displaying properly but I do not move:
https://imgur.com/a/rGXX3gG
Can someone help me understand why this is happening and how to fix it? I'm fine with C++ solutions as well (I've also tried AddForce function in C++ but have the same problem!)
I have noticed that if I tick "Rotation Follows Velocity" in the Projectile Component then I do get some movement but its super weird and jerky and not at all useable.
player and object both have colliders and rigidbodies, object has position and rotation locked, player has only rotation locked. When the player goes to the blocks, the player goes through the blocks, although they do give a bit of resistance. To move the player im setting the rigidbody's velocity, and doing that in FixedUpdate.
i have no idea why this is happening, any ideas?
main part of the code is:
rigidBody.velocity = new Vector3(direction.x, rigidBody.velocity.y + (-Gravity * Time.deltaTime), direction.z);
(direction is determined by the WASD keys, and i'm using my own gravity)
First of all, you do not need to multiply the velocity by time.DeltaTime, because you are moving your object in the FixedUpdate() method; Which uses fixed time intervals since the physics engine does not run in sync with the regular game engine. Also, both objects do not need rigidbodies in order to collide with one another. I suggest looking at your collision matrix in settings and verifying that everything you need collision for is checked correctly. As others have said as well, check your kinematics on the rigidbody.
A last suggestion for working with your own gravity. Do not change the actual gravity value of the game engine. It is typically recommended that you use a multiplier variable and apply it to the constant gravity already set by the physics engine. If you are completely editing the gravity, than maybe consider using a character controller instead.
I guess it has something to do with what the documentation says "In most cases you should not modify the velocity directly, as this can result in unrealistic behaviour".
Try to use AddForce() or similar functions to alter the properties of the rigid body. Colliders etc will then work as expected.
I am making a simulation of some fish in a lake.
I am using rigid body as otherwise they move through the sides. But when they hit into the sides they rotate and then the script I have to set their rotation to 0,0,0 when they leave fails.
Your problem may be occurred by the fact that you are using unity's physics to move the fish but trying to set rotation directly. In this case movement and rotation of the fish are driven by Physics and forces, applied to the object. But you still can affect rotation and position if you reset all forces. Try looking here to learn how to remove forces.
Also when dealing with Physics in Unity, don't mess Update() and FixedUpdate() methods, because it may lead to an incorrect result.
When you say "hit into the sides" do you mean the sides of the simulated lake?
If so, one option might be to set the objects that denote the sides as "kinematic." Then you can detect when a GameObject (such as a fish) collides with it but it won't cause any physics reaction.
(ref: RigidBody)
I have been looking at options to detect a clicked GameObject. For a 2D game, what is the difference between Physics2D.OverlapPoint and Physics.Raycast? Is there any advantage of one over the other, performance maybe? I get the same return value for both and both seem to have the same problems with overlapping sprites. I've been leaning towards using Physics.Raycast since I may want to move to a 3D to a top-down perspective in the future, any other considerations?
A picture is worth 1000 words:
Here is the raycasting:
Here is OverlapPoint, which checks if a collider overlaps a point in space.
OverlapPoint checks if a collider overlaps a given point in space while Raycast shoots a ray along a specific vector and returns anything it hits.
Very different concepts and not to be used interchangeably... even in 2D. You can fire a Raycast starting beyond your 2D scene and fire back toward the viewer (defined by the direction vector) or in front of the scene and fire away from the viewer. Different results will be returned as they ray will hit different colliders depending on z-order.
OverlapPoint is basically a Raycast except with no depth to it. You are right about using the Raycast if you plan on going 3D in the future. Also the returns are different, where Raycast returns you a bool and OverlapPoint returns you a Collider2D