Unity jittery movement - c#

I have the following simple lines of code that moves the camera similar to the camera movement in zig zag game ( moving vertically forever ). I also have very simple cube structure similar to zig zag game. In original zig zag game, there is no jittery movement in iPad Mini. But the below lines of code causing jittery movement..
I tried project settings->Time setting to 0.03 and targetFrameRate to
Tried in Update
Tried in FixedUpdate
Tried in LateUpdate...
But none works, I can always reproduce the jittery movement
void Update() {
float calculatedSpeed = speed * Time.deltaTime;
transform.Translate( direction * calculatedSpeed);
}
void LateUpdate() {
if( !dead ) {
Vector3 position = transform.position;
float halfX = position.x;
float halfZ = position.z;
float distance = Vector3.Distance (position, playerInitPosition);
Vector3 newPos = Main.Instance.camReference.forward * distance;
Main.Instance.camReference.position = newPos;
}
}
In Update, transform.position updates player move in zig zag pattern,
In LateUpdate, camera moves vertically based on the distance the player moves
You can check android game play here:
https://play.google.com/store/apps/details?id=com.angryeggstudio.games.zigzagpenguin
This smoothness is not there in iPad Mini
Update 1:
Android that works without any flaw...its smooth and fine :
iPad Mini that has jittery movement:
Note: Check the cube edges while camera moves... you can spot its jittery movement in iPad Mini...

Related

Unity FPS Controller camera only moves when mouse movement stops

Using unity FPSController asset, here's the code for actually moving the camera called in LateUpdate() method.
private void CameraRotation()
{
// if there is an input
if (_input.look.sqrMagnitude >= _threshold)
{
//Don't multiply mouse input by Time.deltaTime
float deltaTimeMultiplier = IsCurrentDeviceMouse ? 1.0f : Time.deltaTime;
_cinemachineTargetPitch += _input.look.y * RotationSpeed * deltaTimeMultiplier;
_rotationVelocity = _input.look.x * RotationSpeed * deltaTimeMultiplier;
// clamp our pitch rotation
_cinemachineTargetPitch = ClampAngle(_cinemachineTargetPitch, BottomClamp, TopClamp);
// Update Cinemachine camera target pitch
CinemachineCameraTarget.transform.localRotation = Quaternion.Euler(_cinemachineTargetPitch, 0.0f, 0.0f);
// rotate the player left and right
transform.Rotate(Vector3.up * _rotationVelocity);
}
}
At first, I thought I was just having FPS issues, but the game is completely smooth when the camera is not moving, as soon as movement starts, the camera is extremely jittery. further testing it feels like the camera only moves when I stop moving my mouse. Like it knows where it should be but doesn't get there smoothly, just gets there when you're done inputting your movement. I've tried lowering the rotation speed variable to make sure its not a sensitivity thing. Same problem.

Unity: TopDown view - GameObject does not rotate toward mousepos

I've been doing some research on why my player(GameObject) is does not rotate toward my mouse position in my TopDown 3D game and I can't seem to find what is wrong with my code, so im making this post. The problem thats I have is that only the GameObject of my player (in my case, a capsule) rotate toward my mouse position but the axis of my player stays the same. In other word, I can't rotate the axis of my player, to face my mouse position, but I can rotate the GameObject of my player to face my mouse position. Its really hard to explain and this never happened to me before. Question is how can I rotate the axis of my player to face my mouse position. Keep in mind that my game is a top down view.
Here is the code im using for my playerMouvment and for my mouseLook:
public class Controller : MonoBehaviour
{
public float moveSpeed = 6;
Rigidbody rb;
Camera viewCamera;
Vector3 velocity;
void Start()
{
rb = GetComponent<Rigidbody>();
viewCamera = Camera.main;
}
void Update()
{
Vector3 mousePos = viewCamera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, viewCamera.transform.position.y));
transform.LookAt(mousePos + Vector3.up * transform.position.y);
velocity = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical")).normalized * moveSpeed;
}
void FixedUpdate()
{
rb.MovePosition(rb.position + velocity * Time.fixedDeltaTime);
}
}
Again I tried and look for any error in my code and I can't find anything that cause this weird situation and so if anyone can help me find a better way to write this code and solve my problem it would be great!
If I understand you correctly everything is fine with your game and the player is turning as it should, but only in the editor that the axis of the player (red, green and blue arrows) don't turn with the player?
If this is the problem it might be that you are using global space handle instead of local space. Clicking the icon I highlighted in the image should do the trick.
Use this code instead of LookAt() function
Vector2 direction = mousePos.position - player.transform.position;
player.transform.right /* Maybe you need Up or -Up or -right */ = direction;
This would work too in some cases
Vector2 direction = new Vector2
(
mousePos.position.x - player.transform.position.x,
mousePos.positoin.y - player.transform.position.y
)
player.transform.right /* Maybe you need Up or -Up or -right */ = direction;

Unity: Track player movement with camera only when character reaches a certain point on the Y axis?

I'm currently working on a 2D platforming game using Unity. When the player jumps, I prevent the camera from following them in order to allow the player to see what is beneath them. However, I have a mechanic implemented that allows the player to perform a double jump when they grab an enemy, and this double jump makes the player jump past the boundary of the screen. I was wondering how I would go about getting the camera to smoothy follow the player only when they exit certain boundaries. I have basic code written that does this in a choppy way. I will include gifs to show how my game currently behaves, and an example of how I want it to behave.
Here is the code I have written in my camera controller:
transform.position = new Vector3(player.position.x, 50, -100);
if (player.position.y > 50)
{
transform.position = player.position - transform.forward * camDist + Vector3.up * playerHeight;
}
Below is an embeded imgur gif of what my code does now:
<blockquote class="imgur-embed-pub" lang="en" data-id="a/houedOV" data-context="false" ></blockquote><script async src="//s.imgur.com/min/embed.js" charset="utf-8"></script>
and below here is an example of what I am trying to achieve:
<blockquote class="imgur-embed-pub" lang="en" data-id="a/deg5yeq"></blockquote><script async src="//s.imgur.com/min/embed.js" charset="utf-8"></script>
You should moving camera from origin to destination instead of set camera to destination immediately (1 frame or 1 update called)
Try to set camera position with this code
void Update(){
// ...
Vector2 destination = Vector2.zero; // your camera destination you expected
float maxMoveDistance = 1; // maximum distance to move camera in each frame
camera.transform.position = Vector2.MoveTowards(camera.transform.position, destination, maxMoveDistance); // move camera to destination
}
ok, I figured out the answer to my own question!
It was as simple as this:
void Update()
{
if (player.position.y > 50)
{
transform.position = new Vector3(player.position.x, player.position.y, -100);
}
else
{
transform.position = new Vector3(player.position.x, 50, -100);
}

Unity - Joystick to movement and rotate camera same time

I have a problem.
I want to use joystick to movement player.
I want to rotate camera if player click ANYWHERE ELSE on screen.
Here is picture
My problem:
If the player use joystick then camera rotate too!
I tried IsPointerOverGameObject not good, because player press the joystick and drag it to the screen then joystick still work, but camera rotate again :(
So I think if player use joystick then I disable rotate camera, but not good, because If player use two fingers it is possible both at the same time. (one finger joystick and one finger screen)
public float speed = 2.0f;
private float X;
private float Y;
void Update() {
if (Input.GetMouseButton (0)) {
if(!EventSystem.current.IsPointerOverGameObject ()) {
transform.Rotate (new Vector3 (Input.GetAxis ("Mouse Y") * speed, -Input.GetAxis ("Mouse X") * speed, 0));
X = transform.rotation.eulerAngles.x;
Y = transform.rotation.eulerAngles.y;
transform.rotation = Quaternion.Euler (X, Y, 0);
}
}
}
So another solution is needed.
If player use the joystick, do not move the camera.
But if player use it with two fingers (one with the joystick and the other with the camera allowed!)
I hope you understand. (similar to other shooting games).
Thanks!

Rotate around an object to its direction of velocity

(For Unity 5.3.5f1)
Right now I am working on a 3D camera that is orbiting horizontally around the player. The player is a RigidBody rolling sphere. The player can freely rotate the axis horizontally but when there is no input I want the rotation to reset back to the direction of the velocity.
Right now all I need to know is how to situate the camera behind the player's direction of velocity by rotating the camera around the player from its previous rotation.
Here is a crude drawing of what I am looking for:
Currently to update the camera to orbit around the player I use this script on the camera (with comments):
using UnityEngine;
using System.Collections;
public class example : MonoBehaviour {
public GameObject Player;
//I assume you would use this PlayerRB to find data regarding the movement or velocity of the player.
//public Rigidbody PlayerRB;
private float moveHorizontalCamera;
private Vector3 offset;
// Use this for initialization
void Start ()
{ //the offset of the camera to the player
offset = new Vector3(Player.transform.position.x - transform.position.x, transform.position.y - Player.transform.position.y, transform.position.z - Player.transform.position.z);
}
// Update is called once per frame
void Update ()
{
moveHorizontalCamera = Input.GetAxis("Joy X"); //"Joy X" is my right joystick moving left, none, or right resulting in -1, 0, or 1 respectively
//Copied this part from the web, so I half understand what it does.
offset = Quaternion.AngleAxis(moveHorizontalCamera, Vector3.up) * offset;
transform.position = Player.transform.position + offset;
transform.LookAt(Player.transform.position);
}
}
Any help at all would be great!
I would suggest you use Vector3.RotateTowards (https://docs.unity3d.com/ScriptReference/Vector3.RotateTowards.html)
You take the Vector that is currently pointing from the Player to the camera (transform.position - Player.transform.position) and rotate it towards -Player.GetComponent<Rigidbody>().velocity.normalized * desiredDistance (the vector pointing in the opposite direction of the players velocity with magnitude corresponding the desired distance of the camera to the player). You can then use Vector3.RotateTowards to rotate the camera (smoothly or immediately) around the Player by setting the cameras position accordingly.
Something like:
transform.position = Player.transform.position + Vector3.RotateTowards(transform.position - Player.transform.position, -Player.GetComponent<Rigidbody>().velocity.normalized * desiredDistance, step, .0f);
where step if the angle update in radians. (Note: you should avoid calling GetComponent<Rigidbody>() every Update. You are better off storing it somewhere)
I hope I understood your question correctly and this helps.

Categories

Resources