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 /
  • Help Room /
avatar image
0
Question by HaruThePotato · Oct 31, 2016 at 05:25 PM · unity 5rotationtopdownc++diagonal

Rotate an object diagonally and prevent it from going back to 90,180,0,360 degrees

After holding down on a combination keys of AS,SD,DW or WA to move my object, it will successfully move diagonally and rotate to the correct position, but after releasing the keys, it will rotate back to either 0,90,180 or 360 depending on the nearest rotation after releasing my keys, I guess it's because of that 1 frame that I left the keys touched, so it ran that code and moved it to 0,90,180,360. But I don't know how to solve it.

It's my first time using unity answers, but I hope I've provided enough information, and thanks for helping out.

I'm pressing onto AW keys together alt text

After releasing the keys, it doesn't stay that way but moves to either left or top alt text PlayerMovement.cs

 using UnityEngine;
 using System.Collections;
 
 public class playerMovement : MonoBehaviour {
 
     private float speedWalk = 7.5f;
     private float speedRotate = 7.5f;
     private GameObject raycastObject;
 
     // Update is called once per frame
     void FixedUpdate ()
     {
         //Non-Diagonal Movements
         if (Input.GetKey(KeyCode.W) && !Input.GetKey(KeyCode.A) && !Input.GetKey(KeyCode.S) && !Input.GetKey(KeyCode.D)) //player movement up
         {
             transform.localPosition += new Vector3(0.0f, 0.0f, speedWalk * Time.deltaTime);
         }
         if (Input.GetKey(KeyCode.S) && !Input.GetKey(KeyCode.A) && !Input.GetKey(KeyCode.W) && !Input.GetKey(KeyCode.D)) //player movement down
         {
             transform.localPosition -= new Vector3(0.0f, 0.0f, speedWalk * Time.deltaTime);
         }
         if (Input.GetKey(KeyCode.D) && !Input.GetKey(KeyCode.A) && !Input.GetKey(KeyCode.W) && !Input.GetKey(KeyCode.S)) //player movement right
         {
             transform.localPosition += new Vector3(speedWalk * Time.deltaTime, 0.0f, 0.0f);
         }
         if (Input.GetKey(KeyCode.A) && !Input.GetKey(KeyCode.D) && !Input.GetKey(KeyCode.W) && !Input.GetKey(KeyCode.S)) //player movement left
         {
             transform.localPosition -= new Vector3(speedWalk * Time.deltaTime, 0.0f, 0.0f);
         }
 
         //Diagonal Movements **@@@@@@@@** I think this is the problem.
         if (Input.GetKey(KeyCode.A) && Input.GetKey(KeyCode.W)) //player movement Top Left
         {
             transform.localPosition -= new Vector3(speedWalk * Time.deltaTime, 0.0f, 0.0f);
             transform.localPosition += new Vector3(0.0f, 0.0f, speedWalk * Time.deltaTime);
         }
         if (Input.GetKey(KeyCode.W) && Input.GetKey(KeyCode.D)) //player movement Top Right
         {
             transform.localPosition += new Vector3(0.0f, 0.0f, speedWalk * Time.deltaTime);
             transform.localPosition += new Vector3(speedWalk * Time.deltaTime, 0.0f, 0.0f);
         }
         if (Input.GetKey(KeyCode.D) && Input.GetKey(KeyCode.S)) //player movement Bottom Right
         {
             transform.localPosition += new Vector3(speedWalk * Time.deltaTime, 0.0f, 0.0f);
             transform.localPosition -= new Vector3(0.0f, 0.0f, speedWalk * Time.deltaTime);
         }
         if (Input.GetKey(KeyCode.S) && Input.GetKey(KeyCode.A)) //player movement Bottom Left
         {
             transform.localPosition -= new Vector3(0.0f, 0.0f, speedWalk * Time.deltaTime);
             transform.localPosition -= new Vector3(speedWalk * Time.deltaTime, 0.0f, 0.0f);
         }
     }
 }

playerRotateMouse.cs

 using UnityEngine;
 using System.Collections;
 
 public class playerRotateMouse : MonoBehaviour
 {
     public Transform Player;
     private float speed = 7.5f;
     Quaternion targetRotation;
 
     void FixedUpdate()
     {
         Plane playerPlane = new Plane(Vector3.up, transform.position);
 
         // Generates a ray from cursor
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 
         float hitdist = 0.0f;
 
         if (Input.GetButtonDown("Fire1") || Input.GetButtonDown("Fire2"))
         {
             if (playerPlane.Raycast(ray, out hitdist))
             {
                 Vector3 targetPoint = ray.GetPoint(hitdist);
                 Debug.DrawRay(transform.position, targetPoint, Color.green);
                 targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
             }
         }
         // Smooth rotation towards point.
         transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, speed * Time.deltaTime);
 
         //Rotate base on key pressed
         if (Input.GetKey(KeyCode.W) && !(Input.GetButton("Fire1") || Input.GetButton("Fire2"))) //player rotate up
         {
             targetRotation = Quaternion.LookRotation(Vector3.forward);
             transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, speed * Time.deltaTime);
         }
         if (Input.GetKey(KeyCode.S) && !(Input.GetButton("Fire1") || Input.GetButton("Fire2"))) //player rotate down
         {
             targetRotation = Quaternion.LookRotation(Vector3.forward * -1);
             transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, speed * Time.deltaTime);
         }
         if (Input.GetKey(KeyCode.D) && !(Input.GetButton("Fire1") || Input.GetButton("Fire2"))) //player rotate right
         {
             targetRotation = Quaternion.LookRotation(Vector3.right);
             transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, speed * Time.deltaTime);
         }
         if (Input.GetKey(KeyCode.A) && !(Input.GetButton("Fire1") || Input.GetButton("Fire2"))) //player rotate left
         {
             targetRotation = Quaternion.LookRotation(Vector3.left);
             transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, speed * Time.deltaTime);
         }
     }
 }


1.png (5.8 kB)
2.png (5.5 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

0 Replies

· Add your reply
  • Sort: 

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

125 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

Related Questions

Applying AddForce() while rotating player gameobject. 0 Answers

Cannot rotate over 360 degrees 1 Answer

Why the subtracting compound assignment operator is used to ADD a angle in unity? 1 Answer

Reset rotation of gameobject 1 Answer

have enemy try to get in front of player and push them back help 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