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 /
avatar image
0
Question by Stugger · May 22, 2017 at 03:26 PM · rotationrigidbodytransformquaternionrotate

Player character X rotation going haywire on game start

Hey all, so randomly my player characters rotation is messing with me. I was working on an orbit cam that I could rotate around the player on the Y axis depending on which key was pressed sort of like a Runescape deal. I added a dummy object(sphere), attached the camera to it and set the transform position of the dummy object to my player and then added the rotation script to the dummy, I successfully did this and everything was working perfect.

After a happy victory, I decided to attempt and correct the player falling over problem when it attempts to walk over a bump or incline (I'm using a custom thirdperson character) as freezing the rigidbody rotation constraints was not solving this issue. I attempted to mess around with Quaternion and freeze the rotation via script, unfortunately I am still a noob to all of this and had no idea what I was doing, I was able to freeze all 3 rotations and the player was staying upright while going up an incline, but unfortunately now the player would not turn on the Y axis when walking in different directions. So I deleted all my code relating to freezing the rotations and decided to tackle something else. This is when the issue started.

For a couple of game starts the player was doing fine just as it was when I finished the orbit cam setup, but now, every time I start the game, my players X-axis goes automatically to -90 and bounces around! (Even with the X constraint frozen via rigidbody), if I click around the map rapidly the player will eventually bounce back to standing upright, but even then the X axis is a bit touchy and he very slowly starts to fall over (WITH CONSTRAINTS FROZEN?) So my question is, is there a way I could freeze my players X and Z rotation indefinitely via script? as I attempted to do earlier with Quaternions and failed? or even at least correct this issue to where my player will once again stand upright. did me messing with Quaternions hard-code some kind of glitch?

My Code:

 using UnityEngine;
 using System.Collections;
 using UnityEngine.EventSystems;
 
 public class PlayerController : MonoBehaviour
 {
     public float speed = 6;
     public Rigidbody rb;
     public Animator anim;
 
     private Vector3 targetPosition;
     private bool isMoving;
 
     const int LEFT_MOUSE_BUTTON = 0;
 
     void Start ()
     {
 
         targetPosition = rb.transform.position;
         isMoving = false;
 
     }
 
     void Update()
     {
 
         if (Input.GetMouseButtonDown (LEFT_MOUSE_BUTTON))
             SetTargetPosition ();
 
         if (isMoving)
             MovePlayer();
 
         if (PointerOverUI())
          {
              targetPosition = transform.position;
              isMoving = false;
          }
          else
          {
              MovePlayer();
          } 
 
     }
 
     public bool PointerOverUI() // method for checking if pointer is over a UI
     {
         return UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject();
     }
 
     void SetTargetPosition ()
     {
         Plane plane = new Plane (Vector3.up, rb.transform.position);
         Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
         float point = 0f;
 
         if (plane.Raycast(ray, out point))
         {
             targetPosition = ray.GetPoint(point);
         }
 
         if (PointerOverUI()) // If the pointer is clicked over a UI, player will not go to clicked point
         {
             targetPosition = transform.position;
         }
         else // if the pointer is not over UI, then player will move
         {
             MovePlayer(); 
         }
 
         isMoving = true;
     }
 
     void MovePlayer ()
     {
             rb.transform.LookAt(targetPosition);
             rb.transform.position = Vector3.MoveTowards(transform.position, targetPosition, speed * Time.deltaTime);
 
         if (transform.position == targetPosition)
             isMoving = false;
 
         if (isMoving == false) {
             anim.SetInteger("Condition", 0);
             return;    
         } else {
             anim.SetInteger("Condition", 1);
         }
 
         Debug.DrawLine (transform.position, targetPosition, Color.red);
     }
 
 }

and my dummy object camera script, just in case that happened to have something to do with it:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class RotateCamera : MonoBehaviour {
 
     public int speed = 150;
 
     public GameObject player;
 
     void Update()
     {
         Vector3 pos = player.transform.position;
         transform.position = pos;
 
         if (Input.GetKey(KeyCode.A)) // rotate camera left
             transform.Rotate(Vector3.up * speed * Time.deltaTime);
 
         if (Input.GetKey(KeyCode.D)) // rotate camera right
             transform.Rotate(-Vector3.up * speed * Time.deltaTime);
     }
 }

(I HAVE NO COMPILER/CONSOLE ERRORS)

I can't see anything that would affect my players rotation to the point to where he'd fall over, go haywire on the X-axis and ignore the rigidbody constraints? Then again, as i said, I am still a super noob, so any insight or help would be so much apreciated! thank you!

Comment
Add comment · Show 2
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 Stugger · May 19, 2017 at 08:41 AM 0
Share

Okay, so I think I figured it out. I forgot to remove the:

           else
           {
               $$anonymous$$ovePlayer();
           } 

in my the update function of my PlayerController... DOH!

UPDATE: Okay never$$anonymous$$d, it seems to have dampened the glitch, seems at least half the time he'll stand upright now, am I missing something else? BUT! if anyone can tell me how to lock the rotation of my players X and Z axis indefinitely via script and not the rigidbody constraints, perhaps that would correct it? thanks!

avatar image Bonfire-Boy · May 23, 2017 at 12:27 PM 0
Share

Rigidbody constraints do not lock their movement, they constrain what the physics system can do with the rigidbody. You're moving and rotating the object directly so you need to constrain the motion yourself (eg with something along the lines ShadyProductions' answer suggests)

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by ShadyProductions · May 23, 2017 at 11:41 AM

This will lock the X rotation to 0, but leave y and z rotation the same as the transform.

 Vector3 rotation = new Vector3(0, rb.transform.rotation.y, rb.transform.rotation.z);
 rb.transform.rotation = Quaternion.Euler(rotation);

Put this inside the MovePlayer(); method

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

how to tilt boat on turning 2 Answers

Rotation Jumping values (0 to 180) 1 Answer

Object slope rotation issue 0 Answers

Problem with rigidbodies and rotation with quaternion 1 Answer

How to turn a car like in real world? 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