![]() Transform.right = Vector3.Reflect(transform. Many unique optical properties in 2D materials have been revealed, including universal absorption regardless of the wavelength, helicity of valley excitons 10,11, near unity excitonic reflection and photoluminescence (PL) quantum yield 12,13, single-photon emission, Dirac-type plasmons, and so on. void CollisionWithWall(Collision2D other) So you have to freeze the rotation otherwise our direction would mess up on collisions. However you have to ensure the rigidbody doesn't rotate on it's own. If you want to rotate the object's right direction into the movement direction and you want to move the bullet always at the same speed you could do what you originally did. Rb2d.velocity = Vector3.Reflect(lastVelocity, ) That way we know the last velocity and we can reflect it correctly: Vector2 lastVelocity For this there are two possible solutions.įirst you can simply store the last velocity vector in a variable inside the FixedUpdate callback. ![]() You need to know the velocity / movement direction before the collision. To help show the three dimensional perspective, a pink triangle connects the vector to its projection (a1,a2,0) in the xy-plane (gray arrow). The original approach i had of course didn't work as we can't use the current velocity of the rigidbody inside OnCollisionEnter2D since it got already changed due to the collision. Therefore your velocity and your right vector do not point in the same direction. However after the first bounce it's rotated kind of arbitrarily. If you just look at the orientation of your ball / circle the rectangle is perpendicular to the movement direction. Why do you reflect the local right direction? Shouldn't you reflect your velocity vector? Your custom rotation code seems to mess things up. Please Help, I need to get this working correctly and smoothly ASAP For Rotate The Bullet Towards its velocityįloat angle = 90 - Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg Rb2d.velocity = (reflectedPosition).normalized * bulletSpeed Vector3 reflectedPosition = Vector3.Reflect(transform.right, ) Void CollisionWithWall(Collision2D other) Void OnCollisionEnter2D(Collision2D other)Įlse if (("Destroy"))Įlse if (("Target"))ĭebug.Log("Hit: " + ) Rb2d.velocity = transform.up * bulletSpeed Public float bulletSpeed public Rigidbody2D rb2d void Start() So, the 2D solution as presented on that wikipedia page to find the perpendicular line is: Code (csharp): Vector2 bc c - b Vector2 ab b - a Vector2 ad ab - Vector2. This is useful if you need to find the surface area of the whole mesh or want to choose triangles randomly with probability based on their relative areas.I am working on a game where I can launch a ball and when it hits an object it can bounce off without being random, but when I tried to do it I ran into an issues where I was getting weird collision feedback: watch this video and see what I'm talking about: !Ai7u0bw9Rsr-hukdwxJ_MwJ6MaIdLA ![]() It turns out that the area of the triangle is equal to perpLength / 2. You can also normalize the perpendicular vector by dividing it by its magnitude:- var perpLength = perp.magnitude This can be done with the normalized property, but there is another trick which is occasionally useful. The result will point in exactly the opposite direction if the order of the input vectors is reversed.įor meshes, the normal vector must also be normalized. As you look down at the top side of the surface (from which the normal will point outwards) the first vector should sweep around clockwise to the second:- var perp: Vector3 = Vector3.Cross(side1, side2) The “left hand rule” can be used to decide the order in which the two vectors should be passed to the cross product function. The cross product of these two vectors will give a third vector which is perpendicular to the surface. Pick any of the three points and then subtract it from each of the two other points separately to give two vectors:- var a: Vector3 Given three points in the plane, say the corner points of a mesh triangle, it is easy to find the normal. The term direction vector, commonly denoted as d, is used to describe a unit vector being used to represent spatial direction and relative direction. See in Glossary generation and may also be useful in path following and other situations. That corresponds to the two different purple arrows in your diagram: one negated x and one negated y. Nurbs, Nurms, Subdiv surfaces must be converted to polygons. In 2D you can form a perpendicular with a very simple trick exchange the x and y components, and negate one of them: purple.x red.y purple.y -red.x There are two ways to do this of course we could have negated either x or y. ![]() Unity supports triangulated or Quadrangulated polygon meshes. ![]() Meshes make up a large part of your 3D worlds. A normal vector (ie, a vector perpendicular to a plane) is required frequently during mesh The main graphics primitive of Unity. ![]()
0 Comments
Leave a Reply. |