I have a player sprite (playerTexture) and a crosshair sprite (crossTexture) in my game. I need to make the player sprite always face towards the crosshair.
Does anyone know how to do this? I have tried doing it myself but the math involved boggles my mind. I know there's a rotation parameter in the spriteBatch.Draw() method but I'm unsure how to use it.
Thanks!
rotating a sprite to following another sprite is mostly just triangle math. I was going to try and type up a good explanation, but then i found this posting - which includes pictures to help you along the way, which hopefully will help you understand what's going on and what you need to do.
http://www.berecursive.com/2008/c/rotating-a-sprite-towards-an-object-in-xna
Related
Im working on a build-engine style unity game, and something regarding my explosion sprites has really been bugging me. My map is split into several pieces for both occlusion culling and my method for dimming certain sections. I want this explosion sprite to only render up until the nearest wall to its left and right, I thought that maybe sprite masks would work but I can't think of any way to implement this in code.
I'm trying to make my character(a box with 2 long boxes as legs) walk using the legs. The point of the game is the challenge of balancing the character and walking with the legs. I've tried using Hinge Joint 2D but I've never used it before so I have no idea of how to use it. I've also seen people use animations but I'm not sure if it would suit my game well because I want the player to be able to rotate each leg 360 degrees so he could find creative ways of walking. Any suggestions?
Joints seem like the best option here. The official unity youtube channel has some good resources for joints: https://www.youtube.com/c/unity/search?query=joint
this is my first answer so please fell free to comment if you have any doubts.
what you and use is making a script to move the leg or in this sense rotate it in one direction. Something like:
GameObject playerLeg;
Input.GetKeyDown("x");
playerLeg.transform.Rotate(10, 0, 0);
if you don't clamp the Leg rotation you should end up with a 360 rotation.
P.S. don't forget to add colliders to the player Body, legs and Floor.
if you want to go a log way with smoother animation and a bit more complex gameplay further in the game you can check out Procedural animation
i could need some help with this problem:
I have a ball flying around in random directions. everytime he collides with something, he changes the direction.
i also have a player, for which i want to write a simple ai script.
the player should go to the point, where he could meet the ball.
i made a simple picture for this problem in paint so that it's better to understand:
What i have is the yellow arrow, thats the ball's direction that it's moving. what i need is the red arrow direction for the player, but i don't know how to do this?!
thanks for your help!!
I'm currently working on a narrative game and don't know how to accomplish something. The game is a 2D platformer so everything is build with sprites. I have a scene with trees and the trees have eyes, I want the eyes to follow the player along, but the "pupils" need to stay within the "holes" of the tree. I have made separate sprites for both the hole in the tree as well as the pupils of the eye's. I would prefer to write it in c# :).
I have also added a concept screenshot, so you can get an impression about what i'm trying to accomplish.
http://imgur.com/pGCV8Uy
Many thanks to the person who can explain to me how to accomplish this!
Well I suppose you could use a joint to restrict the yellow pupils to stay within the holes of the eyes.
Then you could use C# to calculate the relative Vector between the character and the pupil. Guide the pupil to go in the direction of the Vector, and it will be restricted by the joint to stay in the eye. The would result in the pupil always pointing in the direction of the character while staying in the eye, which is what you want.
I am writting a collision detection engine for my game and I have some problems.
Indeed, since I have several fixed rectangle and one moving (the player), I need to know which side of the fixed one was collided at first by the player, to replace him correctly.
The fixed rectangle are NOT in a grid so they can be placed anywhere on the map and they can have diffents size. They are not rotated.
The player class stores it's direction vector.
Any idea?
KiTe
In a nutshell:
You'll compare the Y and X components of the bounding rectangles to eachother to check for a collision. If the top(Y) of the player is less than the bottom of an enemy then you don't need to check anymore because it's not possible that they're colliding. If the right(X) side of the player is less than the left side of the enemy then they can't be colliding. It would help to define top, right, bottom, left of each object you intend to check inside the class. This will allow you to know which side is hit also. This should be enough to get you thinking and experimenting.
Have fun!
The name is "Axis-Aligned Bounding Box" collision detection.
Now you know the name, you can Google for the rest.
thanks to both of you for your help.
I've heard about AABB, but at first sight it didnt seem to fit to my needs (since I didn't understand it well).
But after writing down everything on papper, the solution I found appeared to be exactly the same as AABB !