• 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 Chanindu · Aug 27, 2021 at 12:57 PM · game

Im going sideways instead of forwards

Hi, My Name is Chanindu, and im a beginner game developer, I just made a script to make my plane fly in my flight simulator, And then, i get a problem, the plane moves sideways instead of forward, Here's y script

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class Move : MonoBehaviour { public float power; public GameObject plane;

 private Rigidbody rb;

  void Start()
 { 
     rb = GetComponent<Rigidbody>();
 }

  void FixedUpdate()
 {
     bool isSpace = Input.GetKey(KeyCode.Space);
     bool isShift = Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift);
     bool isRight = Input.GetKey(KeyCode.RightArrow);
     bool isLeft = Input.GetKey(KeyCode.LeftArrow);
     bool isForward = Input.GetKey(KeyCode.UpArrow);
     bool isA = Input.GetKey(KeyCode.A);
     bool isD = Input.GetKey(KeyCode.D);

     Vector3 rotZ = new Vector3(0.0f, 0.0f - 0.1f);
     Vector3 rotY = new Vector3(0.0f, 0.2f - 0.0f);
     Vector3 rotX = new Vector3(0.1f, 0.0f - 0.0f);

     if (isSpace == true)
     {
         plane.transform.Rotate(rotZ);
     }
     else if (isShift == true)
     {
         plane.transform.Rotate(-rotZ);

     }
     if (isRight == true && plane.transform.rotation.x <= 90)
     {
         plane.transform.Rotate(rotX);
         plane.transform.Rotate(rotY);
     }
     else if (isLeft == true && plane.transform.rotation.x >= -90)
     {
         plane.transform.Rotate(-rotX);
         plane.transform.Rotate(-rotY);
     }

     else if (isForward == true)
     {
         rb.AddRelativeForce(Vector3.right * power * -100);
     }
     else if (isA == true)
     {
         rb.AddRelativeForce(Vector3.right * power * -100);
         plane.transform.Rotate(-rotX);
     }
      else if (isD == true)
     {
         rb.AddRelativeForce(Vector3.right * power * -100);
         plane.transform.Rotate(rotX);
     }
 }

}

Can i know what make smy plane go sideways instead of forward and how do i make it go forward,

If i go more understandably, the plane moves on the X-Axis, but i want it to move on the Z-Axis

How do i do that?

Comment
Add comment · Show 2
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 paparazi666 · Aug 27, 2021 at 01:12 PM 0
Share

try this

else if (isForward == true) { rb.AddRelativeForce(Vector3.forward power 100); }

Show more comments

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by EliasMiettinen · Aug 27, 2021 at 02:02 PM

You have small problem in if. Instead of :

 else if (isForward == true)
      {
          rb.AddRelativeForce(Vector3.right * power * -100);
      }

use this:

 else if (isForward == true)
      {
          rb.AddRelativeForce(Vector3.forward * power * 100);
      }

Change Vector3.right to Vector3.forward, and -100 to 100.

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

Answer by GeroNL · Aug 29, 2021 at 06:57 AM

Hello, if you are make a 3d game, it depend of camera and what are you want to do, if fps or third person, then you can do like this:

  //forward move
  else if (isForward == true)
  {
       Vector3 forward = Camera.main.transform.TransformDirection(Vector3.forward);
       rb.AddRelativeForce(forward * power );
  }

  //go right
  else if (isRight == true)
  {
       Vector3 Right= Camera.main.transform.TransformDirection(Vector3.right);
       rb.AddRelativeForce(Right * power );
  }

  // go left
  else if (isLeft == true)
  {
       Vector3 Left= Camera.main.transform.TransformDirection(Vector3.right);
       rb.AddRelativeForce(Left* power * -1);
  }

Hope it help.

Comment
Add comment · Show 8 · 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 Chanindu · Aug 29, 2021 at 09:42 AM 0
Share

@GeroNL im not using FPS or third person or whatever, im making my aircraft fly

avatar image GeroNL Chanindu · Aug 29, 2021 at 10:54 AM 0
Share

Well, then what perspective in camera are you use? are you already try code that i suggest?

avatar image Chanindu GeroNL · Aug 31, 2021 at 01:12 PM 0
Share

Im Making a Flight Simulator in Third Person, Sorry but i thought you were asking whether i was making a walking game in FPS of 3rd person

Show more comments
Show more comments
avatar image
0

Answer by Riiich · Aug 31, 2021 at 06:10 PM

Check these Vector3 rotZ = new Vector3(0.0f, 0.0f - 0.1f); it should be Vector3 rotZ = new Vector3(0.0f, 0.0f, 0.1f);

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

156 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 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 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 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 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 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 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 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

getting udp package info inside unity (GlovePIE) 0 Answers

How do you save a game in Unity iPhone? 1 Answer

Aiming down a gun 1 Answer

How to do a SLiding door animation in Unity 3d with scripting. 1 Answer

How to store a boolean for each variable in a class 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