Unity Setting Animator Parameters - unity3d

I'm trying to animate my player in Unity using the animator component but for some reason it seems that my character is stuck in the idle animation and I can't change the animation parameters from my code. I'm using a 2D freeform direction blend tree in unity, and I'm not exactly sure why it's not working. The animations themselves seem to be working fine when I change the parameters using the inspector.
`
void Update()
{
groundedPlayer = characterController.isGrounded;
if (groundedPlayer && playerVelocity.y < 0)
{
playerVelocity.y = 0f;
}
Vector2 input = moveAction.ReadValue<Vector2>();
Vector3 move = new Vector3(input.x, 0, input.y);
move = move.x * cameraTransform.right.normalized + move.z * cameraTransform.forward.normalized;
move.y = 0f;
animator.SetFloat(moveXAnimationParameterId, input.x);
animator.SetFloat(moveZAnimationParameterId, input.y);
characterController.Move(move * Time.deltaTime * playerSpeed);
if (jumpAction.triggered && groundedPlayer)
{
playerVelocity.y += Mathf.Sqrt(jumpHeight * -3.0f * gravityValue);
if (move != Vector3.zero)
{
animator.Play(runningJumpAnimation);
}
else
{
animator.Play(jumpAnimation);
}
}
playerVelocity.y += gravityValue * Time.deltaTime;
characterController.Move(playerVelocity * Time.deltaTime);
float targetAngle = cameraTransform.eulerAngles.y;
Quaternion targetRotation = Quaternion.Euler(0, targetAngle, 0);
transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, rotationSpeed * Time.deltaTime);
}

instead of using animator.Play I suggest you use conditions using for example :
_anim.SetBool("isRunning",true);

Related

Problem rotating an object on its local orientation

Hello i am new in the forum! I hope i am in the right section! Im trying to rotate a camera (that rapresent the player POV) using the mouse delta and im rotating the camera in local coordinates not world coordinates and i want avoid gimbal lock effect. I read somewhere on the internet that for that purpose i have to use quaternions, and i read how to do that. The problem is that axis rotations works well moving in local orientation but one of the axis is losing its local orientation and it rotate following the world coordinates orientation. I will post the code and i hope someone can help me and telling me where im doing things wrong. Thanks!
public class Player : MonoBehaviour {
[Header("Camera")]
[SerializeField] private Camera _camera;
[SerializeField] private Vector2 _xMinMaxRotation = new Vector2(-90, 90);
[SerializeField] private Vector2 _yMinMaxRotation = new Vector2(-90, 90);
[SerializeField] private float _mouseXSensistivity = 1;
[SerializeField] private float _mouseYSensistivity = 1;
[SerializeField] private float _mouseZSensistivity = 1;
[SerializeField] private float _xStartRotation = 0;
[SerializeField] private float _yStartRotation = 0;
private Vector2 _mouseDelta;
private float _rotY, _rotX, _rotZ;
//public GameObject head;
// Start is called before the first frame update
void Start() {
Cursor.lockState = CursorLockMode.Locked;
}
// Update is called once per frame
void Update() {
_mouseDelta = new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y"));
MoveCamera();
}
private void MoveCamera() {
_rotX += _mouseDelta.x * _mouseXSensistivity * Time.deltaTime * 100;
_rotX = Mathf.Clamp(_rotX, _xMinMaxRotation.x, _xMinMaxRotation.y);
_rotY += _mouseDelta.y * _mouseYSensistivity * Time.deltaTime * 100;
_rotY = Mathf.Clamp(_rotY, _yMinMaxRotation.x, _yMinMaxRotation.y);
//Calculation for RotZ
if (Input.GetKey(KeyCode.Q)) {
_rotZ += +_mouseZSensistivity * Time.deltaTime * 50;
if (_rotZ > 25) _rotZ = 25;
}
else {
if (_rotZ > 0) {
_rotZ -= 2 * _mouseZSensistivity * Time.deltaTime * 50;
if (_rotZ < 0) _rotZ = 0;
}
}
if (Input.GetKey(KeyCode.E)) {
_rotZ += -_mouseZSensistivity * Time.deltaTime * 50;
if (_rotZ < -25) _rotZ = -25;
}
else {
if (_rotZ < 0) {
_rotZ -= 2 * -_mouseZSensistivity * Time.deltaTime * 50;
if (_rotZ > 0) _rotZ = 0;
}
}
Quaternion currentRotation = Quaternion.identity;
currentRotation = currentRotation * Quaternion.AngleAxis(_rotX, transform.up);
currentRotation = currentRotation * Quaternion.AngleAxis(-_rotY, transform.right);
currentRotation = currentRotation * Quaternion.AngleAxis(_rotZ, transform.forward);
_camera.transform.localRotation = currentRotation;
//head.transform.position = _camera.transform.position;
//head.transform.rotation = _camera.transform.rotation;
}
The last part with quaternions is where im trying to calculate angles in order to properly rotate in local coordinates.
You don’t need to use quaternions at all.
You can use transform.EulerAngles instead of the transform.rotation or transform.localEulerAngles instead of transform.LocalRotation.
I messed up the capitalization I’m sure.
Say you wanted to rotate the camera 10 degrees along the local x axis. That would look something like
transform.localEulerAngles = transform.localEulerAngles.Add(10,0,0);
That’s it as far as I know. If you wanna read more about this,
transfrom.localEulerAngles
If your question was completely different, let me know and I can change or remove my answer.

How can I get this jumping and movement code to work together?

I'm trying to use the Character Controller component in Unity and I managed to make the movement code, however, I was unable to add jumping and gravity or at least have them work together so my temporary solution was to just break them into 2 different methods. This probably isn't ideal so how could I get this to work properly?
void Update()
{
GetInput();
JumpingCode();
MovementCode();
}
void JumpingCode()
{
// Jumping
if (jumpPressed && characterController.isGrounded)
velocityY = Mathf.Sqrt(jumpHeight * -2f * (gravity * gravityScale));
// Gravity
velocityY += gravity * gravityScale * Time.deltaTime;
Vector3 direction = new Vector3(horizontalInput, velocityY, verticalInput).normalized;
characterController.Move(direction * walkSpeed * Time.deltaTime);
}
void MovementCode()
{
Vector3 direction = new Vector3(horizontalInput, 0f, verticalInput).normalized;
if (direction.magnitude > 0.1f)
{
float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + playerCamera.eulerAngles.y;
float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
Vector3 moveDirection = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
transform.rotation = Quaternion.Euler(0f, angle, 0f);
characterController.Move(moveDirection.normalized * walkSpeed * Time.deltaTime);
}
}
I can't understand how you're arriving at the values you're using there, but the solution would be to accumulate the outputs and then do the .Move() action once at the end, like:
void Update()
{
Vector3 motion;
GetInput();
motion += JumpingCode();
motion += MovementCode();
characterController.Move(motion*Time.deltaTime);
}
private Vector3 JumpingCode()
{
// stuff
return direction * walkSpeed;
}
private Vector3 MovementCode()
{
// stuff
return direction * walkSpeed;
}
Noteworthy there is that I dropped Time.deltaTime from your functions, but I don't know how you were using it in the code you provided.

Delay when calculating a players movement direction

I am trying to determine the direction of a transform. I originally tried to do this by tracking the rigidbody.velocity but that property seems unreliable so I am having to calculate the direction manually. The yellow line draws fine and it does point in the wrong direction however it appears to be delayed
I am calling the following method in my update method:
void DetermineMovementDirection()
{
currentLoc = (transform.position - prevLoc) / Time.deltaTime;
Vector3 t = (prevLoc + currentLoc).normalized;
Debug.DrawLine(transform.position, transform.position + t * 5, Color.yellow);
}
I would expect the yellow line to always point in the direction the player was moving rather than have a long delay. How can I fix this?
As requested the movement function:
void Update()
{
float inputZ = Input.GetAxis("Horizontal");
float inputX = Input.GetAxis("Vertical");
if (movementAllowed)
if (inputZ != 0 || inputX != 0)
{
transform.eulerAngles = new Vector3(0, Mathf.Atan2(inputZ, inputX) * 180 / Mathf.PI, 0);
transform.Translate(moveSpeed * inputZ * Time.deltaTime, 0f, moveSpeed * inputX * Time.deltaTime, Space.World);
}
}
And I am simply updating at the end of the update method:
prevLoc = transform.position;

Unity 3rd person controller, rotation nightmare

I am trying to make a MMO character controller like the one of this Youtube video:https://www.youtube.com/watch?v=fOvf7gRO_aM
Basically, you use WASD to move around.
You can move the camera by mouse click and drag, and when moving, you character will now move in the new camera direction.
The thing is, i would like that when i press WASD, that the character (not the camera) would face the direction of the mouvement.
I tried to use this:
if (Input.GetAxis("Vertical") > 0 | Input.GetAxis("Vertical") < 0){
Quaternion turnAngle = Quaternion.Euler(0, centerPoint.eulerAngles.y, 0);
character.localRotation = Quaternion.Slerp(character.rotation, turnAngle, Time.deltaTime * rotationSpeed);
}
The character was not facing the right direction, so i tried this
if (Input.GetAxis("Vertical") > 0 | Input.GetAxis("Vertical") < 0){
Quaternion turnAngle = Quaternion.Euler(0, centerPoint.eulerAngles.y, 0);
character.rotation = Quaternion.LookRotation(movement);
}
but that does not seem to work. I am a noob after all :D
Here is the full code of the controller's move part:
private void Move()
{
moveFrontBack = Input.GetAxis("Vertical") * moveSpeed;
moveLeftRight = Input.GetAxis("Horizontal") * moveSpeed;
Vector3 movement = new Vector3(moveLeftRight, 0, moveFrontBack);
movement = character.rotation * movement;
characterController.Move(movement * Time.deltaTime);
//Animation on move
if (movement.magnitude != 0)
{
anim.SetBool("isWalking", true);
anim.SetBool("isIdle", false);
}
if (movement.magnitude == 0)
{
anim.SetBool("isWalking", false);
anim.SetBool("isIdle", true);
}
centerPoint.position = new Vector3(character.position.x, character.position.y + mouseYPosition, character.position.z);
//The place where things go south it seems
if (Input.GetAxis("Vertical") > 0 | Input.GetAxis("Vertical") < 0)
{
Quaternion turnAngle = Quaternion.Euler(0, centerPoint.eulerAngles.y, 0);
character.rotation = Quaternion.Slerp(character.rotation, turnAngle, Time.deltaTime * rotationSpeed);
}
I had a previous version on the controller, without the change of camera with mouse, but the right character's behaviour facing the direction of the input:
private void Move()
{
moveFrontBack = Input.GetAxis("Vertical") * moveSpeed;
moveLeftRight = Input.GetAxis("Horizontal") * moveSpeed;
Vector3 movement = new Vector3(moveLeftRight, 0, moveFrontBack);
characterController.Move(movement * Time.deltaTime);
if (movement != Vector3.zero) transform.rotation = Quaternion.LookRotation(movement);
if (movement.magnitude != 0)
{
anim.SetBool("isWalking", true);
anim.SetBool("isIdle", false);
}
if (movement.magnitude == 0)
{
anim.SetBool("isWalking", false);
anim.SetBool("isIdle", true);
}
playerCamera.position = new Vector3(character.position.x, character.position.y + yCamera, character.position.z + zCamera);
}
Also, here is the mousemoving part:
void MouseTurnAround()
{
zoom += Input.GetAxis("Mouse ScrollWheel") * zoomSpeed;
if (zoom > zoomMin)
zoom = zoomMin;
if (zoom < zoomMax)
zoom = zoomMax;
playerCamera.transform.localPosition = new Vector3(0, 0, zoom);
if (Input.GetMouseButton(0))
{
mouseX += Input.GetAxis("Mouse X");
mouseY -= Input.GetAxis("Mouse Y");
}
mouseY = Mathf.Clamp(mouseY, -60f, 60f);
playerCamera.LookAt(centerPoint);
centerPoint.localRotation = Quaternion.Euler(mouseY, mouseX, 0);
}
I don't really have more ideas, so maybe smart people can see what they can do ! Thanks in advance..
I managed some kind of a mix of my 2 solutions, and parented my character to an empty gameobject, which receives the new rotations. Then, i leave the character be oriented to the movement with look rotation.
private void Move()
{
moveFrontBack = Input.GetAxis("Vertical") * moveSpeed;
moveLeftRight = Input.GetAxis("Horizontal") * moveSpeed;
Vector3 movement = new Vector3(moveLeftRight, 0, moveFrontBack);
movement = character.rotation * movement;
characterController.Move(movement * Time.deltaTime);
//Animation on move
if (movement.magnitude != 0)
{
anim.SetBool("isWalking", true);
anim.SetBool("isIdle", false);
}
if (movement.magnitude == 0)
{
anim.SetBool("isWalking", false);
anim.SetBool("isIdle", true);
}
centerPoint.position = new Vector3(character.position.x, character.position.y + mouseYPosition, character.position.z);
//Rotates the character to move towards new direction
if (Input.GetAxis("Vertical") > 0 | Input.GetAxis("Vertical") < 0)
{
Quaternion turnAngle = Quaternion.Euler(0, centerPoint.eulerAngles.y, 0);
character.rotation = Quaternion.Slerp(character.rotation, turnAngle, Time.deltaTime * rotationSpeed);
}
if (movement != Vector3.zero) lkModel.rotation = Quaternion.LookRotation(movement);
}

How to prevent tilting over in a unity object

I've got a hover tank and I'm working on a controller for it. The goal is to have the float above the ground, but I don't want it to tilt over more the a degree or two. I want it to basically stay level all the time.
I'm using a Rigidbody on the tank to control it with .MovePosition and .MoveRotation. You can see the FixedUpdate function below. I've got a section below to keep thinks level, where I check if there tank is tipping past its maxTilt amount. If it is, the keep it at the max.
This makes the tank very jittery all the time. It looks like it's bouncing up and down quickly. I think its due to the hover forces, but I'm not sure.
How can I keep the tank level while still letting it hoover?
FixedUdpate
void FixedUpdate () {
if (!isServer) {
return;
}
CheckGrounded ();
Hoover ();
if (_moveForward) {
float moveAmount = moveSpeed * Time.deltaTime;
_rigidbody.MovePosition(_rigidbody.position + _rigidbody.transform.forward * moveAmount);
}
if (_moveBackward) {
float moveAmount = (-moveSpeed * 0.6f) * Time.deltaTime;
_rigidbody.MovePosition(_rigidbody.position + _rigidbody.transform.forward * moveAmount);
}
if (_turnLeft) {
Quaternion rotateAmount = Quaternion.Euler(new Vector3(0f, -angularSpeed, 0f) * Time.deltaTime);
_rigidbody.MoveRotation(_rigidbody.rotation * rotateAmount);
}
if (_turnRight) {
Quaternion rotateAmount = Quaternion.Euler(new Vector3(0f, angularSpeed, 0f) * Time.deltaTime);
_rigidbody.MoveRotation(_rigidbody.rotation * rotateAmount);
}
if (_jump && _isGrounded) {
_isJumping = true;
}
if (_isJumping && _jumpTimeLeft > 0) {
float moveAmount = jumpSpeed * Time.deltaTime;
_rigidbody.MovePosition(_rigidbody.position + _rigidbody.transform.up * moveAmount);
_jumpTimeLeft -= Time.deltaTime;
} else if (_isJumping) {
_isJumping = false;
_jumpTimeLeft = jumpTime;
}
// Keep things level
Vector3 rotation = _rigidbody.rotation.eulerAngles;
if (rotation.x > maxTilt) {
rotation.x = maxTilt;
} else if (rotation.x < -maxTilt) {
rotation.x = -maxTilt;
}
if (rotation.y > maxTilt) {
rotation.y = maxTilt;
} else if (rotation.y < -maxTilt) {
rotation.y = -maxTilt;
}
if (rotation.z > maxTilt) {
rotation.z = maxTilt;
} else if (rotation.z < -maxTilt) {
rotation.z = -maxTilt;
}
Quaternion q = new Quaternion ();
q.eulerAngles = rotation;
_rigidbody.rotation = q;
}
Hoover
void Hoover() {
foreach (Transform hoverPoint in hooverPoints) {
Ray ray = new Ray (hoverPoint.position, -hoverPoint.up);
RaycastHit hitInfo;
if (Physics.Raycast (ray, out hitInfo, hooverHeight)) {
float distance = Vector3.Distance (hoverPoint.position, hitInfo.point);
if (distance < hooverHeight) {
_rigidbody.AddForceAtPosition (hoverPoint.up * hooverForce * (1f - distance / hooverHeight), hoverPoint.position, ForceMode.Force);
}
}
}
}
I'm thinking that the reason you are seeing 'jitters' is because...
All physics calculations and updates occur immediately after FixedUpdate.
https://docs.unity3d.com/Manual/ExecutionOrder.html
Because you are adjusting the tilt in FixedUpdate(), which is then immediately followed by the Physics Engine running its calculations this will sometimes alter the tilt value giving a 'jitter'. The reason I say sometimes is because FixedUpdate() can run multiple times per frame (FPS dependant), which potentially means the following call order for a single frame:
FixedUpdate() // start frame
PhysicsEngine
FixedUpdate() // end frame
In the case above, there would be no jitter because you re-correct the tilt after the physics engine has done its thing - by complete fluke. However when you don't get a second FixedUpdate() call on a frame, you will have:
FixedUpdate() // start frame
PhysicsEngine // end frame
Which will result in your jitter.
So my suggestion is to break up your FixedUpdate() and offset any tilt corrections to LateUpdate(). Becuase LateUpdate() is always the last update call prior to the rendering of the frame.