Hi i am making a small game for a university project. I have a terrain from cinema 4d which has a winding u-shaped groove and is tilted to make it into a slope. It has a mesh collider on it and a mesh render.
I have created a sphere and put it near the top of the slope. The idea is that the ball rolls down the hill because of gravity but you can control it left and right.
I have read through 100s of the posts and finally got my ball to roll down the hill but I cannot control it left or right. I have a script attached to the sphere which is this:
var speed = 10.0;
var gravitypull = 0;
function FixedUpdate() {
rigidbody.AddForce (Vector3(0, gravitypull, 0));
if (Input.GetKey (“a”)) {
rigidbody.AddRelativeTorque(right * speed * Time.deltaTime);
}
if (Input.GetKey (“d”)) {
rigidbody.AddRelativeTorque(right * -speed * Time.deltaTime);
}
}
The sphere also has a Mesh filter, sphere collider, mesh render and rigidbody.
The sphere also moves slowly and does not gain speed as I wish it to as the slope varies in degree.
I have a working countdown timer as eventually I want to collect things to give time bonuses and take time away.
Any help would be appreciated as I have been working on this for 2 months and its due in soon and im really struggling!
When you say nothing is happening, do you mean the ball isn’t turning left and right or that it isn’t moving at all? The torque you are using seems OK, but I would recommend you use Input.GetAxis to get the keystrokes. If the ball appears to rotate, but doesn’t roll, it might be that you need a physic material with higher friction on the collider.
Hey there - this was a problem that I ran into a while ago while making quite a similar thing.
I think your problem is that you’re using torque - though it might seem correct, what it’s really doing is spinning the ball, and the ball may not be making enough surface contact to actually change its path, no matter what the friction settings.
Here’s what I did, and it turned out pretty well:
-instead of using AddTorque, use AddForce with a force perpendicular to the camera forward direction!
So, something like
var tmpLeft = Camera.main.transform.right * speed;
rigidbody.AddForce(tmpLeft);
should work quite well and have your ball strafing to the left and the right, which is what I assume you want…
And of course, you can still add some torque for a spinning effect if you want, but it probably won’t change the actual motion a whole lot.
Oh game programming, such an art of smoke mirrors. :roll:
I don’t usually just give out code solutions in an attempt to make people learn, but you’re so close:
if(Input.GetKey(KeyCode.LeftArrow)) // if you're pressing left
{ // do what's inside these curly braces
var tmpLeft = Camera.main.transform.forward * speed; // make a variable for the force
rigidbody.AddForce(tmpLeft); // and add the force
}
if(Input.GetKey(KeyCode.RightArrow))
{
var tmpRight = Camera.main.transform.forward * speed;
rigidbody.AddForce(-tmpRight);
}
now if that doesn’t work, maybe your speed is too low and the mass is too high?
Thank you for that, i am now getting the errors:
unknown identifier speed, and
NullReferenceException: Object reference not set to an instance of an object (line 12)
sorry i rushed that. ive now corrected it to
var speed = 10.0;
var gravitypull = 0;
function FixedUpdate() {
rigidbody.AddForce (Vector3(0, gravitypull, 0));
if(Input.GetKey(KeyCode.LeftArrow)) // if you’re pressing left
{ // do what’s inside these curly braces
var tmpLeft = Camera.main.transform.forward * speed; // make a variable for the force
rigidbody.AddForce(tmpLeft); // and add the force
}
if(Input.GetKey(KeyCode.RightArrow))
{
var tmpRight = Camera.main.transform.forward * speed;
rigidbody.AddForce(-tmpRight);
}
but get the error object reference is not set to the instance of an objecy (line 12)
function FixedUpdate() {
rigidbody.AddForce (Vector3(0, gravitypull, 0));
}
if(Input.GetKey(KeyCode.LeftArrow)) // if you’re pressing left
{ // do what’s inside these curly braces
var tmpLeft = Camera.main.transform.forward * speed; // make a variable for the force
rigidbody.AddForce(tmpLeft); // and add the force
}
if(Input.GetKey(KeyCode.RightArrow))
{
var tmpRight = Camera.main.transform.forward * speed;
rigidbody.AddForce(-tmpRight);
}
thanks ive added it so there arent any errors. mass is 1, speed is 1000. still no left/right movement!!
function FixedUpdate() {
rigidbody.AddForce (Vector3(0, gravitypull, 0));
}
if(Input.GetKey(KeyCode.LeftArrow)) // if you’re pressing left
{ // do what’s inside these curly braces
var tmpLeft = Camera.main.transform.left * speed; // make a variable for the force
rigidbody.AddForce(tmpLeft); // and add the force
}
if(Input.GetKey(KeyCode.RightArrow))
{
var tmpRight = Camera.main.transform.right * speed;
rigidbody.AddForce(-tmpRight);
}
hahaha yeah that would make more sense. new error:
‘left’ is not a member of ‘UnityEngine.Transform’.