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 $$anonymous$$ · Dec 24, 2012 at 12:13 AM · random

Random movement on a plain

Hi all,

I'm new to Unity and just started exploring its functions. As a test I wanted to make a simple scene with a plain and a cylinder hoovering above the plain.

The idea was to let the cylinder fall and when it hits the ground for it to move forward randomly.

I setup the scene, put the cylinder (with Rigidbody, Capsule Collider and Character Controler) and wrote a small script but it's not working...

What am I (obviously) doing wrong?

 var Speed : float = 1.0;
 var rotateSpeed : float = 10.0;
 var gravity : float = 9.8;
 
 private var moveDirection : Vector3 = Vector3.zero;
 private var curDir = 1;
 
 function Start () {
 
 }
 
 function Update () {
     var controller: CharacterController = GetComponent(CharacterController);
 
     if( controller.isGrounded ) {
         // Rotate around y - axis
         if (Random.value < 0.05f){
               curDir = -curDir;
         }
         transform.Rotate(Vector3.up * curDir * rotateSpeed);
          
         // Move forward object
         moveDirection = Speed * transform.TransformDirection(Vector3.forward);
     }
     
     // Apply gravity
     moveDirection.y -= gravity;
     
     controller.Move(moveDirection * Time.deltaTime);
 }
 
 @script RequireComponent(CharacterController)
Comment
Add comment · Show 2
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 Statement · Dec 24, 2012 at 12:38 AM 0
Share

Do you get any errors in the console log?

avatar image $$anonymous$$ · Dec 24, 2012 at 09:16 PM 0
Share

nop... nothing on the console.

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by aldonaletto · Dec 24, 2012 at 12:50 AM

There are some weird things in your script:
1- Don't add CharacterController and Rigidbody to the same object, unless the Rigidbody is kinematic - the object may show an absolutely weird behaviour. You should use only the CharacterController and add gravity by script.
2- You're using two Move instructions in Update: this will double the actual speed in this case.
3- The chance to flip direction is 50%, thus the rotation may be reversed almost each frame, producing barely noticeable results. You could change the code a little to reduce the chances of flipping direction to about 5%, for instance.

 var speed : float = 1.0;
 var rotateSpeed : float = 20.0; // rotate speed in degrees per second
 var gravity : float = 10.0;
 
 private var moveDirection : Vector3 = Vector3.zero;
 private var curDir = 1;
 
 function Update () {
   var controller: CharacterController = GetComponent(CharacterController);
   if ( controller.isGrounded ) {
     if (Random.value < 0.05f){ // if random value < 5/100...
       curDir = -curDir; // flip direction
     }
     // Rotate around y - axis
     transform.Rotate(Vector3.up * curDir * rotateSpeed * Time.deltaTime);
     moveDirection = speed * transform.TransformDirection(Vector3.forward);
   }
   moveDirection.y -= gravity * Time.deltaTime; // apply gravity
   controller.Move(moveDirection * Time.deltaTime);
 }
Comment
Add comment · Show 2 · 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 $$anonymous$$ · Dec 24, 2012 at 03:32 AM 0
Share

Thanks for your help. It did improve a bit but is not doing what I intend. Right now gravity works fine (I removed rigidbody and the collider) but the cylinder just moves back and forth and does not rotate...

However, if I change the form to a cube it works!! Strange...

avatar image aldonaletto · Dec 25, 2012 at 09:04 AM 0
Share

There was an error in my answer: the line where controller is assigned should be the first one in Update, but it was the second to last. I fixed the answer and tested it, and everything worked fine. I suspect that you may be another victim of the Inspector's elephant memory: change rotateSpeed in the Inspector to 10 or 20 - it was originally 1, and modifying it in the script has no effect because the Inspector always initialize it to the last value saved

avatar image
0

Answer by thefrontback · Dec 13, 2019 at 08:33 PM

what would this look like in C#

Comment
Add comment · 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

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

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Function action applies to every object that have script with that function on it. Help please 1 Answer

Z rotation locks to certain value randomly? 0 Answers

GUI Menu After every round how do I display a random image? 1 Answer

Random Drops 2 Answers

For Loop isn't working properly! 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