Good Morning all people.
I m learning howto to smooth rotate my ship on a project from unity learn.
I`m using this code:
public Rigidbody miBody;
void FixedUpdate () {
Vector3 viewPos = Camera.main.WorldToViewportPoint(transform.position);
viewPos.x = Mathf.Clamp(viewPos.x, 0.05f, 0.95f);
viewPos.y = Mathf.Clamp(viewPos.y, 0.1f, 0.9f);
transform.position = Camera.main.ViewportToWorldPoint(viewPos);
#if UNITY_EDITOR
/*
float lado =Input.GetAxis("Horizontal") ;
float profundo = Input.GetAxis ("Vertical");
//Move the Player
Vector3 movement2 = new Vector3 (lado, 0.0f, profundo);
miBody.velocity = movement2 * speed;
*/
#endif
if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Moved) {
Vector2 touchDeltaPosition = Input.GetTouch (0).deltaPosition;
Vector3 movement = new Vector3 (touchDeltaPosition.x, 0.0f, touchDeltaPosition.y);
miBody.velocity = movement * speed;
}
if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Ended) {
miBody.velocity = Vector3.zero;
}
miBody.rotation = Quaternion.Euler (0.0f, 0.0f, miBody.velocity.x * inclina);
}
When i test that on unity editor with keyboard i get an smooth rotation on moving left and right, but when i use touch system its not smooth…
Thank you for suggestion.s
maybe because Keyboard input gives you 1f as a value and with deltaPosition you will get many different values depending on FPS and the lenght of way your finger moved
but can’t say what your keyboard input does because the #if unity_editor looks more like a gamepad / mouse input than a keyboard Input
1 Like
Thank you for answer.
I`m testing some to get an int from velocity.x
int test = Mathf.FloorToInt(miBody.velocity.x);
miBody.rotation = Quaternion.Euler (0.0f, 0.0f, test * inclina);
But also have same jitter effect or something like this…
make a debug log befor giving you rotate. Look how big the x value is when you use your Keyboard and than your touch, keyboard will give you 0 or 1, touch 0 or 5 or 3 some other value so you wont have the same , i think, but can’t clearly say thats the problem. maybe test something like this →
if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Moved) {
Vector2 touchDeltaPosition = Input.GetTouch (0).deltaPosition;
Vector3 movement = new Vector3 ((float)Mathf.Clamp((int)touchDeltaPosition.x, -1, 1), 0.0f, (float)Mathf.Clamp((int)touchDeltaPosition.y, -1, 1));
miBody.velocity = movement * speed;
}
1 Like
Thank you.
Testing with that… but still same effect, not smooth…
or it’s because of deltaPosition itself. like fixedUpdate its not called every frame.
in my projects, touch recognition is done in the Update and not FixedUpdate. but if it makes a big difference don’t know
i’m also aren’t using unity for that long
Well, i have tested something , and cant get a smooth values, maybe its for touchdeltaposition, because with keyboards rotates smooth.
I have tested with mathf.clamp this values but same effect…