Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Morgan_DMG · Jan 07, 2015 at 01:54 AM · rotationmovementrigidbodykeyboard inputtransform.rotate

Accelerating in Turned Direction

I'm creating a simply space navigation game, but I'm having a problem with "turning and burning."

As of right now, my craft accelerates and decelerates, strafes both left and right, elevates and descends, and rotates. I even have a particle effect working on key presses only.

Here's my problem: you cannot rotate the craft to change your trajectory. In other words, say you were facing and heading towards an asteroid and you see a space dock to your left. You rotate the craft left to face and head towards the space dock, but when you hit the accelerator the craft continues to speed towards the asteroid instead of the direction you're facing currently. I've tried so many different options with MoveRotation, transforms, rigidbodies and so forth, but have yet to fix this seemingly simple issue. If you can help me find a simple solution for this issue I, and I'm sure so many others, would be very grateful. Here's my scripting for the movement; Q and E are being used to initiate the rotation:

Thanks, ~Morgan

===================================================================================

 using UnityEngine;
 using System.Collections;
 
 public class MovementScript : MonoBehaviour 
 {
     public ParticleEmitter boosters;
 
     void Start()
     {
         rigidbody.rotation = Quaternion.identity;
     }
 
     void FixedUpdate()
     {
         if(Input.GetKey(KeyCode.W))
         {
             rigidbody.AddForce(Vector3.forward * 1);
             boosters.particleEmitter.emit = true;
         }
 
         if(Input.GetKey(KeyCode.S))
         {
             rigidbody.AddForce(Vector3.back * 1);
             boosters.particleEmitter.emit = true;
         }
 
         if(Input.GetKey(KeyCode.A))
         {
             rigidbody.AddForce(Vector3.left * 1);
             boosters.particleEmitter.emit = true;
         }
 
         if(Input.GetKey(KeyCode.D))
         {
             rigidbody.AddForce(Vector3.right * 1);
             boosters.particleEmitter.emit = true;
         }
 
         if(Input.GetKey(KeyCode.Q))
         {
             transform.Rotate(Vector3.forward * 1);
             boosters.particleEmitter.emit = true;
         }
 
         if(Input.GetKey(KeyCode.E))
         {
             transform.Rotate(Vector3.back * 1);
 
         }
 
         if(Input.GetMouseButton(0))
         {
             rigidbody.AddForce(Vector3.up * 1);
             boosters.particleEmitter.emit = true;
         }
 
         if(Input.GetMouseButton(1))
         {
             rigidbody.AddForce(Vector3.down * 1);
             boosters.particleEmitter.emit = true;
         }
     }//END FixedUpDate()
 }
 
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 bodec · Jan 07, 2015 at 02:27 AM 0
Share

few things you can do is use some code like `

       if(Input.GetAxis("Vertical")){
     rigidbody.AddForce(Vector3.forward * Input.GetAxis("Vertical) *1)
     }

this will save a bunch of lines of code as it takes vertical as a number from -1 to 1 and then multiplys it

when you hit the forward button you are working with world space so vector3.forward is always the same direction if you are sitting still and rotate then hit forward you should start going sideways

I know what is happening and why which is you are operating in world space not local space this is a problem I have had many times and I can't remember the fix as I normally work with character controllers. but I use this line of code for rotating

 transform.Rotate (0,hmove * rotSpeed * Time.deltaTime,0);

hmove is a Var that holds Input.GetAxis("Horizontal")

avatar image Morgan_DMG · Jan 07, 2015 at 02:55 AM 0
Share

When I changed the line to: if(Input.GetAxis("Vertical")) { yadda yadda yadda... }

It gives me the error of "cannot implicitly convert float to bool"

Okay, figured that part out; the line you gave SHOULD be something like this: if(Input.GetAxis("Vertical") > 0)...

Unfortunately, it didn't fix the issue so I'm still at square 1.

avatar image bodec · Jan 07, 2015 at 03:16 AM 0
Share

and you will everytime not thinking it all the way out when I typed that a IF ask if its true or not after cashing a couple Var to hold what you are getting from the inputs you can place a section of code like this

 if(v$$anonymous$$ove > 0f || v$$anonymous$$ove < 0f){
   curSpeed = maxSpeed * Input.GetAxisRaw("Vertical");
  
  transform.Translate(Vector3.forward *curSpeed*           Time.deltaTime);
 }
avatar image bodec · Jan 07, 2015 at 03:18 AM 0
Share

I used a raw axis as I wanted to know if they where pushing it or not didn't care abount numbers inbetween the final line reads transform.Translate(Vector3.forward * (either 1 or -1) time time.deltatime)

avatar image Morgan_DMG · Jan 07, 2015 at 04:00 AM 0
Share

Thank you, bodec for your time and your suggestions. It sucks that they didn't fix the problem, but I really do appreciate your feedback all the same!

2 Replies

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

Answer by jpthek9 · Jan 07, 2015 at 03:27 AM

Instead of using Vector3.direction which returns the global right, you can use transform.direction.

I.e.

  if(Input.GetKey(KeyCode.W))
      {
              rigidbody.AddForce(transform.forward * 1);
              boosters.particleEmitter.emit = true;
      }
  
      if(Input.GetKey(KeyCode.D))
      {
              rigidbody.AddForce(transform.right * 1);
              boosters.particleEmitter.emit = true;
      }
Comment
Add comment · Show 1 · 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 Morgan_DMG · Jan 07, 2015 at 03:57 AM 0
Share

This worked like a charm for what I was trying to do. I guess I was making it too complicated for my own good, but you were right. Changing it from Vector3 to transform.directional commands was the right move to make. Now, my little space station glides about in the open environment as if it were a hockey puck on ice lol. Thanks for the simplistic suggestion! Simple is always best anyways!

~$$anonymous$$organ

avatar image
0

Answer by DwaynePritchett · Jan 07, 2015 at 05:29 AM

Vector3.Forward is in Global coordinates. Have you tried multiplying the rotation by the forward Vector? You can multiply Quaternions and Vectors, but on of them has to be on the left. I think it's Quaternion*Vector. But, try if(Pushed.W){ addForce(transform.rotation * forwardVector); }

Something like that should work. Sorry, I can't actually write it at the moment to double check.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Help with translation and rotation on a rigid body 2D 0 Answers

Rigidbody.AddForce and incorrect rotation 2 Answers

Add Force to the right of the rigidbody, not right of the screen 1 Answer

Stop A player Turning at Specific Point. 1 Answer

How to make a RigidBody not go into ground when tilted foward? 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