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 Truger · Feb 23, 2014 at 06:40 PM · rotationmovement

Rotating a object towards a direction then move it

Hi normally just searching the forums / unity docs or googling around can solve my programming issues but i've been stuck for about the last month and i'm on the fourth attempt at making a basic tank controller with each attempt i'm getting closer and closer but right now I am just stuck.

My problem isn't with moving it but making it rotate towards the direction the player whats to travel then moving. I provided two videos to show what im trying to accomplish

What I want http://youtu.be/9zrxS0LcnRo

What I currently have http://youtu.be/8FSPA7863-c

Currently it rotates but doesn't stop when it faces forward and so and endless rotation occurs until you stop pressing forward

 public class attempt4 : MonoBehaviour 
 {
     
     Quaternion qtarget;    //quartnon target ie rotation
     public Transform waypoint; //the cube in front of the tank
     public Transform Turrent_pos;    //the top turrent of the tank
     public float rspeed = 10;    //turn speed of tank
     
     public float speed = 8;    //forward speed
 
     public bool tankBody = false;
     
     void LateUpdate() 
     {
         //Quaternion deltaRotation = Quaternion.Euler(waypoint.transform.position-transform.position);
         //rigidbody.MoveRotation(Quaternion.Slerp(rigidbody.rotation, deltaRotation, rspeed * Time.deltaTime));
 
         qtarget = Quaternion.LookRotation(waypoint.transform.position-transform.position);    //non ridgid body quatornon
 
         
         // based on input, determine requested action
         if (Input.GetAxis("Vertical") > 0) 
         { 
             // requesting forward
             transform.rotation = Quaternion.Slerp(transform.rotation, qtarget, rspeed * Time.deltaTime);    //this rotates the object towards the waypoint(direction camera is facing)
 
             transform.position+= (transform.TransformDirection(Vector3.forward) * speed * Time.deltaTime);    //this rotates the object to the direction we need to travel multiply by speed, progressed by time    
         } 
         else if (Input.GetAxis("Vertical") < 0) 
         {
             // requesting reverse
             transform.rotation = Quaternion.Slerp(transform.rotation, qtarget, rspeed * Time.deltaTime);
             
             transform.position-= (transform.TransformDirection(Vector3.forward) * speed * Time.deltaTime);
             
         }
     }
 }
Comment
Add comment · Show 5
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 locrian05 · Feb 23, 2014 at 07:04 PM 0
Share

try messing around with this transform.LookAt(target);

avatar image Truger · Feb 23, 2014 at 08:13 PM 0
Share

Is there anyway i could make it wait until the tank is relatively facing forward and then allow it to move?

avatar image locrian05 · Feb 23, 2014 at 09:17 PM 1
Share

try setting some condition like

 var turretIsRotating : boolean;
 
 //then something like
 
 if(turretIsRotating) {
    //your code here
 }else{
 }

using boolean can help you a lot in tricky situations.

avatar image locrian05 · Feb 23, 2014 at 09:18 PM 0
Share

sorry i used javascript.. but you get my point.

avatar image Truger · Feb 23, 2014 at 11:21 PM 0
Share

Thanks for the help, I got it working I used rotatetowards ins$$anonymous$$d of LookAt which is a close secound in combination with the booleans

0 Replies

· Add your reply
  • Sort: 

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

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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

(sprite2D) Movement like this game... 1 Answer

Rotation that is dependent on button presses? 1 Answer

Rotation and movement applied to other objects 2 Answers

Character rotation help needed. 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