# Rigidbody movement along a Circular Path.

Hello, I am in need of some assistance with my code and am hoping somebody can help.

I am looking to create a game in which a ball move along a circular path as shown in this video - YouTube, however require the ball to move realistically.

I have created 2 scripts that move the ball so far, the first of which moves the rigidbody using addforce however, it only moves along one axis (no rotation).

The second script rotates the ball round an origin point however does not roll.

I need to combine the two scripts so the ball acts realistically as it rotates around the centre point.

Script 1

using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour
{

``````public float speed;
public float jumpHeight;
public Transform centre;
bool IsFalling = false;

void FixedUpdate()
{
//Ball Movement
float moveHorizontal = Input.GetAxis ("Horizontal");
Vector3 movement = new Vector3 (moveHorizontal, 0, 0);

rigidbody.AddForce (movement * speed * Time.deltaTime);

//Ball Jump
if (Input.GetKeyDown (KeyCode.W) && IsFalling == false)
{
rigidbody.velocity += Vector3.up * jumpHeight;
IsFalling = true;
}
}

void OnCollisionStay()
{
IsFalling = false;
}
``````

}

Script 2

``````using UnityEngine;
using System.Collections;

public class PlayerController2 : MonoBehaviour
{
Quaternion rotation;
public Transform centre;

public Vector3 playerRadius = new Vector3(0, 0.5f, -5);
float currentRotation = 0.0f;

void  Update ()
{
currentRotation += Input.GetAxis("Horizontal")*Time.deltaTime*100;
rotation.eulerAngles = new Vector3(0, currentRotation, 0);
Vector3 worldLookDirection = centre.position - transform.position;
Vector3 localLookDirection = transform.InverseTransformDirection(worldLookDirection);
localLookDirection.y = 0;
transform.forward = transform.rotation * localLookDirection;

transform.eulerAngles = new Vector3(0, currentRotation, 0);
}

}
``````

Thankyou.

Should work if you force the direction of the ball’s speed to be perpendicular to a vector coming from the center of the Tower in the update method.

I was able to get this result:

1 (at one point I’m changing the radius on the fly in case you wonder what’s happening)

With this code:

``````using UnityEngine;
using System.Collections;

public class Vects : MonoBehaviour
{
public float _StartAltitude;

public Rigidbody _Ball;
public Transform _Cylinder;

// Use this for initialization
void Start ()
{
_Cylinder.position = Vector.zero;
_Ball.transform.position = new Vector3(_Radius, _StartAltitude, 0f);
}

void Update()
{
Vector3 v = _Ball.velocity;
// any vecotr from cylinders up axis to ball pos
Vector3 radius = _Ball.transform.position - _Cylinder.transform.position;

if (v.x == 0 && v.z == 0)
{
// ball only bouncing up & down. Can't calculate tangetial speed
}
else
{
// a vector perpendicular to both of those
// (a vector that would be an acceptable velocity for the ball sans the vertical component)

// store the magnitude so we don't lose momentum when changing direction of speed
float mag = _Ball.velocity.magnitude;

// new speed is the old velocity projected on a plane defined by "up" and "tangent"
Vector3 newVelo = (Vector3.Project(v, tangent) + Vector3.Project(v, Vector3.up)).normalized * mag;

_Ball.rigidbody.velocity = newVelo;
}
// set the ball to the correct distance from the cylinder axis (assuming the vertical axis of cylinder is at X==0 && Z==0)