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);
transform.position = rotation * playerRadius;
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.