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