- Home /
Cylinder object does not roll as intended
Hi, im working in a project to make some stuff for a maze game.
I have this cylinder primitive object with a sphere or capsule collider and a rigidbody attached. Also a script with speed settings. In this script it also says that if it detects collisions with certain objects, it will revert speed.
Even though it has a speed value set and a rigibody with gravity, it does not roll.
It should be rolling at a constant force forward and backwards if it collides. What can i do?
A video upload to get the picture
using UnityEngine;
using System.Collections;
public class SpikeWheelBehaviour : MonoBehaviour {
public float speed;
public GUIText displayText;
public bool changeDirection;
// Use this for initialization
void Start () {
changeDirection = false;
displayText.text = " ";
}
void OnCollisionEnter(Collision other){
if (other.gameObject.name == "OWwest01" ||
other.gameObject.name == "OWsouth01" ||
other.gameObject.name == "SpikeWheelX" ||
other.gameObject.name == "SpikeWheelZ" )
{
displayText.text = " Collided with an object ";
changeDirection = false;
moveForward();
}
else {
displayText.text = " No Collision ";
}
if (other.gameObject.name == "OtherWall01" ||
other.gameObject.name == "OWnorth01" ||
other.gameObject.name == "SpikeWheelX" ||
other.gameObject.name == "SpikeWheelZ" )
{
changeDirection = true;
moveForward ();
displayText.text = " Collided with an object ";
}
else {
displayText.text = " No Collision ";
}
}
// Update is called once per frame
void Update () {
moveForward ();
}
void moveForward(){
if (!changeDirection)
{
transform.Translate (Vector3.forward * speed * Time.deltaTime);
//changeDirection = true;
}
else
{
transform.Translate (-Vector3.forward * speed * Time.deltaTime);
//changeDirection = false;
}
}
}
Answer by ryanmillerca · Oct 20, 2014 at 06:52 PM
Try using rigidbody.AddForce() instead of transform.Translate(). That way you're pushing the cylinders with the physics engine instead of overpowering physics by changing it's transform directly.
Based on what I see in your video, built-in physics may not be the best choice for you. It's easier for you to use the animation system for this kind of consistent animation.
Thx for your answer. I tried addForce, but it did not work as expected. It kept adding force. I have no idea how animation works in here, im pretty new to program$$anonymous$$g in unity as wel as c#, so on that note i did not spend any time to learn it (yet). Ins$$anonymous$$d of that you made me re-evaluating my codework. I decided to start again from sratch and that worked.
The following code plus freezing transformation of the directions in position and rotation axis which are not needed, did the job.
public float localSpeed, worldSpeed;
public bool changeDirection;
private float freeze = 0.00f;
// Use this for initialization
void Start () {
changeDirection = false;
worldSpeed = 0.03f;
localSpeed = -30f;
}
void FixedUpdate(){
if (gameObject.name == "WheelZ") {
transform.Translate (freeze, freeze, worldSpeed, Space.World);
transform.Rotate (new Vector3 (freeze, localSpeed, freeze) * Time.deltaTime);
} else {
transform.Translate (worldSpeed, freeze, freeze, Space.World);
transform.Rotate (new Vector3 (freeze, localSpeed, freeze) * Time.deltaTime);
}
if (changeDirection) {
worldSpeed = worldSpeed *-1f;
localSpeed = localSpeed *-1f;
changeDirection =false;
} else {
}
}
void OnCollisionEnter(Collision other){
if (other.gameObject.name == "Wall" ||
other.gameObject.name == "WheelZ" ||
other.gameObject.name == "WheelX") {
changeDirection = true;
}
}
Your answer
Follow this Question
Related Questions
Limited sideway roll 0 Answers
Rotate GameObjects to define a cylinder 1 Answer
Cylinder touch rotation 1 Answer
Rotation glitch with camera 1 Answer
Sphere rotation in moving direction 1 Answer