Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by JonathanXD12 · May 05, 2021 at 08:53 AM · rotationscripting problemraycastingz-axis

Lock rotation of player

I'm currently making a 2D game as a beginner and I made a spinning platform. But when it's rotating the player's rotation (z-axis) also changes because it's a child of the platform. I need this when I use moving platforms. Now I want to lock the z-axis of the rotation of the player. I already tried it in 3 different ways, but none of them seems to be working. Does anybody know how to do this?

These are the three ways I tried:

 // 1
 PlayerTrans.transform.Rotate(
     PlayerTrans.transform.rotation.x, 
     PlayerTrans.transform.rotation.y, 
     0);
 
 // 2
 PlayerTrans.transform.Rotate(
     PlayerTrans.transform.rotation.x, 
     PlayerTrans.transform.rotation.y, 
     0, 
     Space.Self);
 
 // 3
 PlayerTrans.transform.localRotation = Quaternion.Euler(new Vector3(
     PlayerTrans.transform.localEulerAngles.x, 
     PlayerTrans.transform.localEulerAngles.y, 
     0f));

and this is, what my code looks like for staying on the moving platforms. I used raycasting for this:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Raycasting : MonoBehaviour
 {
     // Start is called before the first frame update
     Transform PlayerTrans;
     public float RayCastRange = 3;
     void Start()
     {
         PlayerTrans = transform.parent;
     }
 
     // Update is called once per frame
     void Update()
     {
         RaycastHit2D PlattformCheck = Physics2D.Raycast(transform.position, -Vector2.up, RayCastRange);
 
         if (PlattformCheck.collider != null)
         {
             if (PlattformCheck.collider.gameObject.tag == "Platform")
             {
                 PlayerTrans.transform.SetParent(PlattformCheck.collider.gameObject.transform);
             }
             else
             {
                 PlayerTrans.transform.SetParent(null);
             }
         }
         else
         {
             PlayerTrans.transform.SetParent(null);
         }
     }
 }




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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by kevinhart1001001 · May 05, 2021 at 09:42 PM

Hello, if you are using a Rigidbody, you can easily lock the z.rotation (image)alt text

,Hello, i guess you can disable rotation, if you use a Rigidbody (Disable Rotation on Z-Axis)


screenshot-2021-05-05-114726.jpg (26.1 kB)
screenshot-2021-05-05-114726.jpg (26.1 kB)
Comment
Add comment · Show 1 · 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 The-Peaceful · May 06, 2021 at 06:24 PM 0
Share

That would only mean that he disables rotation applied by the physics system I think. So I don't think it would help with freezing the rotation that is being applied to it through it's parent.

avatar image
0

Answer by The-Peaceful · May 05, 2021 at 11:07 AM

I think all you need to do is set the global eulerAngles like this:

 Vector3 eulers = PlayerTrans.eulerAngles;
 PlayerTrans.eulerAngles = new Vector3(eulers.x, eulers.y, 0f);

Though this would mean that you set the rotation around your Z-axis to 0 which you might not want to do. So maybe then go and create a variable storing the global angle around the Z-axis and use that instead:

 float zRotation = 0f; //Add the rotation value onto this variable when changing the Z-Rotation
 
 void Update() {
     Vector3 eulers = PlayerTrans.eulerAngles;
     PlayerTrans.eulerAngles = new Vector3(eulers.x, eulers.y, zRotation);
 }

And to answer why the methods you've tried didn't work:

transform.Rotate:

  • Rotates given degrees over given angles

  • if you rotate 0 over the Z-axis you won't rotate it at all

  • Also rotation is not a value in degrees! The rotation is stored in Quaternions, to get angles in degrees you have to use EulerAngles

transform.localRotation = Quaternion.Euler(...... validt option, but you take the local (relative to it's parent) eulerAngles so 0 degrees on the Z-axis is equal the degrees that it's parent has on that axis (so it will still rotate if it's parent rotates)


Hope that helps, if not let me know :D

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

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

Unity Mouse Look Script Issue 0 Answers

Sprite rotation (z-axis) and translation (y) is not working 1 Answer

raycast to determine pivot 1 Answer

rotation z of child wont work can some one help me plz 0 Answers

Bullet bounce issue using Vector3.reflect 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