Unity Rotation Issue - Character Feet look towards movement direction - c#

My question seems simple but I am also very simple and cannot figure this out. The thing is, I´ve got an IK system for the Character's legs. I need the character's feet to "look" in the movement direction.
If I do this: rightLegCurrentRotation = Quaternion.Euler(-transform.up);
The Character´s feet "look" flat down, just as I want, however, if I use the movement direction and do anything but fall down (Since that gives me the same value as -transform.up) the feet´s rotation completely goes wild and makes no sense.
Not sure if I´m explaining myself but essentially what I need is the player's feet to lay flat towards the given direction, kind of like this.
Please let me know if I don´t make sense, quaternions are a real pain and I hardly understand what I´m doing. I am in dire need of help.

Related

Adding Smooth Rotation To 2D Character with only vertical movement

If you've seen the picture my space ship is stuck on on place and can only go up and ddown, now that's not the problem the problem is that while it goes up or down, it looks really bland and i want to add some smooth rotation to that, but please I do not know what mathf lerps and slerps are, and they are confusing everyone keeps suggesting only that so if you have a different answer please tell me that!
but as always anything is appreciatable than nothing!
I have tried the Mathf Lerps and Slerps and can't seem to understand where to add them and what variables i should create just to make it work!, so if you have an answer please suggest it!

How can i make a rigidbody speed up everytime it collides with a wall at an angle?

I am trying to make a game where i got a ball with an arrow rotating around it. The ball starts as immovable, but when i hit a button i launch it to the direction the arrow is pointing at. Player will have the option mid air to press another button to make the ball "sticky" so when it hits a wall it will stick to the surface and repeat the process till he gets to the top of the level.
Aside from that, i want to give the option to the player to not stick to the wall if he doesn't press that button and instead bounce off the wall but when he does that, the player should speed up with every bounce, giving the option to either play it safe and slow or try to go fast getting more points as he does.
For the early prototyping i used force to move the player up every time he launches but i am not sure how i can make him speed up every time he bounces off the wall. It feels to me like a math problem more than it is a coding challenge. What i am thinking is that i have to find the angle on which player hits the wall, and add force according to that towards the direction he is supposed to follow after the collision.
Sadly i am not that good with trigonometry (working on it though). I am thinking that i might need to use a formula containing sin, cos, tan formulas but i m not sure how to do it. Any help is much appriciated! If you need more information on it please tell me and i ll be happy to provide.
Edit: After the first reply to this question i also found out those links that dive deeper into the subject. I m gona link them here for people that have the same issue.
Bouncing a ball off a wall with arbitrary angle?
http://www.3dkingdoms.com/weekly/weekly.php?a=2
If your ball has velocity vector V=(vx,vy), then after bouncing from standing surface with normal N, ball's new velocity is
V' = V - 2 * N * (V.dot.N)
where dot is scalar product of vectors (vx*nx+vy*ny)
Particular cases:
bouncing from vertical wall (N=(+-1, 0)) causes reversing of vx-component, vy component remains the same. V' = (-vx, vy)
bouncing from horizontal wall (N=(0, +-1)) causes reversing of vy-component, vx component remains the same. V' = (vx, -vy)
Note I recommend to work in velocity vector components, and use angles only when they are really needed.
If you need to calculate bouncing from moving bat, it is worth to change stationary coordinate system to moving one, connected with the bat, find reflection in that system, and revert to stationary system.
Ok so problem was solved!
I want to thank both #MBo and #shingo for their contribution to the solution. While the answer of Mbo solved the problem via trigonometry and gave me nice material to study and figure out how things work, i followed shingo's advice in the comments of my question and managed to do it without diving deep into math.
So basically what he said, and what i did, was to use Unity's physics engine and let the ball hit the wall. After the ball bounces off the wall, it gets a new Velocity vector towards the direction it would go based on physics. I then created an OnColllisionExit check, and when the ball stops colliding with the wall, i AddForce to it to the direction of its new Velocity Vector3.
Works like a charm!
Thank you all for your contributions!

Is it possible to move 2D character at a specific angle?

I have started developing 2D isometric game in Unity, but i'm struggling with the movement. I want to move my character at a specific angle. To be more specific with you and give you better idea what i like to achieve is to create movement like in "Bouncy Bits"
Here is a video of their gameplay: https://www.youtube.com/watch?v=A6xA4RzuRcM&t=19s
I would love to achieve that kind of moving to my player including him to move forward alone in that same angle without me pressing any keys.
Would really appreciate if someone could help me out, by giving me a code or explain me what i exactly need to do.
Move your character on vector.forward, which is equivalent of (0, 0, 1) on the Z axis. Put your camera on that front angle how they have it. It would look the same.

Aiming towards thumbstick XNA

currently having some fun in XNA programming a topdown shooter towards the XBox-controller but I'm having some issues right now.
So I move my character with the left thumbstick and aim with my right, so essentially I can move in any direction and still shoot in 360 angle. What I have right now works, but it's very sensitive and "tacky", it likes to stick a bit extra on all the 90 angles before it moves on to the next angle if I'm moving the thumbstick in a perfect outer circle.
So this is what I have right now:
direction.X = gpState_new.ThumbSticks.Right.X;
direction.Y = gpState_new.ThumbSticks.Right.Y;
rotation = Math.Atan2(direction.Y, direction.X);
And then when I draw the player I use rotation as the angle of which I'm drawing it.
Do you got any tips on how to do this better ?
Fredrik
It looks like this is because of the "deadzone" on the stick. The basic fix is probably something like is to use GamePadDeadZone.Circular. Perhaps before your code, something like
GamePadState gpSTate_new = GamePad.GetState(playerIndex, GamePadDeadZone.Circular);
A very similar question was asked on GameDev (and you can see a more detailed answer there), which you might have better luck on with this kinds of questions in the future.

C#: How can I tell which side a collision has occured on?

I think the title is rather self explanatory but just to clarify I am trying to figure out how to tell which side the collision has occured on.
For a bit more detail, I'm trying to make a maze-like game so I can't simply stop all movement upon a collision. Instead I need to be able to tell which side the collision has happened on so I can block that direction.
Any help is appreciated and if there is a better approach to this issue, I'm all for trying it out.
I hope this is enough details but if you need anymore, ask and I'll edit. Thanks in advance.
[edit]
#viggity - No, I'm not using any specific game engine and I would post the current "detection" code but it's a little, absurdly, robust.
#Streklin - I'm using the this.Paint event to draw onto the form itself as it was recommended I start by doing that to get better at drawing real time. I'm also using a location that's updated each time the timer ticks based on what I press (left, right, up, down). Yes the maze is tile based. Currently it only consists of 3 colors even. I'm not a very advanced programmer.
#Eric - Definately a one-d game. Again, I only have 3 colors, the lines are black, the background is white and the square (the user) is green. I'm using the DrawImage() with Bitmaps to draw onto the screen.
[edit psuedo-code summary]
foreach(Wall _wall in walls)
if(player.intersectsWith(_wall))
stop movement;
#JeffH - I'm not really sure what you're asking as that's pretty much all there is besides testing code that I was using to try and get it working. The only thing I left out was the if statement to check if it was the x axis or not so that x and y could move indepedently from each other. So instead of getting "stuck" because you touched the wall, you could slide against it. I didn't see the point in including that though since the problem occurs before that.
Assuming you're talking about a 3D game here.
The normal of the face you can see points towards you, so the dot product of your direction vector with the face normal will be negative. If it's positive then you are coming at the face from the back.
If it's zero you're travelling at right angles to the face.
| <---------- your direction of travel
|
|----------> <- face normal
|
| <- face
If you're not in 3D then you could store the direction the wall is facing (as a 2D vector) and do the same dot product with your 2D direction of movement.
Based on your edit you can only go one direction at a time? Or can you go in diagonal directions? If it's the later, ChrisF has provided you the answer in 3D and the corresponding information for 2D. If not, you should just have to stop travel in the direction of travel - since there are only four possibilities it should be easy enough to check them all for simple starter game.

Categories

Resources