- Home /
My Obstacle Rotations Are Not Correct
Hello,
I have these giant meatballs that I want to rotate as they come down my plane; think of a meatball rolling down a mountain, rolling straight at you.
I have this script I wrote that I believe should be working correctly and doing what I want, but it is not.. Here it is:
using UnityEngine;
using System.Collections;
public class MeatballMover : MonoBehaviour {
private Rigidbody rb; //Reference to Rigidbody Component
public float speed; //Speed, updated through script
public float acceleration; //Every second, the speed will increase by this much
public float spinSpeed; // speed at how fast I want the meatball to rotate
//Executes once, when object is spawned / scene loaded
void Start() {
//Get reference to rigidbody, and set the speed
rb = GetComponent<Rigidbody> ();
rb.velocity = -transform.right * speed;
}
//Executes every frame
void Update() {
// Rotate the meatball to make it look like it is rolling down
transform.Rotate(Vector3.left, spinSpeed * Time.deltaTime);
//Add acceleration to speed, make sure it's not above topSpeed)
speed += Time.deltaTime * acceleration;
//Set object velocity
rb.velocity = -transform.forward * speed;
}
}
Instead of rotating in place, down my plane, they rotate around each other; almost like a black hole is in the middle and they go around it; or someone stuck an Axle in the middle and they are going around it. Can someone explain what is happening? Thank you
If you are using rigidbodies you shouldn't have to "fake" them rolling... you can simply apply a torque force which will make them rotate and the friction of rotating from the torque will move them like you would expect in real life.
http://docs.unity3d.com/ScriptReference/Rigidbody.AddTorque.html
Look into the two different Force$$anonymous$$ode's as you may want Force$$anonymous$$ode.Impulse ins$$anonymous$$d of the default.
I also tried using Torque, but to no avail :/.
I also played around with the pivot points and that did not solve the problem. :/ But I think I cracked the main problem... In my Update function, I do not think what I wrote on line 24 is getting along with my code on lines 27 and 29. I know this because I did some trials on the same Prefab but I did not attach the $$anonymous$$eatball$$anonymous$$over Script. I am sure it is the logic of my rigidbody in my update function. Do you have any idea on how to combine my update function so as to make everything "get along".? I know that is a complete new question in itself, so I may have to post it again. Thank you! :)