# Unable to rotate a 2D object, wanting to move it like a ball/wheel

I'm using Unity2D.

I have a very basic 2D space with a platform, a ball and 2D physics.

Physics is being applied and ball sits on top of the platform.

However, it will not spin.

Below is the script attached to the ball:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public class Circle : MonoBehaviour
{
private Vector3 screenPoint;
private Vector3 offset;
private float speed = 10f;
public Rigidbody rb;

void Start()
{
rb = GetComponent<Rigidbody>();
}

void FixedUpdate()
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");

Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);

rb.maxAngularVelocity = 999;
}

void OnMouseDown()
{
offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
}

void OnMouseDrag()
{
Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
transform.position = curPosition;
}
}


The script is attached to the ball, I know this because I'm able to drag it (see OnMouseDown).

I have also tried with other objects such as a rounded-corner square, but I just can't get any spin or torque to be applied.

Below is a screenshot of the inspector on the element. Circle.cs (the script) is the above pasted script.

Any help appreciated.

• You say you're using 2D physics, but it looks like you're using the 3D version of the Rigidbody, instead of Rigidbody2D. Is this deliberate? – DMGregory Aug 23 '18 at 12:25
• It's not deliberate no, but that explains a lot. I have added Rigidbody2D in Unity UI, but am I referencing a 3D one in the code? I can't see any difference in the docs, but have a feeling I'm looking in the wrong place? – mikemike Aug 23 '18 at 16:07
• They're two different components, Rigidbody, and Rigidbody2D. They expose similar capabilities, but work through two different physics systems (PhysX & Box2D), so you can't substitute one for the other. Can you show us in the editor view how you've set up your object? – DMGregory Aug 23 '18 at 16:13
• Sure @DMGregory I have just added it to my question. Looking at docs, although they're different, the class names within C# are the same. Hoping I'm wrong here though, as that would be the likely cause of the issue. I am using Rigidbody2D though – mikemike Aug 23 '18 at 16:59
• Totally see the issue now. It is a different class name. I need to reference Rigidbody2D in GetComponent. Thanks – mikemike Aug 23 '18 at 17:04

I need to reference Rigidbody2D instead of Rigidbody.

Code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public class Circle : MonoBehaviour
{
private Vector3 screenPoint;
private Vector3 offset;
private float speed = 10f;
public Rigidbody2D rb;

void Start()
{
rb = GetComponent<Rigidbody2D>();
}

void FixedUpdate()
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");

Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);

}

void OnMouseDown()
{
offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
}

void OnMouseDrag()
{
Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
transform.position = curPosition;
}
}