• 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 BreakSilence · Jan 31, 2016 at 01:48 PM · c#scripting problemrotation axiscodepage

Player rotation always resets.

,I have run into a problem and I really can't recapitulate the origins. When I move the Player Cube the Cube will change its Y rotation, as long as I hold a direction. But as soon as I release the movement buttons, the rotation of the cube is reset to (0,0,0). I'd like it to stay at the last rotation it had after releasing the stick or buttons.

Any clues?

BaseMotor class

 using UnityEngine;
 using System.Collections;
 
 public abstract class BaseMotor : MonoBehaviour 
 {
     protected CharacterController controller;
     protected BaseState state;
     protected Transform thisTransform;
 
     private float baseSpeed = 5.0f;
     private float baseGravity = 25.0f;
     private float baseJumpForce = 7.0f;
     private float terminalVelocity = 30.0f;
     private float groundRayDistance = 0.5f;
     private float groundRayInnerOffset = 0.1f;
 
     public float Speed{get{return baseSpeed; } }
     public float Gravity{ get{return baseGravity; } }
     public float JumpForce{get{return baseJumpForce;} }
     public float TerminalVelocity{ get {return terminalVelocity; } }
 
     public float VerticalVelocity{ set; get;}
     public Vector3 MoveVector{ set; get;}
     public Quaternion RotationQuaternion{ set; get;}
 
     protected abstract void UpdateMotor ();
 
     protected virtual void Start()
     {
         controller = gameObject.AddComponent<CharacterController> ();
         thisTransform = transform;
 
         state = gameObject.AddComponent<WalkingState> ();
         state.Construct ();
     }
 
     private void Update()
     {
         UpdateMotor ();
     }
 
     protected virtual void Move()
     {
         controller.Move (MoveVector * Time.deltaTime);
     }
 
     protected virtual void Rotate()
     {
         thisTransform.rotation = RotationQuaternion;
     }
 
     public void ChangeState(string stateName)
     {
         System.Type t = System.Type.GetType (stateName);
 
         state.Destruct ();
         state = gameObject.AddComponent (t) as BaseState;
         state.Construct ();
     }
 
     public virtual bool Grounded()
     {
         RaycastHit hit;
         Vector3 ray;
 
         float yRay = (controller.bounds.center.y - controller.bounds.extents.y) + 0.3f,
         centerX = controller.bounds.center.x,
         centerZ = controller.bounds.center.z,
         extentX = controller.bounds.extents.x - groundRayInnerOffset,
         extentZ = controller.bounds.extents.z - groundRayInnerOffset;
 
 
         //Middle Raycast
         ray = new Vector3 (centerX,yRay,centerZ);
         Debug.DrawRay (ray, Vector3.down, Color.green);
         if (Physics.Raycast (ray, Vector3.down, out hit, groundRayDistance)) 
         {
             return true;
         }
 
         ray = new Vector3 (centerX + extentX,yRay,centerZ + extentZ);
         Debug.DrawRay (ray, Vector3.down, Color.green);
         if (Physics.Raycast (ray, Vector3.down, out hit, groundRayDistance)) 
         {
             return true;
         }
 
         ray = new Vector3 (centerX - extentX,yRay,centerZ + extentZ);
         Debug.DrawRay (ray, Vector3.down, Color.green);
         if (Physics.Raycast (ray, Vector3.down, out hit, groundRayDistance)) 
         {
             return true;
         }
 
         ray = new Vector3 (centerX - extentX,yRay,centerZ - extentZ);
         Debug.DrawRay (ray, Vector3.down, Color.green);
         if (Physics.Raycast (ray, Vector3.down, out hit, groundRayDistance)) 
         {
             return true;
         }
 
         ray = new Vector3 (centerX + extentX,yRay,centerZ - extentZ);
         Debug.DrawRay (ray, Vector3.down, Color.green);
         if (Physics.Raycast (ray, Vector3.down, out hit, groundRayDistance)) 
         {
             return true;
         }
 
         return false;
     }
 }

PlayerMotor class

 using UnityEngine;
 using System.Collections;
 
 public class PlayerMotor : BaseMotor 
 {
     private CameraMotor camMotor;
     private Transform camTransform;
 
     protected override void Start()
     {
         base.Start ();
 
         camMotor = gameObject.AddComponent<CameraMotor> ();
         camMotor.Init ();
         camTransform = camMotor.CameraContainer;
     }
 
     protected override void UpdateMotor()
     {
         //Get the input
         MoveVector = InputDirection();
 
         //Rotate MoveVector with Camera
         MoveVector = RotateWithView(MoveVector);
 
         //Send the input to a filter
         MoveVector = state.ProcessMotion(MoveVector);
         RotationQuaternion = state.ProcessRotation (MoveVector);
 
         //Check if we need to change current state
         state.Transition();
 
         // Move
         Move();
         Rotate ();
     }
 
     private Vector3 InputDirection()
     {
         Vector3 dir = Vector3.zero;
 
         dir.x = Input.GetAxis ("Horizontal");
         dir.z = Input.GetAxis ("Vertical");
 
         if (dir.magnitude > 1)
             dir.Normalize ();
 
         return dir;
     }
 
     private Vector3 RotateWithView(Vector3 input)
     {
         Vector3 dir = camTransform.TransformDirection (input);
         dir.Set (dir.x, 0, dir.z);
         return dir.normalized * input.magnitude;
     }
 }
 
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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

74 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

Related Questions

Can Someone Explain Why Message does not show Anything In Inspector 0 Answers

Problems with limiting angles 1 Answer

Scripting Help desperately needed. 1 Answer

Why won't my model rotate up on X axis? 1 Answer

Trying to have bullets travel in the direction the player is facing 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges