Rotating a 3D character on when they move - unity3d

I want to start with a little 3D platformer on Unity. When I move, I want the character looking to the moving direction. So when I press Left/"A" I want the character instantly turning left and walking forward. Same for the other directions. The problem is that the character turns back to the default rotation when I leave the key.
The important code:
private void FixedUpdate()
{
float inputX = Input.GetAxis("Horizontal"); // Input
float inputZ = Input.GetAxis("Vertical"); // Input
if (GroundCheck()) // On the ground?
{
verticalVelocity = -gravity * Time.deltaTime; // velocity on y-axis
if (Input.GetButtonDown("Jump")) // Jump Key pressed
{
verticalVelocity = jumpPower; // jump in the air
}
}
else // Player is not grounded
{
verticalVelocity -= gravity * Time.deltaTime; // Get back to the ground
}
Vector3 movement = new Vector3(inputX, verticalVelocity, inputZ); // the movement vector
if (movement.magnitude != 0) // Input given?
{
transform.rotation = Quaternion.LookRotation(new Vector3(movement.x, 0, movement.z)); // Rotate the Player to the moving direction
}
rigid.velocity = movement * movementSpeed; // Move the character
}
The second thing is, at
transform.rotation = Quaternion.LookRotation(new Vector3(movement.x, 0, movement.z));
there is 0 on the y-axis. It says the viewing Vector is zero. When I pass in another number like movement.y the character tilts to the floor. So I do not know what to pass in there.

As for resetting when you let go of the key: your line
if (movement.magnitude != 0) // Input given?
is a good idea, but there's a good chance your controller is reporting slightly off of 0, so your character's direction will change even when you're not actually moving. I would change this to
if (movement.magnitude >.1f) // Input given?
or some other number close to (but not exactly) zero. While working on this, I would add Debug.Log(movement.magnitude); to this function and make sure that the values are in the range you expect.
On the second topic:
while it is important to have verticalVelocity in your movement vector for when you apply it to rigidBody.velocity, you don't want it in your character facing vector. If you want your character to only look in a single plane, it makes perfect sense to only give it two dimensions to consider; adding a third dimension would make it look at the sky or the ground as you mentioned. Furthermore, I would change your input-checking line to use this as well, because you only want to change facing based on whether the character is moving horizontally or not. This would make your code look something like this:
Vector3 movement = new Vector3(inputX, verticalVelocity, inputZ); // the movement vector
Vector3 horizontalMovement = new Vector3(inputX, 0f, inputZ);
if (horizontalMovement.magnitude != 0) // Input given?
{
transform.rotation = Quaternion.LookRotation(horizontalMovement); // Rotate the Player to the moving direction
}
rigid.velocity = movement * movementSpeed; // Move the character
And one final note, when you are grounded, you might want to set verticalVelocity to 0, rather than -gravity*deltaTime. This error may not be visible (the physics engine will push your character back up out of the floor), but if the user alt-tabs and there's too long between frames, your character will teleport through the floor!
Good luck.

Related

Unity 3D. How to find out where the movement of an object is directed relative to its rotation?

I need to make a 3d top down character controller and animate it.
To begin with, I made a code for the character's movement relative to the camera. And this code works, the character walks and turns well.
Vector3 inputDirection = new Vector3(_input.GetAxis.x, 0, _input.GetAxis.y);
float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + _camera.rotation.eulerAngles.y;
float angle = Mathf.SmoothDampAngle(transformPlayer.eulerAngles.y, targetAngle, ref _turnVelocity, TurnSmoothTime);
transformPlayer.rotation = Quaternion.Euler(0f, angle, 0f);
Vector3 moveDirection = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
moveDirectionNormalized = moveDirection.normalized;
_characterPlayer.Move(moveDirectionNormalized * SpeedMultiplier * Time.deltaTime);
The character model is a child object of _characterPlayer.
And if, when the character moves, an enemy gets into his radius, then the player's model will turn towards the enemy, and the character himself will go further along the moveDirectionNormalized.
For turns, I wrote the following code:
if (NearestEnemyAtAttackRadius != null)
{
Quaternion rotation = Quaternion.LookRotation(NearestEnemyAtAttackRadius.transform.position - MeshCharacter.position);
MeshCharacter.rotation = Quaternion.RotateTowards(MeshCharacter.rotation, rotation, 800f * Time.deltaTime);
}
else
{
MeshCharacter.localRotation = Quaternion.RotateTowards(MeshCharacter.localRotation, Quaternion.Euler(Vector3.zero), 800f * Time.deltaTime);
}
Now if there is an enemy nearby, my character swings towards the enemy, and if there is no enemy or the enemy has left the radius, then the initial state is returned.
And here my problem begins. I wanted to add an animation of the movement. The character has 4 animations: movement with a tilt to the right, left, forward and backward.
In the Animator, I made a Blend Tree (2d simple direction) with 4 animations. Added 2 Float values MoveDirectionX, MoveDirectionY correctly configured for all motion pos x and pos y.
And if I am in MoveDirectionX, MoveDirectionY will feed the vector moveDirectionNormalized, then it does not work correctly. The values of moveDirectionNormalized do not depend on the repetition of my model in any way, and if my character moves away from the enemy, he looks at the enemy but in fact goes backwards, the animation should turn on as he leans back. But now moveDirectionNormalized does not depend on the repetition of my model, and the character tilt animations randomly switch depending on moveDirectionNormalized. As I understand it, it is necessary to create a new vector that will take into account the rotation of my model, but I do not understand how to do this.
As a result, I want to get a vector where x - shows where the character is moving to the left or right (from -1 to 1) and y - shows the movement forward, backward (also from -1 to 1), and this vector should take into account the rotation of the player. At the moment, in the character model, only y changes in rotation. moveDirectionNormalized returns a value that indicates where the movement is directed relative to world space. And I need to make a vector that will return directions relative to the player's rotation. That is, if the character's gaze is directed at the enemy, and the character himself retreats from him, I would like to receive a vector (0, -1). And if the player's gaze is directed at the enemy and he goes to the right (I remind you that the player is constantly looking towards the enemy, that is, you can walk around the enemy and the character will constantly turn in his direction), then I would like to get a vector (1,0)
I am not 100% sure I understand, but if I do, this should help:
You can use Quaternion.Inverse() on the rotation of your player, and multiply that by the vector.
Vector3 yourVector;
Quaternion inverse = Quaternion.Inverse([player rotation]);
return inverse * yourVector;
Multiplying a quaternion by a vector rotates it with the quaternion, so if you invert it it will cancel out the rotation of the player.

Reverse movement done with transform.RotateAround()

I am making a third person mobile game with the help of joysticks. I have it set up where joystick.vertical moves the character forward or backwards depending on where he is looking and joystick.horizontal turns the character. Since the camera is parented to the character the camera always stays behind the character.
Swiping across the screen rotates the camera around the player with a touch panel using Camera.main.transform.RotateAround() function and the transform.LookAt() ensures I am looking at my character always.
My issue: I would like when the swipe is let go the camera to return to its original position behind the character but in a smooth motion or at a set speed moving around the player.
My Solution 1: To make an empty gameObject parented to the character and place it in the position where I want the camera to revert back to and call this position when the mouse is let go.
float spanCamera = -Joystick.Horizontal;
Camera.main.transform.LookAt(rb.transform.position);
if (spanCamera != 0)
Camera.main.transform.RotateAround(rb.position, Vector3.up, spanCamera * Time.fixedDeltaTime * spanSpeed);
else if (Input.touchCount <= 1)
{
float var6 = var5 * Time.deltaTime;
Camera.main.transform.position = camPos.transform.position;
Camera.main.transform.LookAt(camLookAt.transform.position);
}
This piece of code moves the camera back to the start position immediately without smoothing.
Solution 2: use a Vector3.MoveTowards() instead
// Camera.main.transform.position = camPos.transform.position;
Camera.main.transform.position = Vector3.MoveTowards(Camera.main.transform.position, camPos.transform.position, var6);
This code allows me to smoothly move to the start position in a straight line. I want it to go around the player.
So I tried a different method where I assign a float value 1 if the camera turns right and check if the rotate button is let go and if the value is 1 within an if block.
public float axisDir;
...
else if (spanCamera == 0 && axisDir == 1)
{
Camera.main.transform.RotateAround(rb.position, Vector3.up, -1 * Time.fixedDeltaTime * spanSpeed);
}
But this results in an infinite spin because I do not know how to check if the desired position has been reached.
I hope someone can help. This is quite a long post. :(
You could have an dummy gameObject as a child of the player, and the camera as a child of the dummy.
That way, the camera is looking at the centre of the gameObject: where the player is. The camera would also rotate with the player, so it would always be behind the player.
Now, you know that when Mathf.Approximately(dummy.transform.localEulerAngles.y, 0.0f), the camera is behind the player. [1][2]
Knowing that, you can check when the player stopped swiping and start slowly rotating it back.
// If the player let go and camera is not behind the player
if (!playerIsSwiping && !Mathf.Approximately(dummy.transform.localEulerAngles.y, 0.0f))
{
// Slowly rotate until the camera is behind the player
dummy.transform.RotateAround(Vector3.zero, Vector3.up, rotationSpeed * Time.deltaTime);
}
try this buddy :
// Maximum turn rate in degrees per second.
public float turningRate = 30f;
// Rotation we should blend towards.
private Quaternion _targetRotation = Quaternion.identity;
// Call this when you want to turn the object smoothly.
public void SetBlendedEulerAngles(Vector3 angles)
{
_targetRotation = Quaternion.Euler(angles);
}
private void Update()
{
// Turn towards our target rotation.
transform.rotation = Quaternion.RotateTowards(transform.rotation, _targetRotation, turningRate * Time.deltaTime);
}
i found it here
from my own experience using LERP works quite well, you lookin for a smooth transition, or between two points, known as interpolation, in this case linear interpolation.

How to move an object by a certain angle over a period of time in a circle [duplicate]

I a new here and i try to start working with Unity Engine.
Could somebody explain me, how works Quaternion.Slerp? Because I want to rotate some object in different angles 90, 180 and 270. My code you can see below. Unfortunately when I add 180 degrees, object make crazy things and than put rotation to (0, 180, 180) for this game object. I would like to get (180,0,0)
public float speed = 0.1F;
private float rotation_x;
void Update()
{
if (Input.GetButtonDown("Fire1"))
{
rotation_x = transform.rotation.eulerAngles.x;
rotation_x += 180;
}
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(rotation_x, transform.eulerAngles.y, transform.eulerAngles.z), Time.time * speed);
}
Most examples out there including Unity examples from their official website are using Lerp in the wrong way. They didn't even bother to describe how it works in the API documentation. They just starch it in the Update() function and call it a day.
Mathf.Lerp, Vector3.Lerp, and Quaternion.Slerp work by changing from one position/rotation to another with the t value(last parameter) being passed in.That t value is also know as time.
The min of the t value is 0f and the max is 1f.
I will explain this with Mathf.Lerp to make it easier to understand. The Lerp functions are all the-same for both Mathf.Lerp, Vector and Quaternion.
Remember that Lerp takes two values and returns values between them. If we have a value of 1 and 10 and we do Lerp on them:
float x = Mathf.Lerp(1f, 10f, 0f); will return 1.
float x = Mathf.Lerp(1f, 10f, 0.5f); will return 5.5
float x = Mathf.Lerp(1f, 10f, 1f); will return 10
As you can see, the t(0) returns the min of the number passed in, t(1) returns the max value passed in and t(0.5) will return mid point between the min and the max value. You are doing it wrong when you pass any t value that is < 0 or > 1. That code in you Update() function is doing just that. Time.time will increase every second and will be > 1 in a second, so you have problems with that.
It recommended to use Lerp in another function/Coroutine instead of the Updated function.
Note:
Using Lerp has a bad side of it when it comes to rotation. Lerp does not know how to rotate Object with the shortest path. So bear that in mind. For example, you have an Object with 0,0,90 position. Lets say you want to move the rotation from that to 0,0,120 Lerp can sometimes rotate left instead of right to reach that new position which means it take longer to reach that distance.
Let's say we want to make the rotation (0,0,90) from whatever the current rotation is. The code below will change the rotation to 0,0,90 in 3 seconds.
ROTATION OVER TIME:
void Start()
{
Quaternion rotation2 = Quaternion.Euler(new Vector3(0, 0, 90));
StartCoroutine(rotateObject(objectToRotate, rotation2, 3f));
}
bool rotating = false;
public GameObject objectToRotate;
IEnumerator rotateObject(GameObject gameObjectToMove, Quaternion newRot, float duration)
{
if (rotating)
{
yield break;
}
rotating = true;
Quaternion currentRot = gameObjectToMove.transform.rotation;
float counter = 0;
while (counter < duration)
{
counter += Time.deltaTime;
gameObjectToMove.transform.rotation = Quaternion.Lerp(currentRot, newRot, counter / duration);
yield return null;
}
rotating = false;
}
INCREMENTAL ANGULAR ROTATION OVER TIME:
And to just rotate the Object to 90 in z axis, the code below is a great example of that. Please understand there is a difference between moving Object to new rotational point and just rotating it.
void Start()
{
StartCoroutine(rotateObject(objectToRotate, new Vector3(0, 0, 90), 3f));
}
bool rotating = false;
public GameObject objectToRotate;
IEnumerator rotateObject(GameObject gameObjectToMove, Vector3 eulerAngles, float duration)
{
if (rotating)
{
yield break;
}
rotating = true;
Vector3 newRot = gameObjectToMove.transform.eulerAngles + eulerAngles;
Vector3 currentRot = gameObjectToMove.transform.eulerAngles;
float counter = 0;
while (counter < duration)
{
counter += Time.deltaTime;
gameObjectToMove.transform.eulerAngles = Vector3.Lerp(currentRot, newRot, counter / duration);
yield return null;
}
rotating = false;
}
All my examples are based on frame-rate of the device. You can use real-time by replacing Time.deltaTime with Time.delta but more calculation is required.
Before anything, you can't add 180 on euler angles like that, and that's mainly what is causing your problem. You'd better use quaternion directly instead, or work on the transform itself.
You can think of a quaternion as an orientation in space. In contrary to what have been said, I do recommend learning how to use them if you can. However, I don't recommend using euler angles at all... as they're suject to different writing conventions, and will fail sometimes. You can look at 'gimbal lock' if you want details about that.
Simply a slerp or lerp (standing for spherical linear interpolation, or linear interpolation respectively) is a way to interpolate (go from one orientation to another, by increasing t from 0 to 1, in a coroutine or anywhere else) between orientation A and B. The difference between the two is that the slerp is giving you the shortest path from A to B.
In the end, when t = 1, lerp(A,B,t) and slerp(A,B,t) will give you B.
In your case, if you want to instantly rotate an object in space to a specific orientation, I suggest you use Quaternion.AngleAxis which is the most forward way to describe mathematically a quaternion.
If you want to add a rotation, say 90° to you actual orientation (without animation between the two), you can do something like this :
transform.rotation *= Quaternion.AngleAxis(axis_of_rotation, angle)
or use transform.rotate (depending on the parameters, it can be a right multiply, or left : local, or world transform).
Programmers' answer is detailling how to animate your transform. But I do suggest you to investigate quaternion themselves, as it will give you global understanding of space transforms.

How to Calculate Target Destination

I'm having a bit of trouble figuring this one out. What I'm trying to achieve is a sort of tackling motion. The player lunges at the target from a distance.
The diagram shows the set up. The blue diamond is the player and the red thing is the target. The purple box is the renderer bounds of the targets SkinnedMeshRenderer. I'm using renderer bounds because some target's mesh are much larger than other. Currently, the player is shooting to the orange star...which is unrealistic. I want him to, no matter what way the target is facing, always target the closest point of the target relative to his position...in the diagram's case that would be the brown star. Here's the code I've been using...
public IEnumerator Blitz()
{
rigidbody.velocity = Vector3.zero; //ZERO OUT THE RIGIDBODY VELOCITY TO GET READY FOR THE BLITZ
SkinnedMeshRenderer image = target.GetComponentInChildren<SkinnedMeshRenderer>();
Vector3 position = image.renderer.bounds.center + image.renderer.bounds.extents;
position.y = target.transform.position.y;
while(Vector3.Distance(transform.position, position) > 0.5f)
{
transform.position = Vector3.Lerp(transform.position, position, Time.deltaTime * 10);
yield return null;
}
Results(); //IRRELEVANT TO THIS PROBLEM. THIS CALCULATES DAMAGE.
Blitz.Stop(); //THE PARTICLE EFFECT ASSOCIATED WITH THE BLITZ.
GetComponent<Animator>().SetBool(moveName, false); //TRANSITIONS OUT OF THE BLITZ ANIMATION
GetComponent<Input>().NotAttacking(); //LET'S THE INPUT SCRIPT KNOW THE PLAYER CAN HAVE CONTROL BACK.
}
//Get the derection to tarvel in and normalize it to length of 1
Vector3 Direction = (Target - transform.position).normalized
With Direction, you can do many things. For example, you can multiply the direction by your speed and add that to your position.
transform.position += Direction * MoveSpeed;
If you want to get to the closest point, you can use the handy Collider.ClosestPointOnBounds method.
Target = TargetObject.GetComponent<Collider>().ClosestPointOnBounds(transform.position)
and plug the Target into the code used to get the direction.
Alternatively, you can use Vector3.Lerp without getting a direction since it's just interpolating.
transform.position = Vector3.Lerp(Target,transform.position,time.DeltaTime);
For stopping at the target point, you can use the arrival behavior.
//Declare the distance to start slowing down
float ClosingDistance = Speed * 2;
//Get the distance to the target
float Distance = (Target - transform.position).magnitude;
//Check if the player needs to slow down
if (Distance < ClosingDistance)
{
//If you're closer than the ClosingDistance, move slower
transform.position += Direction * (MoveSpeed * Distance / ClosingDistance);
}
else{
//If not, move at normal speed
transform.position += Directino * MoveSpeed;
}

Sprite character slides back when near edge of other collider

I am making a 2D game in Unity. I have added a 2D box collider and a circle collider as trigger on my sprite character. The platform on which the character is standing also have a 2D box collider. So, when my character moves near edge of platform, it experiences a force or something that pulls it away from the edge. You can think it as a protective force that helps your character not falling down the plane but the problem is that this force is not part of game and that's why it should not be there. Following is the code I am using to move my character:
// call this method to move the player
void Move(float h)
{
// reduces character velocity to zero by applying force in opposite direction
if(h==0)
{
rigidBody.AddForce(new Vector2(-rigidBody.velocity.x * 20, 0.0f));
}
// controls velocity of character
if(rigidBody.velocity.x < -topSpeed || rigidBody.velocity.x > topSpeed)
{
return;
}
Vector2 movement = new Vector2 (h, 0.0f);
rigidBody.AddForce (movement * speed * Time.deltaTime);
}
Here is image of properties.
If I keep pushing the character it will fall off the edge but if I stop just by edge the unwanted protective force pulls it back on plane.
Also, if the character bumps into another 2D box collider, it bounces back instead of just falling down.
EDIT- Bouncing effect arises mostly when player bump into other objects while jumping. Code for jumping is
void Update()
{
// The player is grounded if a linecast to the groundcheck position hits anything on the ground layer.
grounded = Physics2D.Linecast(transform.position, groundCheck.position, 1 << LayerMask.NameToLayer("Ground"));
// If the jump button is pressed and the player is grounded then the player should jump.
if(Input.GetButtonDown("Jump") && grounded)
jump = true;
}
void Jump()
{
if (jump)
{
jump = false;
rigidBody.AddForce (Vector2.up * jumpSpeed * Time.deltaTime);
}
}
The problem lies in how you're implementing the "drag" force that slows your player down to zero. Because game physics happens in steps rather than continuously, your opposing force can overshoot and briefly cause the character to move in the opposite direction.
In this situation your best bet is to multiply your speed by some fraction each frame (likely between 0.8 and 0.95), or to use Mathf.Lerp to move your X velocity towards zero.
rigidbody.velocity += Vector2.right * rigidbody.velocity.x * -0.1f;
OR
Vector3 vel = rigidbody.velocity; //Can't modify rigidbody.velocity.x directly
vel.x = Mathf.Lerp(vel.x, 0, slowRate * Time.deltaTime);
rigidbody.velocity = vel;
Also give some thought to doing your movement/physics related code within FixedUpdate() instead of Update() and using Time.fixedDeltaTime instead of Time.deltaTime. This will allow for more consistent results independent of framerate.