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 TomatoOrgyLT · Oct 16, 2013 at 04:47 PM · rotationrigidbodyspeedcoordinates

Changing speed and rotation of a rigidbody gets bugy

Hello everyone I have 2 rigidbodys with scripts attached. I made the first script to change the rotation of an object1 and the speed to negative(just because the controls would not go reverse) when the object1 X coordinates are bigger than object2 X coordinates. The problem is that when the object1 coordinates gets bigger than the object2, object1 gets stuck in that position and goes bugy. I don't understand why does that happen and I would really appreciate your help :)

The object1 move script:

 var object1 : Transform;
 var object2 : Transform;
 
 var Xspeed = 10.0;
 var Yspeed = 10.0;
 
 function Start() {
 
 }
 
 function FixedUpdate () {
 var x = Input.GetAxis("Horizontal") * Time.deltaTime * Xspeed;
 var y = Input.GetAxis("Vertical") * Time.deltaTime * Yspeed;
 transform.Translate(x, y, 0);
 
 if(object1.transform.position.x > object2.transform.position.x){
     transform.Rotate(new Vector3(0,180,0));
     Xspeed = -10.0;
     print("Rotating");
 }
 else{
     transform.Rotate(new Vector3(0,0,0));
     Xspeed = 10.0;
     print("RotatingBack");
 }
 }

The object2 move script:

 #pragma strict
 
 var speed = 10.0;
 
 function FixedUpdate () {
 var x = Input.GetAxis("Horizontal2") * Time.deltaTime * speed;
 var y = Input.GetAxis("Vertical2") * Time.deltaTime * speed;
 transform.Translate(x, y, 0);
 }
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

2 Replies

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

Answer by TomatoOrgyLT · Oct 18, 2013 at 08:48 PM

After hours of pain I finally did it :D All I had to do was to change the transform.Rotate(new Vector3(0,180,0)); into transform.rotation = Quaternion.Euler(0,180,0); Anyway here is the final script:

 private var fliped : boolean = false;
 
 function Start() {
 
 }
 
 function FixedUpdate () {
 var x = Input.GetAxis("Horizontal") * Time.deltaTime * Xspeed;
 var y = Input.GetAxis("Vertical") * Time.deltaTime * Yspeed;
 transform.Translate(x, y, 0);
 
 if(object1.transform.position.x > object2.transform.position.x){
 if(fliped == false){
  flip();
 }
 }
 if(object1.transform.position.x < object2.transform.position.x) {
 if(fliped == true){
  flip();
 }
 }
 }
 
 function flip()
 {
 if(fliped == false){
     transform.rotation = Quaternion.Euler(0,180,0);
     Xspeed = -10.0;
     fliped = true;
       print("Rotating");
     }
     else {
     transform.rotation = Quaternion.Euler(0,0,0);
     Xspeed = 10.0;
     fliped = false;
       print("RotatingBack");
       }
   }
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
avatar image
0

Answer by whydoidoit · Oct 16, 2013 at 04:53 PM

You need to change the rigidbody.rotation not the transform.rotation in FixedUpdate:

     rigidbody.MoveRotation(Quaternion.AngleAxis(180, Vector3.up) * rigidbody.rotation);


And in the second script you need to affect the rigidbody's position:

      rigidbody.MovePosition(rigidbody.position + rigidbody.rotation * new Vector3(x,y,0));
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 TomatoOrgyLT · Oct 16, 2013 at 05:04 PM 0
Share

I'm still getting the same result.

 if(object1.transform.position.x > object2.transform.position.x){
     rigidbody.$$anonymous$$oveRotation(Quaternion.AngleAxis(180, Vector3.up) * rigidbody.rotation);
     Xspeed = -10.0;
     print("Rotating");
 }
 else{
     rigidbody.$$anonymous$$oveRotation(Quaternion.AngleAxis(0, Vector3.up) * rigidbody.rotation);
     Xspeed = 10.0;
     print("RotatingBack");
 }

Second script

 function FixedUpdate () {
 var x = Input.GetAxis("Horizontal2") * Time.deltaTime * speed;
 var y = Input.GetAxis("Vertical2") * Time.deltaTime * speed;
 transform.Translate(x, y, 0);
 rigidbody.$$anonymous$$ovePosition(rigidbody.position + rigidbody.rotation * new Vector3(x,y,0));
 }
avatar image TomatoOrgyLT · Oct 16, 2013 at 05:13 PM 0
Share

I forgot to mention that when it gets stuck it looks like the object is rotating in both directions and after some random time it randomly decides in what direction it should be facing to.

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

15 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 avatar image avatar image avatar image avatar image

Related Questions

Why does my rigidbody slows down at time ? 1 Answer

Change movement speed based on rotation 0 Answers

How can I slow-stop the RigidBody Speed? 2 Answers

Rigidbody.AddForce with Raycast? 1 Answer

Detect if object with Hinge Joint is moving 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