• 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 HAPPY_SIR · Jan 23, 2017 at 08:14 AM · player movementrotate objectmove an objectflyingjetpack

how do I make a obeject (player) move the way it's pointing ? (like a jetpack) (c#)

Hi , have been looking how to fix this for some time now and haven't found how to so if you know how to fix it and write that , thank you.

I am working on a game where you fly arond with a jetpack (in almost no gravity) and can rotate the character .The script I made works but not as I want it to. When I press space it the player/obeject moves up but it always go up. When I rotate the character it still moves the same way (when I press space it moves up , even if my character is upside down) and I haven't found a way how to fix that.

script I am using (c#) :

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PlayerMovement : MonoBehaviour {
 
 
     public float JetSpeed;
     public float RotateSpeed = 30f;
 
 
     void Update() {
 
         if (Input.GetKey(KeyCode.A))
         {
             transform.Rotate(-Vector3.forward * -30 * Time.deltaTime);
         }
 
         else if (Input.GetKey(KeyCode.D))
         {
             transform.Rotate(Vector3.forward * -30 * Time.deltaTime);
         }

         **// this is the part that makes the player fly/jump**

         if (Input.GetKeyDown(KeyCode.Space))
         {
             transform.position = transform.up * JetSpeed;
 
         }
 
     }
 }
 
 

Thanks for reading and have a great day! // HAPPY_SIR

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

Answer by voncarp · Jan 23, 2017 at 08:29 AM

I readjusted your rotation and forward movement functionality as they didnt seem to move in a meaningful way. Something like this might be what you are looking for :

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class helpscript : MonoBehaviour {
 
     public float JetSpeed;
     public float RotateSpeed = 30f;
  
     void Update() {
 
         if (Input.GetKey(KeyCode.A))
         {
             transform.Rotate(-Vector3.down * -30 * Time.deltaTime);
         }
 
         else if (Input.GetKey(KeyCode.D))
         {
             transform.Rotate(Vector3.down * -30 * Time.deltaTime);
         }
          if (Input.GetKey(KeyCode.Space))
         {
             transform.position += transform.forward * JetSpeed * Time.deltaTime;
 
         }
 
     }
 }


You might have to adjust the rotations as I don't know if you are using 2D or 3D

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 Pengocat · Jan 23, 2017 at 09:22 AM 0
Share

You may want to use Space.self/LocalPosition ins$$anonymous$$d of using the world coordinate so when the object is rotated you move upwards in the direction of the object and not upwards relative to the world.

     [Range(1f, 50f)]
     public float JetSpeed = 10f;
     [Range(1f, 100f)]
     public float RotateSpeed = 30f;
 
     void Update()
     {
         bool rotateLeft = Input.Get$$anonymous$$ey($$anonymous$$eyCode.A);
         bool rotateRight = Input.Get$$anonymous$$ey($$anonymous$$eyCode.D);
         bool flyUpwards = Input.Get$$anonymous$$ey($$anonymous$$eyCode.Space);
 
         if (rotateLeft && rotateRight)
         {
             // No rotation
         }
         else if (rotateLeft)
         {
             transform.Rotate(Vector3.back * RotateSpeed * Time.deltaTime);
         }
 
         else if (rotateRight)
         {
             transform.Rotate(Vector3.forward * RotateSpeed * Time.deltaTime);
         }
         // this is the part that makes the player fly/jump**
         if (flyUpwards)
         {
             var translateSpeed = JetSpeed * Time.deltaTime;
             transform.Translate(Vector3.up * translateSpeed, Space.Self);
         }
     }
avatar image HAPPY_SIR · Jan 23, 2017 at 05:21 PM 0
Share

Thanks so much for the answares , but sadly I am using 2d , and what I am trying to do is make the character fly in the derection it is pointing. But Thanks for the answare anyways! I am really thankfull for your answer!

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Is it better to make the Player in FPS be a Rigidbody or a characterController? 1 Answer

Player Momentarily Stuck at Edges of Screen! 1 Answer

Issue moving Object around surface of sphere 3 Answers

Problem with player bouncing or jumping 1 Answer

Two problems One code 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