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 Flight King · Mar 15, 2013 at 12:49 AM · rotationjavascriptrigidbodyraycastlimit

Need help with limiting rotation.

I'm brand new to unity and scripting and I'm working on a vehicle in unity that will be able to avoid anything in it's way. It's going to be pretty simple. I want it to drive forward, stop if it gets too close to a wall, then decide if it should turn right or left. It uses ray-casting to check for obstacles. The car body is a simple object I made in Blender and it is connected to cylinders with hinge joints for the wheels. Both the body and the wheels are rigidbodys. At the moment it drives forward just like I want it to, then it stops when it comes to a wall, then it sends rays out the sides to see if there is a wall on the right or left. If there is, it will start rotating away from the wall. My problem is I want to make it stop rotating when it was turn X amount of degrees. Anyone that knows how to do this please help. Below is a youtube link for a video showing what the vehicle does at the moment.

http://youtu.be/NWDuYYkna1k

Here is the script below for those that need to see it. By the way, it's a JavaScript.

 var mode : String = "nothing";
 var force : float = 10;
 var turnpower : float = 10;
 
 
 function Start()
 {
     yield(WaitForSeconds(3));
     mode = "gofwd";
 }
 
 
 function Update()
 {
     var fwd = transform.TransformDirection(Vector3.forward);
     
     Debug.DrawRay(transform.position, fwd *15, Color.yellow);
     
     if(mode == "gofwd")
     {
         rigidbody.AddForce(0,0,force);
     }
     
     if(Physics.Raycast(transform.position, fwd, 15) && mode == "gofwd")
     {
         mode = "stop";
     }
     
     if(mode == "stop")
     {
         Turn();
     }
     
     if(mode == "rightorleft")
     {
         var left = transform.TransformDirection(Vector3.left);
         var right = transform.TransformDirection(Vector3.right);
         
         Debug.DrawRay(transform.position, left *40, Color.red);
         Debug.DrawRay(transform.position, right *40, Color.green);
         
         if(Physics.Raycast(transform.position, left, 40))
         {
             rigidbody.AddTorque (0, turnpower, 0);
         }
     }
 }
 
 
 function Turn()
 {
     yield(WaitForSeconds(3));
     
     mode = "rightorleft";
 }


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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by robertbu · Mar 15, 2013 at 02:31 AM

There are a couple of ways to approach this problem. The simplest is probably to just use an absolute angle;

    var destAngle : float;  // At the top of the file;

This calcualtes a 45 degree right turn

    destAngle = Atan2(transform.forward.z, transform.forward.x) * Mathf.Rad2Deg;
    destAngle = destAngle - 45.0;

   

And this is how you might check it in Update()

 var currAngle = Atan2(transform.forward.z, transform.forward.x) * Mathf.Rad2Deg); 
 if (Mathf.Abs(Mathf.DeltaAngle(currAngle, destAngle) < threshold)) {
   ; // Code here to stop the turn;
 }

Note, looking at your code, just because you are not still applying torque, doesn't mean the car will straighten out. You will have to counteract the torque somehow...friction, drag, counter force...

Also you have to be careful about how you set threshold. It needs to be big enough so that a fps drop will not allow the car to skip on by and keep rotation another 360 degrees. As an alternative, you could save the previous angle from the previous frame and check to make sure it is decreasing. You would bail out of turning when the angle started climbing again. Note this might also cause the car to stop turning if it collided with something that effected his angular velocity.

Comment
Add comment · Show 4 · 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 Flight King · Mar 15, 2013 at 11:58 PM 0
Share

Okay I added everything you said to add and I get these three errors. alt text

capture.jpg (46.8 kB)
avatar image robertbu · Mar 16, 2013 at 01:11 AM 0
Share

I had a couple of typos. Corrected code below. You need to define a variable called 'threshold' with a meaningful threshold for your situation (or hard code a value). You want it small enough to do the job but large enough so that if the frame rate drops, the calculation won't skip on by the check.

 var currAngle = $$anonymous$$athf.Atan2(transform.forward.z, transform.forward.x) * $$anonymous$$athf.Rad2Deg; 
 if ($$anonymous$$athf.Abs($$anonymous$$athf.DeltaAngle(currAngle, destAngle)) < threshold) {
   ; // Code here to stop the turn;
 }
avatar image Flight King · Mar 19, 2013 at 08:36 PM 0
Share

Okay I've been experimenting with the code because there are no errors anymore but I'm not getting the desired result. Here is the code so you can look over in just to make sure I have everything in the right place. I've made a print command where I will code later to stop the turn to make sure it's working right. If I set the threshold to a positive number it gives me the print message as soon as it starts turning, or as soon as it goes to "rightorleft" mode. If I set the threshold to a negative number it does not give me the message at all.

 var mode : String = "nothing";
 var force : float = 10;
 var turnpower : float = 10;
 var destAngle : float;
 
 
 function Start()
 {
     yield(WaitForSeconds(3));
     mode = "gofwd";
 }
 
 
 function Update()
 {
     var fwd = transform.TransformDirection(Vector3.forward);
     
     Debug.DrawRay(transform.position, fwd *15, Color.yellow);
     
     if(mode == "gofwd")
     {
         rigidbody.AddForce(0,0,force);
     }
     
     if(Physics.Raycast(transform.position, fwd, 15) && mode == "gofwd")
     {
         mode = "stop";
     }
     
     if(mode == "stop")
     {
         Turn();
     }
     
     if(mode == "rightorleft")
     {
         var left = transform.TransformDirection(Vector3.left);
         var right = transform.TransformDirection(Vector3.right);
         
         Debug.DrawRay(transform.position, left *40, Color.red);
         Debug.DrawRay(transform.position, right *40, Color.green);
         
         if(Physics.Raycast(transform.position, left, 40))
         {
             var currAngle = $$anonymous$$athf.Atan2(transform.forward.z, transform.forward.x) * $$anonymous$$athf.Rad2Deg;
             destAngle = destAngle - 45.0;
             
             rigidbody.AddTorque (0, turnpower, 0);
             
             var threshold : float = -1000;
             
             if ($$anonymous$$athf.Abs($$anonymous$$athf.DeltaAngle(currAngle, destAngle)) < threshold) 
             {
                 print("Okay. Stop turning now.");
             }
         }
     }
 }
 
 
 function Turn()
 {
     yield(WaitForSeconds(2));
     
     mode = "rightorleft";
 }
avatar image robertbu · Mar 20, 2013 at 02:02 AM 0
Share

As a start on line 45, you need to assign to destAngle, not currAngle. Or you could rewrite the next line to be:

 destAngle = currAngle - 45;

'threshold' will be a positive number.

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

10 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

Related Questions

Preventing rotation of rigidbody at a specific rotation 0 Answers

Aligning player to surface 2 Answers

Euler angle problems with rotation limit script 3 Answers

Trouble with RayCast, rigid body and the colliders tag 0 Answers

How can I set a limit to my object rotation???(Here's the code) 2 Answers


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