Unity returning incorrect transform.rotation - unity3d

In the endless running game I am creating, there is a mechanic called the jump pad. It basically takes in the change in the player's position since the last frame and converts that to an angle. Then it uses that along with the transform.rotation of the jump pad to figure out which direction to move the player in, as if it was actually bouncing off of it.
This algorithm would've been working perfectly except for one problem: Unity was reading an incorrect transform.rotation. When the jump pad was rotated 45 degrees, it read 21.92615 (after being multiplied with Mathf.Rad2Deg.) When the jump pad was rotated 20 degrees, it read 9.949307.
At first I thought it was being multiplied by an unknown constant for some reason, but after checking it through the Calculator app I found this to be not exactly the case. After a bit more experimenting I found that the possible "unknown constants", while close enough together, did seem to have a range from around 2 to around 2.1. And while I don't have the faintest clue how this is happening, it does seem to be remarkably consistent.
So my question is: what is this problem I am facing and is there any way I can fix it? Brief side note: I know this is not some other problem with the code because I stripped it down to the bare minimum:
public void OnTriggerEnter2D(Collider2D collision){
Debug.Log(collision.gameObject.transform.rotation.z * Mathf.Rad2Deg);
}

OK. After looking up the scripting API for transform.rotation and messing around a bit, I discovered that it was returning the z value of a quaternion and fixed it using the .ToEulerAngles() call.

Related

Forward facing vector is a bit offset

I am making an FPS game and I need for the forward facing vector to hit exactly where the crosshair is aiming.
This is my current blueprint.
It takes all references from the camera position where the players head should be. (as it usually is in FPS games).
When "shooting" the vector it is slightly offset though. (pink dot near crosshair)
Things I have tried:
Increasing distance of vector makes the problem go away but it then becomes inconsistent, which means it's a bad solution to the problem :(
Manually changing axis values, but that was also very inconsistent.
Changing between 3 different nodes of taking rotation from the camera, they all (didn't) work the same way :/
Maybe there is an issue of the values that I am taking, although the starting position of the camera seems to be correct.
Thank you for any insight you may have!
Suggested by user Ruzihm the issue was that the crosshair was off-center. My blueprints were actually okay.
So for anyone looking see if your crosshair is in the center.

Unity3D forcing object to a given orientation with minimum artificial effect

In my board game, the points are given by throwing 7 sea-shells cowry shell. These shells are dropped onto a sphere in Unity so they get rolled over randomly to different places. Once the rigidbody.isSleeping() returns true, I do a Raycast(from the belly side downwards) to figure out the orientation of the shell. If it is NOT a hit we know the shells belly is turned upside which means a point.
All is good and very realistic when in single player mode. Reason is I just activate the gravity of the shells and they dropped on to sphere, gets rolled randomly and when stopped i get the marks as stated above.
Now the problem is I am making the game multiplayer. In this case, I sent the randomly generated marks from the server and client will have to animate the shells to represent the marks. For example, if server send 3, out of 7 shells, 3 should have it's belly turned upside.
Trying to do this has been a major problem for me. I tried to transform.Rotate() when the velocity is reduced but it was not very reliable and sometimes acts crazy. Rotating afterrigidbody.isSleeping() works but very unrealistic.
I know I am trying to defy physics here, but there may be some ways to achieve what I want with minimum artificial effect.
I actually need some ideas.
Update - 1
After infor I receive below, I did found some information here, some advanced stuff here. Since the latter link had some advanced stuff, I wanted to start small. So I followed the first link and did below test.
I recorded the position, rotation & velocity of the sea shell with autosimulation enabled and logged them to a file. Then i used the Physics.Simulate() for the same scenario and logged the same.
Comparing the two tells me that data in both cases are kind of similar. So seems like for my requirements I need to simulate the sea-shell drop and then apply that sequence to the actual object.
Now my problem is how can I apply the results of physics.simulate() results (position, rotation, velocity etc..) to the actual sea-shell so the animation can be seen. If I set the positions to my gameobject within the simulation loop nothing happens.
public void Simulate()
{
rbdy = GetComponent<Rigidbody>();
rbdy.AddForce(new Vector3(0f, 0f, 10f));
rbdy.useGravity = true;
rbdy.mass = 1f;
//Simulate where it will be in 5 seconds
int i = 0;
while (simulateTime >= Time.fixedDeltaTime)
{
simulateTime -= Time.fixedDeltaTime;
Debug.Log($"position: {rbdy.position.ToString()} rotation: {rbdy.rotation.ToString()} Velocity {rbdy.velocity.magnitude}");
gameObject.transform.position = rbdy.position;
Physics.Simulate(Time.fixedDeltaTime);
}
}
So, how can I get this simulated data applied to actual gameobject in the scene?
Assume Physics are deterministic, just set the velocity and position and let it simulate on each client. Output should be the same. If the output differs slighly, you could adjust it and it may be only barely noticable.
Physics.simulate may be interesting to read, even if it's kind of the opposite of what you want.
You can throw in the client, record the steps in realtime or using physics.simulate (see point 2) and transmit the animation data as binary - then use it in the other clients to play the animation.

Unity stop character controller jumping up mountains

as most of you might know, the default character controller settings in unity allow you to simply jump up any mountain / hill, even if its more than the slope limit. Granted you can't WALK up it, but you can still jump up it while moving forwards.
In an attempt to fix this, I used the code from another answer:
void OnControllerColliderHit(ControllerColliderHit hit)
{
float radian = cont.slopeLimit * Mathf.Deg2Rad;
if (hit.normal.y <= radian)
{
canJump = false;
} else
{
canJump = true;
}
}
and in my update function I only jump if canJump is true (as you might have guessed)
So it almost works as expected, when the player approaches slopes that are beyond the slope limit (I THINK at least, if my radians calculation was correct, 'cause I don't really get what hit.normal.y is, if that's the same as the slope limit or not, so I kind of guessed... if anyone can correct me on that, that would help.)
Anyway, first of all I need to fix that fact that the character can't jump even when he's facing AWAY from the mountain, I don't know how to calculate if he's FACING the steep slope or not, only if he's colliding with it, so if anyone can help with that, that would be good, and (related):
I also can't figure out how to stop the player from jumping on these steep slopes from far away and staying on them, if he jumps on them or ends up on them however, he should simply slide down the mountain until he's on a regular slope, but I have no idea ow to get the CharacterContrller to slide down things, and especially not only until he reaches a regular slope?
Also it seems like this also applies (the fact taht he can't jump) to when the character is walking up stairs sometimes, but that's a big no-no 'cause he should always be able to jump up stairs if they're reasonably small enough, but I don't know how to differentiate between a terrain object and stairs...
If anyone can at least answer one of these three issues, that would be great (but prefereably all of them 'cause it's all part of the same question: How to just get normal character sliding / jumping regarding slpoes and stuff! (even though that wasn't the official title)

How to smooth movement in Unity 4.3 2D with AddForce?

at the moment i'm working on a 2D plattformer and the work is going very well. But there is one Problem i can't get rid of.
The player can use a dash, which should move the player very fast in the direction he is looking. My problem is, that the player game object instant appears at the target location... like a teleport.
I'm using the AddForce Function to move the game object. On jumping i'm using AddForce as well and there it works really nice, i get a smooth jump movement.
The only different between dash and jump is that on jump i apply force to the y axis and on dash to the x axis. Changing the amount of force doesn't affect the movement only the distance.
Does anyone have an idea what i'm doing wrong?
// Dash
rigidbody2D.AddForce (new Vector2((dashSpeed * direction), 0));
// Jump
rigidbody2D.AddForce (new Vector2(0, jumpForce));
Best,
Verdemis :)
EDIT: I can not access my project at the moment, but i will try to show you what i have done.
Note: dashSpeed is a float value, at the moment something like 3500
and direction contains 1 or -1, depending on the direction the player is looking. The Dash code is part of the Update method.
// Dash
if(Input.GetKeyDown(dashKey))
rigidbody2D.AddForce (new Vector2((dashSpeed * direction), 0));
What is your direction vector, is it normalized? Since multiplying non-normalized vectors can be rather hazardous. Do you have more of the script to show?
EDIT: You should always do physics things in the FixedUpdate loop(link).
Ok i could solved the problem. My problem was that i only did this AddForce only once. The AddForce code was only executed in a single frame. I added a time which i count down to define how long the dash movement gonna be!
The problem may be that you are using a very big force, I was messing around with some physics today and realized that even a force of 100 almost looks instant. Try making a smaller force number to see if that helps you. I just tested making a smaller number and that does not work.

Need Unity character controller to make sharp 90 degree turns and not slide when turning

I'm using the first person controller for my characters movement. On a left arrow keypress, I'd like the character to instantly rotate 90 degrees and keep moving forward. Currently, when I hit the arrow key, the character makes the sharp 90 degree turn, but the forward momentum the character previously had takes a second to wear off so the character ends up sliding in the direction he was previously moving a short bit.
The closest example I can think of to visually explain what I'm trying to do is how the character turns sharp in Temple Run. How my game is currently working, if I had the character on a ledge make a sharp left turn, he'd likely keep the original momentum and slide off the edge right after he turns.
Since my character is running on the x/z axis, I'm wondering if there would just be some way to maybe swap the directional velocity/momentum? The speed the character had on the x axis would instantly be switched to the z when it turns and the other would be set to zero. I'm obviously open to any solution that accomplishes what I'm looking for.
I dug into the CharacterMotor class in the first person controller, but have yet to find what part I can tweak to accomplish this.
I'd greatly appreciate any help.
Thank you.
You can try to stop the velocity of the Rigidbody before turning.
this.rigidbody.velocity = Vector3.zero;
this.rigidbody.angularVelocity = Vector3.zero;
If you want the object to continue like it did, you can try playing around with it by saving the current velocity in a variable, setting it to 0, rotate it and then putting back the old velocity (still forward).
If it works with global vectors (so from the point of view of the world, not the object), then you can try negativing the velocity, actually causing it to go 'backwards'. I can't test it for now but either way I think you need to set the velocity to zero first before turning the character.