- Home /
RigidBody2D velocity speed difference between devices
I looked everywhere but cannot find correct solition for my problem here is my code. This control a paddle and we give paddle speed and direction with touch but paddle velocity different all different devices. Thanks for help.
public class Velocity : MonoBehaviour
{
Vector2 startPoint;
Vector2 endPoint;
public Vector2 k;
public Rigidbody2D rb;
public bool a;
public bool rMove;
// Start is called before the first frame update
void Start()
{
a = true;
rMove = false;
rb = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
if (Input.touchCount > 0 && a)
{
if(Input.GetTouch(0).phase == TouchPhase.Began)
{
startPoint = Input.GetTouch(0).position;
}
if(Input.GetTouch(0).phase == TouchPhase.Ended)
{
endPoint = Input.GetTouch(0).position;
Vector2 fromPosition = startPoint;
Vector2 toPositionSmoke = endPoint;
Vector2 directionCharacter = (toPositionSmoke - fromPosition);
k = directionCharacter * ((float)(gameObject.GetComponent<RectTransform>().rect.width / gameObject.GetComponent<RectTransform>().rect.height));
if (k.x * 0.01f > -3 && k.x * 0.01f < 0)
{
Debug.Log("Raket Sol Girdi");
k.x = -3 / 0.01f; ;
// GetComponent<Rigidbody2D>().velocity = new Vector2(-3, k.y * 0.01f);
}
if (k.x * 0.01f < 3 && k.x * 0.01f > 0)
{
Debug.Log("Raket Sağ Girdi");
//GetComponent<Rigidbody2D>().velocity = new Vector2(3, k.y * 0.01f);
k.x = 3 / 0.01f; ;
}
a = false;
rMove = true;
}
}
}
void FixedUpdate()
{
if (rMove)
{
GetComponent<Rigidbody2D>().velocity = new Vector2(k.x * 0.01f, 5);
if (endPoint.x > startPoint.x)
{
Debug.Log("x büüyük");
rb.AddTorque(-30f);
}
if (endPoint.x < startPoint.x)
{
Debug.Log("x küçük");
rb.AddTorque(30f);
}
rMove = false;
}
}
void OnCollisionEnter2D(Collision2D coll)
{
if (coll.collider.CompareTag("Box"))
{
GameObject.Find("Canvas").GetComponent<CanvasScript>().gLost = true;
}
if (coll.collider.CompareTag("Floor"))
{
a = true;
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Instantiated object not showing in scene or hierarchy 2 Answers
How to make the tower shoot at the one who is closer to the finish line? 0 Answers
My balance in the game and my multiplier are not saving, i cant figure out why, any help? 0 Answers
How to check if a enemy is moving up or down? 1 Answer
Character not moving on Unity 5 2 Answers