I'm trying to detect the movement (to the right or left) performed by users.
We assume that the user starts with his arm extended in front of him and then moves his arm to the right or to the left (about 90 degrees off center).
I've integrated CMMotionManager and want to understand detecting direction via startAccelerometerUpdatesToQueue and startDeviceMotionUpdatesToQueue methods.
Can anyone suggest how to implement this logic on an iPhone and then on an Apple Watch?
Apple provides watchOS 3 SwingWatch sample code demonstrating how to use CMMotionManager() and startDeviceMotionUpdates(to:) to count swings in a racquet sport.
Their code demonstrates how to detect the direction of a one-second interval of motion, although you may have to tweak the thresholds to account for the characteristics of the motion you want to track.
func processDeviceMotion(_ deviceMotion: CMDeviceMotion) {
let gravity = deviceMotion.gravity
let rotationRate = deviceMotion.rotationRate
let rateAlongGravity = rotationRate.x * gravity.x // r⃗ · ĝ
+ rotationRate.y * gravity.y
+ rotationRate.z * gravity.z
rateAlongGravityBuffer.addSample(rateAlongGravity)
if !rateAlongGravityBuffer.isFull() {
return
}
let accumulatedYawRot = rateAlongGravityBuffer.sum() * sampleInterval
let peakRate = accumulatedYawRot > 0 ?
rateAlongGravityBuffer.max() : rateAlongGravityBuffer.min()
if (accumulatedYawRot < -yawThreshold && peakRate < -rateThreshold) {
// Counter clockwise swing.
if (wristLocationIsLeft) {
incrementBackhandCountAndUpdateDelegate()
} else {
incrementForehandCountAndUpdateDelegate()
}
} else if (accumulatedYawRot > yawThreshold && peakRate > rateThreshold) {
// Clockwise swing.
if (wristLocationIsLeft) {
incrementForehandCountAndUpdateDelegate()
} else {
incrementBackhandCountAndUpdateDelegate()
}
}
// Reset after letting the rate settle to catch the return swing.
if (recentDetection && abs(rateAlongGravityBuffer.recentMean()) < resetThreshold) {
recentDetection = false
rateAlongGravityBuffer.reset()
}
}
Related
So my movement isn't instant. You accelerate and decelerate. I use two floats starting at 0, going to 1, to calculate my movement speed. Here it is:
private void Update()
{
float moveTowardsX = 0;
float moveTowardsY = 0;
// Adjust thrust to be below max Thrust
if(thrust >= maxThrust)
{
thrust = maxThrust;
}
// Calculates movement speed based on all variables
float changeRatePerSecond = 1 / timeFromZeroToMax * thrust * Time.deltaTime;
changeRatePerSecond /= weight / 1.5f;
if(onMobile == true)
{
// Checks for Input. If Input is detected, values get changed [MOBILE]
if(isMovingLeft == true && isMovingRight == false)
{
moveTowardsX = -1.0f;
}
else if(isMovingRight == true && isMovingLeft == false)
{
moveTowardsX = 1.0f;
}
if(isMovingUp == true && isMovingDown == false)
{
moveTowardsY = 1.0f;
}
else if(isMovingDown == true && isMovingUp == false)
{
moveTowardsY = -1.0f;
}
if(isRotatingLeft == true && isRotatingRight == false)
{
rotationsPerSecond++;
}
else if(isRotatingRight == true && isRotatingLeft == false)
{
rotationsPerSecond--;
}
}
Then I parse these values into my movement code and smooth them out:
void FixedUpdate()
{
// Makes values change smoothly
valueX = Mathf.MoveTowards(valueX, moveTowardsX, changeRatePerSecond);
valueY = Mathf.MoveTowards(valueY, moveTowardsY, changeRatePerSecond);
// Turn numbers into movement
rb.velocity = new Vector2(valueX * runSpeed, valueY * runSpeed);
}
The Problem is that when I run into a wall, the numbers don't decrease. And if I run into a wall standing still, the numbers increase. Here is a video linking to my problem too (Look at the top right): https://youtu.be/XRE9p0yo4GA
What could I implement to fix this?
I have 2 suggestions, either one should work:
Do a ground check. Platformers often perform ground checks to check that the player is on the ground. By doing a box cast, you can check if your character is adjacent to a wall in the direction of their movement, and if they are, do not let them move (or gain speed) in that direction.
Here is a video for how to do a ground check. I would recommend the box cast method here, since it would give you the most accurate results:
https://www.youtube.com/watch?v=c3iEl5AwUF8
Use the rb.velocity of the object in a direction divided by your character's maximum speed in place of your valueX and valueY. So, use:
rb.velocity.x / runSpeed
instead of
valueX
and the same for y. This would take into account anything that changes the velocity of the object outside this script, such as crashing into a wall, but also if your character is thrown by some effect you create later on.
im having trouble with the hand IK of a -recoil shoot animation- that is on an override layer in the animator. I have it set up so that when I aim(left trigger), it allows me to shoot with another button(right trigger). I have it so the weight (hand IK) increases to 1 (in the rig layer) when I aim and decreases to 0 when I let go of the aim button.
The problem is that if I shoot the gun (uses recoil gun animation) and let go of the aim button immediately afterwards, the animation will continue to play but the IK will go to 0 and the hands will look all messed up.
I've tried setting the IK hand weight to 1 when he fires the gun, so he'll still hold onto it, but it doesn't work since shoot is only true if aim is being held down.
So overall I fire the gun, he does a recoil animation, and when you let go of aim too early, the animation looks all messed up like he's waving to you from behind his back. Like it aborts the animation or something.
private void HandleMovementInput()
{
horizontalMovementInput = movementInput.x;
verticalMovementInput = movementInput.y;
animatorManager.HandleAnimatorValues(horizontalMovementInput, verticalMovementInput, runInput);
if (verticalMovementInput != 0 || horizontalMovementInput !=0) //running
{
if (aimingInput)
{
if (animatorManager.isAimingGunn == false)
{
shootInput = false;
}
}
}
if (animatorManager.isAimingGunn)
{
animatorManager.leftHandIK.weight = 1; // IK weight
animatorManager.rightHandIK.weight = 1;
horizontalMovementInput = 0f; // joystick
verticalMovementInput = 0f;
}
else if(animatorManager.isAimingGunn == false)
{
if(verticalMovementInput != 0 || horizontalMovementInput !=0) //running
{
animatorManager.leftHandIK.weight = 0;
}
animatorManager.leftHandIK.weight = 0; // IK weight
animatorManager.rightHandIK.weight = 0;
}
}
private void HandleShoot()
{
if (aimingInput) // must aim otherwise shootInput(right trigger on controller) wont work
{
if (shootInput)
{
shootInput = false;
playerManager.UseCurrentWeapon();
}
}
}
I was able to fix it. I used this code to tell the character to keep aiming if still in the current animation.This allows for the animation to finish before you set the Hand IK to 0.
if(animator.GetCurrentAnimatorStateInfo(1).IsName("Pistol_Shoot"))
{
aimingInput = true;
}
Also Due to it getting overly complicated I changed the aim cancel button to the top left trigger on the controller ( while aim is on the bottom left trigger).
I'm currently learning Unity. I have learnt and did many projects and started making a new one about cars. My goal is NOT to achieve realistic physics. I'm trying to have a 0 slip.
I have done all the steps about the wheelcolliders, velocity, rotation, and overall movement. Currently I'm struggling to disable the car slip.
I don't want the car to slip at all. I tried using transform and change the position, but that isn't the method that I want to use.
Using the following method, I'm doing the acceleration:
private void doAccelerate() {
currentSpeed = (int) (rb.velocity.magnitude * 3.6f);
if (!hasBraked) {
if (hasReachedMax) {
rb.velocity = new Vector3(rb.velocity.x, rb.velocity.y, maxVelocity);
} else {
if (currentSpeed < maxSpeed) {
rearDriverW.motorTorque = m_verticalInput * motorForce * motorAccelerate;
rearPassengerW.motorTorque = m_verticalInput * motorForce * motorAccelerate;
} else if (currentSpeed > maxSpeed) {
hasReachedMax = true;
}
}
}
}
The following two pics provide the UI units for the Rigidbody and WheelColliders.
This pic, and this one.
I would some suggestions to apply 0 slip. I have tried playing with the value, yet I didn't reach the perfect values. Nevertheless, I didn't understand it correctly. That's why I didn't know.
So, I want to implement dodging which is something like a dash in a direction. It works pretty simple - if you move in a direction and press the button the character dodges there. The velocity should not matter for this, so that the dodging speed is always the same, no matter if you are running or standing and then pressing a direction + dodging. I've got something that seems to work but there are still problems left. It seems like that diagonal dodging is a bit too far in comparison to horizontal/vertical dodging and I tried to fix this. But I'm still not sure if it worked or not. In practice the character dodges and stuff but something still seems off, like it works pretty good with a controller but sometimes it's still wacky when using keyboard after standing still and then using dodge. The thing is - I'm not sure if the code I have is the best I could have or if there are still problems.
A good example of how it should work would be the dodging in Rune Factory 4.
Edit: If the problem lies in the Input Settings I could show them.
if (IsDodging == false)
{
Vector3 input = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
if (input.sqrMagnitude > 1) input.Normalize();
if (Input.GetButtonDown("Jump"))
{
// Jumping stuff I blended out
}
else if (Input.GetButtonDown("Block/Dodge") && input.x != 0 || Input.GetButtonDown("Block/Dodge") && input.z != 0) // To activate dodging
{
if (Time.time > Time_Start + DodgeCooldown) // Checks the cooldown
{
Time_Start = Time.time;
IsDodging = true;
dodge_direction = input;
if (dodge_direction.x > 0) { dodge_direction.x = 1; } // To fixate the distance traveled when dodging while ignoring how fast you are
if (dodge_direction.z > 0) { dodge_direction.z = 1; }
if (dodge_direction.x > 0 && dodge_direction.z > 0) { dodge_direction.Normalize(); } // To prevent diagonal dodging distance from becoming too big
}
}
else
{
// Walk.
transform.position += input * run_speed * Time.deltaTime;
}
}
else
{
transform.position += dodge_direction * dodge_distance;
distance_traveled += Vector3.Distance(transform.position, dodge_direction * dodge_distance) * Time.deltaTime;
if (distance_traveled > dodge_distance)
{
IsDodging = false;
dodge_direction = Vector3.zero;
distance_traveled = 0;
}
}
Im trying to control a robot by sending positions with 100hz. It's making a shaking movement when sending so much positions. When I send 1 position that is like 50 mm from his start position it moves smoothly. When I use my sensor to steer,(so it send every position from 0 to 50mm) it is shaking. I'm probably sending like X0-X1-X2-X1-X2-X3-X4-X5-X4-X5 and this is the reason why it might shake. How can I solve this making the robot move smoothly when I use my mouse to steer it?
Robot is asking 125hz
IR sensor is sending 100hz
Otherwise does the 25hz makes the diffrent?
Here is my code.
while(true)
// If sensor 1 is recording IR light.
if (listen1.newdata = true)
{
coX1 = (int) listen1.get1X(); //
coY1 = (int) listen1.get1Y();
newdata = true;
} else {
coX1 = 450;
coY1 = 300;
}
if (listen2.newdata = true)
{
coX2 = (int) listen2.get1X();
coY2 = (int) listen2.get1Y();
newdata = true;
} else {
coY2 = 150;
}
// If the sensor gets further then the workspace, it will automaticly correct it to these
// coordinates.
if (newdata = true)
{
if (coX1< 200 || coX1> 680)
{
coX1 = 450;
}
if (coY1<200 || coY1> 680)
{
coY1 = 300;
}
if (coY2<80 || coY2> 300)
{
coY2 = 150;
}
}
// This is the actually command send to a robot.
Gcode = String.format( "movej(p[0.%d,-0.%d, 0.%d, -0.5121, -3.08, 0.0005])"+ "\n", coX1, coY1, coY2);
//sends message to server
send(Gcode, out);
System.out.println(Gcode);
newdata = false;
}
}
private static void send(String movel, PrintWriter out) {
try {
out.println(movel); /*Writes to server*/
// System.out.println("Writing: "+ movel);
// Thread.sleep(250);
}
catch(Exception e) {
System.out.print("Error Connecting to Server\n");
}
}
}
# Edit
I discovered on wich way I can do this. It is via min and max. So basicly what I think I have to do is:
* put every individual coordinate in a array( 12 coordinates)
* Get the min and max out of this array
* Output the average of the min and max
Without knowing more about your robot characteristics and how you could control it, here are some general considerations:
To have a smooth motion of your robot, you should control it in speed with a well designed PID controller algorithm.
If you can only control it in position, the best you can do is monitoring the position & waiting for it to be "near enough" from the targetted position before sending the next position.
If you want a more detailed answer, please give more information on the command you send to the robot (movej), I suspect that you can do much more than just sending [x,y] coordinates.