- Home /
How to make a ball roll by pushing it with a cube?
Hello,
Here is the scenario. I made a plane with a ball on top and a cube. The cube I made controllable with the mouse x and y positions, and is a rigidbody as well as the ball. I wanted to be able to push the ball around with the cube which did work but what I'd like to happen is that the ball would keep rolling after I hit it with the cube and it doesn't do that. The ball is just pushed around by the cube but stops when the cube stops pushing. Help.
There are lots of possible causes.
$$anonymous$$ight be pushing it too slow.
The ball might not actually be rolling, just sliding.
$$anonymous$$ight be too much friction.
Hope this helps.
Answer by clunk47 · Dec 16, 2012 at 05:02 AM
You would want to add constant force to the sphere in the direction of the cube's velocity. So you want to be moving the cube with physics (force) when using the mouse axes. I wouldn't use transform.Translate or anything like that. Just test out this simple script I've wrote for your question to see if it get you what you want. Disable any code you're using now if any. Attach this C# script to the CUBE. Be sure to name the C# script EXAMPLE because that's what I have the class name as, which you could always change. I've also turned up the max angular velocity of all rigidbodies via this script, so the ball can actually "roll" and not slide.
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(Rigidbody))]
public class EXAMPLE : MonoBehaviour
{
float ZSpeed = 1.0f;
float XSpeed = 1.0f;
void Start()
{
rigidbody.freezeRotation = true;
rigidbody.drag = 5.0f;
Physics.maxAngularVelocity = 100.0f;
}
void Update()
{
rigidbody.AddForce(new Vector3(Input.GetAxis ("Mouse X") * XSpeed,
0, Input.GetAxis ("Mouse Y") * ZSpeed), ForceMode.Impulse);
}
void OnCollisionEnter(Collision other)
{
if(other.rigidbody)
{
if(!other.gameObject.GetComponent<ConstantForce>())
other.gameObject.AddComponent<ConstantForce>();
else
other.rigidbody.constantForce.force = rigidbody.velocity;
}
}
}
Your answer
Follow this Question
Related Questions
How do I move my object through physics with my keyboard? 1 Answer
How to make a ball that can roll? 2 Answers
Get a free moving ball. 1 Answer
Making a ball roll and keeping the axis? 2 Answers