• 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 Hollowayinc · Apr 12, 2020 at 01:23 AM · rotaterotate objectrotation axisrotatearoundrotatetowards

Flying player Rotates in the left and right direction not forward and back

I'm trying to follow a tutorial on a flying rocket game (where you fly around objects and try not to hit them. The player (A pig) rotates only to its left and right and tilts over. I need it to rotate on the axis that will allow it to rotate foward and back so when the player presses the "Space" bar the character will hover in the right direction. The image entails how the player(pig) falls talt texto its side not rotating the in the right axis. ps: The "A" and "D" Keys are used/pressed for rotating.

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

/ Vector3 direction = otherObject.position - transform.position; Quaternion toRotation = Quaternion.FromToRotation(transform.up, direction); transform.rotation = Quaternion.Lerp(transform.rotation, toRotation, speed Time.time);*/

public class takeFlight : MonoBehaviour { AudioSource audiosource;

 public Rigidbody rigidBody;
   
 float speed = 100f;

 void Start()
 {
     rigidBody = GetComponent<Rigidbody>();
     audiosource = GetComponent<AudioSource>();
 }
 // Update is called once per frame
 void Update()
 {
     processInput();
 }

 private  void processInput()
 {

     if ((Input.GetKey("a")))
     {
         transform.Rotate(Vector3.forward * speed * Time.deltaTime);
     }
     else if ((Input.GetKey("d")))
     {
         transform.Rotate(-Vector3.forward * speed * Time.deltaTime);
     }

     else if (Input.GetKey("space"))
     {
         rigidBody.AddRelativeForce(Vector3.up * speed * Time.deltaTime);
     }
     else
     {

     }
 }

}

questionforudemy.png (36.8 kB)
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 Scribe · Apr 13, 2020 at 12:04 AM

Most likely is that the Vector3.forward is just not correct in your case. At a guess, quite a few models import with a 90degree offset in some direction which you may have fixed by manually rotating your object, however it's quite possible that forward for your pig isn't the same as the world forward.

Since I don't know your axis from the screenshot your options are, try a different vector, since up seems to be correct, probably swap forward for right and see if that's better.

other solution would be to work in world space rather than local. Using the 3 arg form of Rotate you can do:

 transform.Rotate(Vector3.forward, speed * Time.deltaTime, Space.World);

First arg is the axis to rotate around, second is the amount, 3rd is the space that the supplied axis it relative to.

Hope that helps!

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

129 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

Related Questions

Rotating around self axis by n degrees ?? 1 Answer

I want to use LookAt but based on the object original rotation 1 Answer

Smooth Camera Rotation After 2 Seconds 0 Answers

Joystick Rotation with Transform.RotateAround 0 Answers

Having trouble rotating a turret 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