I try setControlRotation but Pitch always ignore that. It works only for Yaw
Also i tried set Pitch in animation bluprint. Seems that somthing change it before animation bluprint, beacose Pitch tried to return in to previous value
at the end i make aiming through creating special simple pawn:
create pawn
setFocus on that pawn
move pawn to enemy's head
The default AIController overrides pitch in this function. You'll need to make a new parent to get the other features of AIController.
void AEEAIController::UpdateControlRotation(float DeltaTime, bool bUpdatePawn)
{
APawn* const MyPawn = GetPawn();
if (MyPawn)
{
FRotator NewControlRotation = GetControlRotation();
// Look toward focus
const FVector FocalPoint = GetFocalPoint();
if (FAISystem::IsValidLocation(FocalPoint))
{
NewControlRotation = (FocalPoint - MyPawn->GetPawnViewLocation()).Rotation();
}
else if (bSetControlRotationFromPawnOrientation)
{
NewControlRotation = MyPawn->GetActorRotation();
}
// We override this so flying units can go up and down
//// Don't pitch view unless looking at another pawn
//if (NewControlRotation.Pitch != 0 && Cast<APawn>(GetFocusActor()) == nullptr)
//{
// NewControlRotation.Pitch = 0.f;
//}
SetControlRotation(NewControlRotation);
if (bUpdatePawn)
{
const FRotator CurrentPawnRotation = MyPawn->GetActorRotation();
if (CurrentPawnRotation.Equals(NewControlRotation, 1e-3f) == false)
{
MyPawn->FaceRotation(NewControlRotation, DeltaTime);
}
}
}
}
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 a beginner on Unity3D and I decided to create a 3D Tetris to start learning. Everything was fine so far, but I have a problem with collision detection.
https://gyazo.com/49fbf5798dc67a546c5e187fde8f6096
As you can see on this screen, the blue tetromino is not at the good place. This is due to the fact that the orange tetromino position in X is not precise (-2.9999).
Each block that makes up a tetromino has the tag "Block". The ground has the tag "Ground".
This is how I detect collisions in my OnCollisionEnter method.
if (collision.collider.tag == "Block" && !detectedBefore)
{
for (int k = 0; k < collision.contacts.Length; k++)
{
if (Vector3.Angle(collision.contacts[k].normal, validDirection) <= contactThreshold)
{
GetComponent<TetrominoMovement>().Snap();
GetComponent<TetrominoMovement>().enabled = false;
FindObjectOfType<Tetromino>().GenTetromino();
detectedBefore = true;
break;
}
}
}
if (collision.collider.tag == "Ground" && !detectedBefore)
{
GetComponent<TetrominoMovement>().Snap();
GetComponent<TetrominoMovement>().enabled = false;
FindObjectOfType<Tetromino>().GenTetromino();
detectedBefore = true;
}
Do you know how to get around this problem? Thank you in advance.
I'm trying to create a modification to the ARCore sample Andy placement app by only rendering a single plane instead of all the detected planes. I'd like to achieve this by tapping on the plane to be rendered. Here's what I've tried.
protected override void OnEndManipulation(TapGesture gesture)
{
if (gesture.WasCancelled)
{
return;
}
// If gesture is targeting an existing object we are done.
if (gesture.TargetObject != null)
{
return;
}
// Raycast against the location the player touched to search for planes.
TrackableHit hit;
TrackableHitFlags raycastFilter = TrackableHitFlags.PlaneWithinInfinity | TrackableHitFlags.PlaneWithinPolygon;
if (Frame.Raycast(gesture.StartPosition.x, gesture.StartPosition.y, raycastFilter, out hit))
{
Debug.Log("Entered into hit if condition");
// Use hit pose and camera pose to check if hittest is from the
// back of the plane, if it is, no need to create the anchor.
if ((hit.Trackable is DetectedPlane) &&
Vector3.Dot(FirstPersonCamera.transform.position - hit.Pose.position,
hit.Pose.rotation * Vector3.up) < 0)
{
//var andyObject = Instantiate(AndyPrefab, hit.Pose.position, hit.Pose.rotation);
Debug.Log("Hit at back of the current DetectedPlane");
}
else
{
if (PlaneFixed is false)
{
foreach (GameObject plane in DetectedPlaneGenerator.instance.PLANES) //RK
{
// THE PROBLEM IS HERE! HOW TO GET CORRECT TYPECAST?
if (hit.Trackable is plane)
{
Debug.Log("Plane Selected");
PlaneFixed = true;
FixPositionButton.gameObject.SetActive(true);
}
else
{
plane.SetActive(false);
}
}
}
}
}
}
The problem is that I'm unable to get the correct matching between a GameObject (PLANES - created in DetectedPlaneGenerator.cs) and the DetectedPlane object (return by hit.Trackable, using the PlaneWithinInfinity | PlaneWithinPolygon raycastfilters).
I've also seen the method to try and associate a 'tag' to the DetectedPlaneVisualizer (solution to this). However, I see that this too deals with planes using a GameObject. How to match the GameObject and Hit.Trackable (DetectedPlane Object) to identify which specific plane was just tapped on?
Context
I am making a mobile game in which the player is required to touch objects in a specified order. The correct order is determined in a List called clickOrder. To determine the current object the player is supposed to click, currClickIndex is used.
Problem
When touching a correct object, the debug text will display "Correct" for a split second, and will then immediately change to "Wrong." What I am unsure about is why both the if and else blocks are executed when only touching a single object.
Code
void Update()
{
if (Input.touchCount == 1)
{
if (this.enabled)
{
Vector2 worldPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition);
RaycastHit2D hit = Physics2D.Raycast(worldPoint, Vector2.zero);
if (hit != null && hit.collider != null)
{
// check if the touched object is the correct one
if (hit.collider.gameObject == clickOrder[MyData.currClickIndex])
{
debug.text = "Correct";
MyData.currClickIndex++;
}
else
{
debug.text = "Wrong";
}
}
}
}
}
As soon as the correct object is being touched, you do this:
MyData.currClickIndex++;
which moves you forward in the ordered sequence, and from then on, the previously correct object is not correct anymore. But you're still touching it.
If you want to avoid this, you need to move forward in the sequence after you've touched the correct object.
if (there are touches and the correct object is being touched)
{
set a flag;
}
else if (a flag has been set)
{
MyData.currClickIndex++;
reset the flag;
}