• 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
Question by TheDevilsHitman · Jun 27, 2016 at 02:54 PM · c#rotationscripting problemscripting beginnerscript error

[C#] Quaternion Rotations with Input.GetAxis problems.

Hello, I am New to Unity however I have been doing Game Maker for a couple of years now and so I know how make codes on an intermediate level. Now, I don't know all the functions that is within Unity or the inner workings of it. But I am having trouble with some code. Now I have managed to make a Movement script for my Player within a 3D space in C# Code and I have been figuring out Quaternion Rotations and looking up and researching it. What I am trying to achieve with this code is after 3D movement that can make the player go in 8 directions as you would have in most games I am sure. I have a void that is for moving and I have one for turning ( So void Move() and void Turn() ) they are both called by the Update function. Now what I want is to go for an old PS1 3D platformer where you don't need to walk in a circle to rotate. All you have to do is press a button and the player snaps to that direction. So in this case the GetAxis. I have those working but when he does snap to the direction that I have pressed he doesn't go in that one direction that I want him too, instead he goes in the same direction no matter whichever way he faces ( Faces forward, turn to face Left, He moves in the direction he was originally facing). So I figured it must be just the model being rotated not the object. So with any luck could somebody maybe give me some ideas or fix up some of my code for me? Again, I am new to the program itself but coding I am pretty good at.

Here is my code: [RequireComponent(typeof(Rigidbody))] [RequireComponent(typeof(Animator))]

public class PlayerMovement : MonoBehaviour {

     //Variables.
         Animator Anim;
         Rigidbody rBody;

// Quaternion targetRotation;

 public int Walkspeed = 10;
         //public int Runspeed = 10;
 //public int Ground = 1; 

 //public int RotateVEL = 360;

 public int JumpHeight = 16;
 public float JumpSpd = 10f;


 public Quaternion TargetRotation;

         void Awake () {
     Anim = GetComponent<Animator> ();
     rBody = GetComponent<Rigidbody> ();
 }
             

 void Start () {
     //Turn ();
 }

 // Update is called once per frame
 void Update () {
     Move ();
     Turn (); 

 }


         void Move () {
     // Moving Forward, Backwards and Left and Right.
     float Z = Input.GetAxis ("Vertical"); 
     float X = Input.GetAxis ("Horizontal"); 

     transform.Translate (X * Time.deltaTime * Walkspeed, 0, Z * Time.deltaTime * Walkspeed);
 
     Anim.SetFloat ("Forward", Input.GetAxis ("Vertical") );
 }



 //Rotations.
     void Turn() {

     //float Z = Input.GetAxis ("Vertical"); 
     //float X = Input.GetAxis ("Horizontal"); 


         if (Input.GetAxis ("Horizontal") > 0)
         transform.rotation = Quaternion.LookRotation (Vector3.right);

     //    transform.LookAt (Vector3.right * Time.deltaTime * Walkspeed );
 
     //    transform.Rotate (Vector3.up * Time.deltaTime * RotateVEL);
     //transform.Rotate (Vector3.right * Time.deltaTime * Walkspeed);
         else if (Input.GetAxis ("Horizontal") < 0)
         transform.rotation = Quaternion.LookRotation (Vector3.left);


     if (Input.GetAxis ("Vertical") < 0) 
         transform.rotation = Quaternion.LookRotation (Vector3.forward);

     else if (Input.GetAxis ("Vertical") > 0)
         transform.rotation = Quaternion.LookRotation (Vector3.back);

 }



}

I have tried a lot of things. And its taken me about 5 days to get this far with working it out by myself so any help would be nice and appreciated. Again, I just basically want to use the GetAxis function to move in 8 directions (for my 360 Controller mainly) and the character or the player facing the direction and him being able to move in that direction when held down.

Thank you. Sorry for the long post but I sometimes have a hard time explaining stuff ^^'

Comment

People who like this

0 Show 0
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

Answer by regred002 · Jul 08, 2016 at 06:45 PM

Maybe try the Transform.LookAt command with a set of transforms that are dependent on the player? like

 if (Input.GetKey (KeyCode.u)) {
 transform.LookAt(new Vector3(transform.position.x+5,transform.position.y,transform.position.z));
 }
 if (Input.GetKey (KeyCode.J)) {
 transform.LookAt(new Vector3(transform.position.x-5,transform.position.y,transform.position.z));
 }

That should work (given a couple other sets of these), But it will be quite jittery if you attach a camera to it.

I don't use axes at all but I would image you could just use the code you made earlier instead of my key inputs.

Comment

People who like this

0 Show 0 · 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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How can i place subjects in different positions? 0 Answers

Flip over an object (smooth transition) 3 Answers

Editing rotation of an Instantiated object 3 Answers

"Only assignment, call, increment, decrement and new object expressions can be used as statements" 1 Answer

"Follow Target" script with a prefab (C#) Help me please! 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