How to move a kinematic rigidbody with touch?
So, I am making a clone of AirHockey on Unity which would run on mobile devices. For that, I am using a cube as a player and a ball as the puck for the game. The cube has a Rigidbody component. There are left and right buttons on the canvas. Both buttons have OnPointerDown and OnPointerUp events attached.
Here is the code which controls the player:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class testscript : MonoBehaviour {
Rigidbody rb;
public float thrust = 10f;
// Use this for initialization
void Start () {
rb = GetComponent<Rigidbody> ();
}
// Update is called once per frame
void Update () {
}
public void moveLeft() {
rb.AddForce (-transform.right * thrust);
}
public void moveRight() {
rb.AddForce (transform.right * thrust);
}
public void stopMoving() {
rb.velocity = new Vector3 (0, 0, 0);
}
public void sceneReset() {
SceneManager.LoadScene (1);
}
}
If the player is Rigidbody and kinematic then the controls don't work but the ball bounces perfectly. if the player is not kinematic, then when the ball hits the player, it stops or starts moving in a straight line.
Note that the ball has a rigidbody (not kinematic) and a bounce material which has bounce: 1, friction:0.
What do I do so that I am able to move the player as well as keep the ball bouncing properly (like in airhockey).
Thanks in advance.