Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 beelan · Jul 23, 2011 at 03:51 PM · transform.translatehoverstrafe

Midair Strafing

I'm making a flying game where the ship has hovering style controls. I want to have it be able to strafe left and right while hovering. The problem is, if I strafe left and right while the ship is banked (i.e. during a turn) then it strafes up or down as well. If I set the transform.translate to be relative to world space then it doesn't have that problem, but then it will always go left and right relative to the world and not the direction of the ship.

So the question is, how can I make the ship strafe right or left relative to the way it is facing while keeping it's height constant relative to the ground.

Note: This is not physics based. I am not using a rigidbody and I would prefer to keep it that way.

Any help would be greatly appreciated.

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

3 Replies

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

Answer by aldonaletto · Jul 23, 2011 at 04:47 PM

The best way to split the movements is to split the objects: make an empty object which will effectively turn, strafe and move forth/back, and child to it another object which will fake the banking. If it's a first person game, you should child the camera to the inner object; if the camera is external, child the 3d ship model to the inner object. You can place the control script in the external object, find the inner one with Find("innerName") and tilt it around Z when banking with localEulerAngles:

var bankSpeed: float = 2.0; // set the bank speed

function Update(){ var bankAng = 0.0; ... // do the main object movement, and set bankAng to the desired banking angle var inner:Transform = Find("Inner"); // get child object called "Inner" inner.localEulerAngles.z = Vector3.Lerp(inner.localEulerAngles.z, bankAng, Time.deltaTime * bankSpeed); }

EDITED: That's a simple example on how to bank in the correct order (avoid Rotate: it accumulates errors and the model may get more and more skewed):

function Update(){
    var v = Input.GetAxis("Vertical");
    var h = Input.GetAxis("Horizontal");
        // move the external object using v and h then do the banking:
    var inner = transform.Find("Inner");
    inner.localEulerAngles.x = v*15; // forth/back banking first!
    inner.localEulerAngles.z = -h*15;  // left/right
}
Comment
Add comment · Show 9 · 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 beelan · Jul 24, 2011 at 03:18 PM 0
Share

Thanks so much for your quick response!

I tried your solution Aldonaletto and I got it working. I thought of doing something like that, but I was hoping there was a simpler solution. I also want to implement a flight mode (not hovering) in which case the pitch of the ship will actually matter, but I guess I can retrieve the Euler Angles from the ship model and use that to deter$$anonymous$$e forward movement even if the inner is unchanged.

Thanks again! I've been stuck on this for a while.

avatar image aldonaletto · Jul 24, 2011 at 03:57 PM 0
Share

I suppose the pitch should be done by the external object, since it affects the direction of movement - unless it's a fake pitch, just to look down or up in a first person perspective, for instance.

avatar image beelan · Jul 25, 2011 at 12:53 AM 0
Share

So, since I got the strafing working I decided to get banking to work when moving forward or backwards as well (like a helicopter). I used the exact same solution, having the ship model tilt forward and back but the actual forward and backwards movement controlled by the external object. It works fine but I've discovered that if I combine the two forms of banking, ie left or right while tilted forward or back, then it causes the forward direction of the ship model vs. the external object to become skewed. This means that forward and backwards motion from the external object no longer match the forward direction of the ship model.

Any suggestions?

P.S. I didn't use a Vector3.Lerp like in your example, I just used a simple transform.Rotate. I don't know if that makes a difference.

avatar image aldonaletto · Jul 25, 2011 at 02:42 AM 0
Share

Lerp was only to rotate gradually. The problem is caused by the order Unity converts eulerAngles or Rotate to Quaternions: z, x then y. Since z is rotated first, the x axes gets tilted, and when rotated the ship nose will point to the other side. The solution is to use localEulerAngles.x first, then localEulerAngles.z. Don't use Rotate because it can accumulate errors and after some time the model will not return to initial position. I edited my answer to show a simple example of how to bank using this approach.

avatar image beelan · Aug 17, 2011 at 10:08 PM 0
Share

Life has kept me away from this project for a while, but now I'm back and deter$$anonymous$$ed to get it working right. I tried your newest suggestion and it does seem to correct that issue. $$anonymous$$y only question is, shouldn't I be using Time.deltaTime to make sure the banking is frame rate independent rather than just input times a set value (15 in your example)? I tried putting Time.deltaTime in (as I had it before when I was using Rotate) but it just causes the model to jitter rather than bank properly. Any help on this last point would be greatly appreciated and thanks for all your help so far.

Show more comments
avatar image
1

Answer by Blankzz · Jul 24, 2011 at 03:24 PM

Could you not take the cross product of your ships forward vector and a world up vector to get one direction. Then flip it to get the other direction?

Edit: Something like the following. Give it a try I'm not sure if it will work. You may have to swap the directions because the cross product depends on the hand rule of the world coordinate system.

 Vector3 leftDirection = Vector3.Cross(this.transform.forward, Vector3.up).normalized;
         Vector3 rightDirection = -leftDirection;
Comment
Add comment · Show 3 · 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 beelan · Jul 25, 2011 at 12:58 AM 0
Share

That's a very interesting solution, but if I understand it correctly, it wouldn't work if the ship was pitched forward or backwards, which is definitely something I want to implement.

avatar image Blankzz · Jul 25, 2011 at 01:04 AM 0
Share

By pitch I assume you mean tilting backwards and forwards. If so It will work if the ship is tilted too.

avatar image Blankzz · Jul 25, 2011 at 01:16 AM 0
Share

The cross product will equal 0 if your two vectors are the same. Depending on your calculations they probably will never be exactly the same but you could do a check in your code first and adjust the forward vector slightly so the cross product will return a suitable vector.

avatar image
0

Answer by DaveA · Jul 23, 2011 at 04:13 PM

Couldn't you save the world Y position, have it strafe relative to the ship, then set the world Y position back to what you saved?

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 beelan · Jul 24, 2011 at 03:20 PM 0
Share

Thanks for your quick response DaveA. Your solution sounds interesting but I don't really think I understand it or how to implement it. Do you think you could explain a little more?

Thanks!

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

6 People are following this question.

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

Vector3.Lerp with Translate Javascript 0 Answers

How to import the object from server to unity 2 Answers

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 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