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 jonmar2348 · Aug 11, 2011 at 10:33 PM · movementcontrolairplane

Airplane Script Control

Hello, I'm new to Unity programming. The problem with plane is that it can move around but, the plane stays facing one direction. This is the program for it at the bottom. Can anyone help. Thank You.

private var controller :CharacterController; controller = gameObject.GetComponent(CharacterController); private var moveDirection = Vector3.zero; private var forward = Vector3.zero; private var right = Vector3.zero;

function Update() { forward = transform.forward; right = Vector3(forward.z, 0, -forward.x); var horizontalInput = Input.GetAxisRaw("Horizontal"); var verticalInput = Input.GetAxisRaw("Vertical"); var targetDirection = horizontalInput right + verticalInput forward;
moveDirection = Vector3.RotateTowards(moveDirection, targetDirection, 200 Mathf.Deg2Rad Time.deltaTime, 1000); var movement = moveDirection Time.deltaTime 2; controller.Move(movement); transform.rotation = Quaternion.LookRotation(moveDirection); if (targetDirection != Vector3.zero) { transform.rotation = Quaternion.LookRotation(moveDirection); } }

@script RequireComponent(CharacterController)

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
Best Answer

Answer by aldonaletto · Aug 12, 2011 at 12:55 PM

You should do any movement or direction change inside the if:

private var controller :CharacterController; controller = gameObject.GetComponent(CharacterController); private var moveDirection = Vector3.zero; private var forward = Vector3.zero; private var right = Vector3.zero;

function Update() { forward = transform.forward; right = Vector3(forward.z, 0, -forward.x); var horizontalInput = Input.GetAxisRaw("Horizontal"); var verticalInput = Input.GetAxisRaw("Vertical"); var targetDirection = horizontalInput right + verticalInput forward;
if (targetDirection != Vector3.zero){ moveDirection = Vector3.RotateTowards(moveDirection, targetDirection, 200 Mathf.Deg2Rad Time.deltaTime, 1000); var movement = moveDirection Time.deltaTime 2; controller.Move(movement); transform.rotation = Quaternion.LookRotation(moveDirection); } }

@script RequireComponent(CharacterController) But this isn't the best way to do what you want (this script looks like a Frankenstein creature buit from 3rd Person Controller dead scripts, and gets crazy when you try to go backwards).
An easier way to get the same results without this backward issue is:

var turnSpeed: float = 90; var moveSpeed: float = 5;

private var controller :CharacterController; controller = gameObject.GetComponent(CharacterController);

function Update() { var horizontalInput = Input.GetAxisRaw("Horizontal"); var verticalInput = Input.GetAxisRaw("Vertical"); transform.Rotate(0, horizontalInput turnSpeed Time.deltaTime, 0); var movement = transform.forward verticalInput Time.deltaTime * moveSpeed; controller.Move(movement); }

@script RequireComponent(CharacterController)

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 jonmar2348 · Aug 12, 2011 at 04:35 PM 0
Share

The plane is able to rotate now without getting stuck in one direction. Thank you for your help.

avatar image aldonaletto · Aug 12, 2011 at 09:15 PM 0
Share

@jonmar2348, if this answer solved the problem, please mark it as accepted: click the check mark below the voting thumbs, so the answer will become green and help other people with similar problems ;)

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How to make camera position relative to a specific target. 1 Answer

disable scripts 1 Answer

Newbie... Master script? 5 Answers

Vehicle/car/ Movement 0 Answers

Issue with Collisions due to Parenting script 0 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