- Home /
Question by
arjun873 · Aug 09, 2018 at 01:08 AM ·
movement2d gamerigidbody2dsmooth
How do I make Rigidbody2D movement smoother?
Hey, so I'm making a mobile 2d basketball game where you just swipe up on a ball and try to get it in the net. I'm using a rigidbody2d on the ball and I noticed the movement is a little jerky/sticky. It's just not as smooth as I would want it to be. Do any of you know how I can make it as smooth as possible?
Here's the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Swipe : MonoBehaviour {
Vector2 startPos, endPos, direction, newDirection;
public GameObject ball;
public CircleCollider2D ballCollider;
public PhysicsMaterial2D mat;
void Start () {
ball = GameObject.FindGameObjectWithTag("Ball");
}
bool TouchOnBall() {
if(startPos.x > 330f && startPos.x < 700f && startPos.y > 120 && startPos.y < 500)
{
return true;
}
else
{
return false;
}
}
// Update is called once per frame
void FixedUpdate () {
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
{
startPos = Input.GetTouch(0).position;
}
if (TouchOnBall() && ball.transform.position.y < -3f)
{
ballCollider.sharedMaterial = null;
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
{
startPos = Input.GetTouch(0).position;
}
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Ended)
{
endPos = Input.GetTouch(0).position;
direction = endPos - startPos;
newDirection = direction / direction.magnitude;
GetComponent<Rigidbody2D>().velocity = newDirection * 34.3f;
}
}
else
{
ballCollider.sharedMaterial = mat;
}
}
}
Thanks!
Comment
Have you tried using AddForce ins$$anonymous$$d of changing the velocity?