Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Nirish-Alakhramsing · Oct 20, 2014 at 06:12 PM · rotationrollcylinder

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;
                 }
         }
 }
 


Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Nirish-Alakhramsing · Oct 20, 2014 at 06:37 PM 0
Share

A video upload to get a better picture of what im trying to do

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Nirish-Alakhramsing · Oct 21, 2014 at 10:44 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges