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 sam32x · Nov 08, 2012 at 12:09 PM · transformrotatesomething

object rotates when moving backward

whenever i press s to move my vehicle backwards it also rotates it to the right, this is a script i made ages ago when i was new to unity and i cant see anything wrong.

 var weapon = 0;
 var m = 1;
 var f = 2;
 var c = 3;
 var machinegun1 : GameObject;
 var machinegun2 : GameObject;
 var flamethrower : GameObject;
 var cannon: GameObject;
 var tractor1cube : GameObject;
 var forwards : float = 0;
 var turn : float = 0;
 var maxSpeed = 8;
 var minSpeed = -6;
 var maxTurn = 135;
 
 function Update () {
 maxTurn = forwards / 6 * 135 + 20;
 if (weapon >= 1)
 Invoke("WeaponSelect",0);
 
 if (weapon >= 1){
  transform.Rotate(Vector3.up * Time.deltaTime * turn);
  }
 
 if (weapon >= 1){
   transform.Translate(Vector3.left * Time.deltaTime * forwards);
   }
   
   if(turn >= 0){
   turn -= 3.25;
   }
   
   if(turn <= 0){
   turn += 3.25;
   }
   
   if(forwards <= 0){
   forwards += 0.2;
   }
   
   if(forwards >= 0){
   forwards -= 0.1;
   }
   
   if(forwards >= maxSpeed){
   forwards = maxSpeed;
   }
   
   if(forwards <= minSpeed){
   forwards = minSpeed;
   }
   
   if(turn >= maxTurn){
   turn = maxTurn;
   }
   
   if(turn <= -maxTurn){
   turn = -maxTurn;
   }
 
 if (Input.GetKey("a")) {
 turn -= 7.5;
 if(forwards >= maxSpeed / 2){
 transform.Translate(Vector3.forward * Time.deltaTime * turn / 50);
 }
 if(forwards >= maxSpeed * 0.75){
 forwards -= 0.6;
 }
 }
 
 if(Input.GetKey("d")) {
 turn += 7.5;
 if(forwards >= maxSpeed / 2){
 transform.Translate(Vector3.forward * Time.deltaTime * -turn / 50);
 }
 if(forwards >= maxSpeed * 0.75){
 forwards -= 0.6;
 }
 }
 
 if (Input.GetKey("w")) {
 forwards += 0.3;
 }
 
 if (Input.GetKey("s")) {
 forwards -= 0.5;
 }
 }
 
 function OnGUI () {
     if (weapon < 1){
     if (GUI.Button (Rect (Screen.width * 0.02,60,160,120), "Guns")) {
         weapon = m;
         Destroy (tractor1cube);
         }
         }
         if (weapon < 1){
     if (GUI.Button (Rect (Screen.width * 0.02,220,160,120), "Flamethrower")) {
         weapon = f;
         Destroy (tractor1cube);
     }
     }
     if (weapon < 1){
     if (GUI.Button (Rect (Screen.width * 0.02,380,160,120), "Cannon")) {
         weapon = c;
         Destroy (tractor1cube);
     }
     }
 }
 
 function WeaponSelect() { 
 if (weapon != m)
 Destroy (machinegun1);
 
 if (weapon != m)
 Destroy (machinegun2);
 
 if (weapon != f)
 Destroy (flamethrower);
 
 if (weapon != c)
 Destroy (cannon);
 }

also with the

 if(forwards >= maxSpeed / 2){
     transform.Translate(Vector3.forward * Time.deltaTime * turn / 50);
     }


part if i rotate it right it drifts around a point in front of it but if i rotate left it drifts around a point behind it, does anyone know why or how to fix it?

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 sparkzbarca · Nov 10, 2012 at 09:30 AM

what happens if there going slow (less than half speed)

if (Input.GetKey("a")) { turn -= 7.5; if(forwards >= maxSpeed / 2){ transform.Translate(Vector3.forward Time.deltaTime turn / 50); }

what happens if forwards is less than maxspeed/2 apparently slow moving tanks dont turn?

the positive and negative sign probably change the vector to point in front or behind changing what your rotating around.

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

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

Moving objects in game using .transform does not stop 3 Answers

rotare arrow object to mouse position 0 Answers

rot not working for 2nd prefab 2 Answers

object rotating around parent 1 Answer

can anyone tell me why this doesn't work? 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