Unity Constant rotation whilst using Transform - c#

This is something i've struggled with all week. I'm working on a 2D project, and what I want is for my enemies to move from the right to the left hand side of the screen (landscape). I'm moving them like this:
transform.Translate (new Vector3(1,0,0) * speed * Time.deltaTime);
At the same time, I want them to constantly rotate on a pivot in the middle of the sprite. As an example, imagine they are in space and are sort of floating uncontrollably, they would spin. I've asked this question a couple of times with no response so I guess my explanation isn't really very good. This is what I've tried:
Animating the objects. This didn't work because changing the Z rotation caused the sprites to spin in a tornado but not from one side of the screen to the other
This: transform.Rotate (0,0,50*Time.deltaTime); I messed around with the X,Y and Z properties but they pretty much all made a tornado type effect or rotated in 3d so disappeared when at 180 degrees.
I also imported a spritesheet where they are at different points in the "spin" so say at 10,20,30...360 degrees etc. but this wans't smooth at all
I hope this makes sense. I've spend quite a few hours on it now!! I can't get my head around it as I've moved from Xcode where this sort of stuff is 1 line of code. A point in the right direction would be amazing.
Note: if my question makes no sense please ask!

have you tried transform.rotation = Quaternion.lerp() ? I used this myself to rotate an object using an input though you could use it to do a random rotation on the z plane easily enough

Related

Creating turning animation for up and down or rubiks cube

im creating a rubiks cube solver and simulator for my a level project and i have come across the problem of the top face of the cube not rotating properly. After a few tests of it i have come to the conclusion that the top face is rotating around the axis properly but in order for it to stay in line witht eh rest of the cube i also need to update the other axis. This is my code to rotate the face
for (int i = 0; i < 90 * n; i++)
{
cube[0].rotateY(degree); // degree is defined as Math.PI / 180 as its in radians
cube[3].rotateY(degree);
cube[6].rotateY(degree);
cube[9].rotateY(degree);
cube[12].rotateY(degree);
cube[14].rotateY(degree);
cube[17].rotateY(degree);
cube[20].rotateY(degree);
cube[23].rotateY(degree);
Invalidate();
}
I have tried also rotating the left and right side and they both work correctly while using the same function but for the other axis. i have definitions for the entire cubes rotation stored in rotation with x y and z properties. Initially i thought this wouldnt be a very hard fix so as well as rotating each cube on the y axis i rotated them along the x axis but for -rotation.x * 2 in order to counteract the problem however this didnt work and just transformed it into very weird positions. Im sure its just a simple maths expresion that i need to rotate it by but i cant work it out.
As i found it hard to explain my problem ive created this video to show it visually. You can see that the left and right rotations work although they are not yet animated and the top face rotates round and always snaps back into the original position but not the other 4 correctly. I am also aware that the colours dont come round the cube like they should but this is something i will work on once all the rotations work
Video of current coded soloution
the functions for the rotations simply update the rotation variable and redraws the cube and all the different cube's in the array are just there as i have created 26 smaller cubes to create 1 bigger cube so that rotations can work without just changing the colour.
If anyone has a nice maths equation to rotate this correctly that would be great but if not and you have another way of rotating the cubes in the cube then that would also be great.
Also just a side quest if anyone knows why my code doesnt rotate the side by 1 degree and then redraw it again inside the Invalidate command then id love to hear why.
Many thanks in advance.

(Unity) Issue with rotation when transform.up is aligning to a direction

I am testing out some ideas in Unity where a player can walk around a circle while staying on it (so the circle has its own gravity) and also being oriented properly. This game is currently being done in 2D, so all objects are sprites.
I do hope I can explain myself properly. Please ask if you need any further clarification...
It appeared that I succeeded with my idea until I noticed something odd.
So as expected, the player moves around the circle without falling off (custom gravity worked just fine) and its Z rotation is affected as it aligns itself with a direction:
// Align code:
// We reverse the direction so the object is standing up the right way.
private void Update()
{
transform.up = -(planet.position - transform.position);
}
It works... mostly. However, when the player object's rotation Z naturally reaches 180, it appears to flip horizontally (like a mirror effect) and then it returns to normal as rotation Z leaves 180. The visual flip happens because for some reason the object's Y becomes 180 at the same time too. At no other point does X or Y change in regards to rotation. Only Z. So the moment Z hits 180, Y is affected and the moment we leave Z 180, Y returns to 0.
I'm happy to provide a quick video of it happening in-game if anybody needs some visual understanding of what's going on.
The visibility of this bug tends to rely on how fast you're moving around the circle. If you're moving fast enough, you can probably skip over 180 and not see it happen at all, however if you move slow enough there's no denying it's there. It's also problematic for the fact that I simply make the camera a child of the player so when the player flips, so does the camera causing the entire scene to flip which can look extremely glitchy for a player to see.
I really have no idea how to tackle this issue as I have no clue why it would do such a thing. At every other rotation value it behaves just fine. It's only at Z = 180 (so the object is exactly upside down) does it decide to rotate in the wrong ways.
EDIT: Changed tag to Unity3D
It's probably because of converting quaternions (which is what's actually used internally for rotations) to euler angles for display. Can you try to use Quaternion.Slerp to rotate?

Draw travel distance in XNA 4

So basically i am creating an XNA game at college and i need some help with something as i cannot seem to figure it out myself and i'm pretty new to this.
Basically i have a spaceship with a scrolling background of stars. I have falling asteroids and basically the point of my game is to travel as far as possible without being hit by said asteroids.
I'm really looking for some guidance as to how i could measure a theoretical distance travelled by the ship and then draw it on screen?
Any help would be greatly appreciated.
Many thanks.
Solution A
Somewhere in your code you are defining the offset of the backdrop for each frame. You could just invert* this value and add it to the total amount every frame:
totalDistance += -backdropOffset;
If the offset is defined in pixels you have to convert it to your game world unit (kilometers, lightyears, ...) before displaying the distance.
* If the ship moves forward, the backdrop "slides" in the other direction.
Solution B (more work but less headaches)
It is actually not the backdrop that is moving; it's the ship. So why not move the ship and follow it with the camera?
You will be able to do all kinds of motion. Right now you have to invert every movement of the ship and then apply it to the backdrop. Kind of counter-intuitive, don't you think? So going with this solution your code will be much closer to reality => less headaches during debugging, easier maintenance of your application and you will be quicker when adding new features.
And of course, getting the total distance would be as trivial as
var totalDistance = myShip.Position.Y;

Rotating a ship 360*

In game Im trying to make, I have some ships(not space ships or so, actual ships they are in water)
If I just directly rotate them, I get absurd results.
Do I need to make 8 picture for each ship ? (considering there is 8 direction)
Are there any way that I can do it with just creating one image or at least a few, instead of 8 ?
Essentially, rotation mathematics are an interpretation of the original image.
Sure, it works depending on the complexity of the image and the relationship of straightlines and things that are perpendicular, but some things just dont work.
If you're doing a top-down 2D game with ships, I'm going to assume Sail ships here, then rotating mathematically really just isn't going to look good as the sails them selves will move and angle depending on Wind speed/direction and the angle of the ship.
Long story short ? Mathematical rotation works well for an Asteroids style triangle ship, doesn't work well for proper graphics.
Hope this helps!
If you are talking 2D graphics and are getting "absurd results" I'm assuming you're not taking into account an origin. If you have a Texture2D and give it a rotation value, it will be rotating by the default origin which is (0,0). Try setting your origin in your spritebatch.Draw call to a new Vector2(texture.width / 2, texture.height / 2) and see if that is a step in the right direction.
Another approach would be to have a spritesheet with the 8 drawings that you mention and reference a different source rectangle of the texture2D.

2D Platformer AABB collision problems

I have a problem with AABB collision resolution.
I resolve AABB intersection by resolving the X axis first, then the Y axis.
This is done to prevent this bug: http://i.stack.imgur.com/NLg4j.png
The current method works fine when an object moves into the player and the player has to be pushed horizontally. As you can see in the .gif, the horizontal spikes push the player correctly.
When the vertical spikes move into the player, however, the X axis is still resolved first. This makes "using the spikes as a lift" impossible.
When the player moves into the vertical spikes (affected by gravity, falls into them), he's pushed on the Y axis, because there was no overlap on the X axis to begin with.
Something I tried was the method described in the first answer of this link.
However the spikes and moving objects move by having their position changed, not velocity, and I don't calculate their next predicted position until their Update() method is called.
Needless to say this solution didn't work either. :(
I need to solve AABB collision in a way that both of the cases described above work as intended.
This is my current collision source code: http://pastebin.com/MiCi3nA1
I'd be really grateful if someone could look into this, since this bug has been present in the engine all the way back from the beginning, and I've been struggling to find a good solution, without any success. This is seriously making me spend nights looking at the collision code and preventing me from getting to the "fun part" and coding the game logic :(
I tried implementing the same collision system as in the XNA AppHub platformer demo (by copy-pasting most of the stuff). However the "jumping" bug occurs in my game, while it doesn't occur in the AppHub demo.
[ jumping bug: http://i.stack.imgur.com/NLg4j.png ]
To jump I check if the player is "onGround", then add -5 to Velocity.Y.
Since the player's Velocity.X is higher than Velocity.Y (refer to the fourth panel in the diagram), onGround is set to true when it shouldn't be, and thus lets the player jump in mid-air.
I believe this doesn't happen in the AppHub demo because the player's Velocity.X will never be higher than Velocity.Y, but I may be mistaken.
I solved this before by resolving on the X axis first, then on the Y axis. But that screws up the collision with the spikes as I stated above.
Why not resolve on the Y-axis first for vertical spikes, and on the X-axis first for horizontal spikes?
Nice graphic, by the way.
As I understand it, you're handling movement and collision something like this:
Move all objects.
For each object O, test for intersection between the player and O, and if necessary, eject the player horizontally or vertically so that it is no longer intersecting with O.
If the player is still intersecting with some object, then (something).
This means that when you come to step (2), you have forgotten which way the object O was moving, so you can't tell if it is trying to push the player upwards or sideways.
Solution: in step (1), store for each object the direction it is moving. When you find the player intersecting with an object, you can look to see whether the object is moving up, down, left or right, and that will tell you which way to perform the ejection.
As Gareth Rees already said, the issue is after a collision is detected, you need more information (both current location and either direction came from or last position) to perform the collision response.
It gets quite complicated if both objects are moving. Instead, choose one object to be the frame of reference and subtract its velocity from everything else.
A straight forward solution might be to create line segments for the movement/delta of the non-frame-of-reference object. Then intersect those segments with the 4 AABB edges. This gives the time of intersection and the normal at the point of intersection. Then you can apply the same response you have now.
One possible solution I found is sorting the objects before resolving based on the velocity of the player.

Categories