Hi,
It has been a while since I have played around with unity and there may be a better way to do this, so feel free to say “that is a bad idea and you should do it like this…”
My character can have the transform.up positive and negative on every axis, so the number of possibilities for the player to navigate is 6 ( x/-x/y/-y/z/-z). The player is controlled perfectly while in the original orientation ( transform.up == y ), but when the player is parented to another object, then rotated, then un-parented, it doesn’t work as expected.
The script is below, and it basically is supposed to take the vertical/horizontal axis based on the camera orientation and translate that to usable world directions… Within the if statement of around line 50 is where things aren’t adding up, I’m sure.
***EDIT: OOPs, I guess I should have mentioned what exactly was happening when “things weren’t working as expected”… What happens, is the player ends up rotating back to transform.up == y when it should have transform.up == x… So I believe the lookat functions are sending the wrong info, but I’m not quite sure where to start.
I’m going &)$(%^*#() nuts with this, so if anyone can help me, I would greatly appreciate it… Thanks in advance.
public class PlayerControl : MonoBehaviour
{
public delegate void EventHandler (GameObject e);
public event EventHandler UpCubeRotate;
public event EventHandler DownCubeRotate;
public event EventHandler RightCubeRotate;
public event EventHandler LeftCubeRotate;
private Animator anim;
private Vector3 upStateTop;
private Vector3 upStateForward;
private Vector3 upStateRight;
public bool cubeRotWait = false;
public Transform mainCam;
public Vector3 cubeRotDir;
public GameObject objectBelow;
public GameObject cubeCenter;
// Use this for initialization
void Start ()
{
anim = GetComponent<Animator>();
cubeCenter.transform.up = transform.up;
upStateTop = transform.up;
upStateForward = transform.forward;
upStateRight = transform.right;
}
// Update is called once per frame
void FixedUpdate ()
{
if( Input.GetKeyDown( KeyCode.Space ))
{
anim.SetFloat("Speed", 0);
transform.parent = cubeCenter.transform;
mainCam.transform.parent = cubeCenter.transform;
cubeRotWait = true;
Vector3 transPosUpOne = transform.position + (transform.up);
RaycastHit hit;
if (Physics.Raycast(transPosUpOne, -transform.up, out hit)) //change position to one of 4 points and get rid of physics raycasts below
{
objectBelow = hit.transform.gameObject;
}
}
if( Input.GetKey( KeyCode.UpArrow ) || Input.GetKey( KeyCode.DownArrow ) || Input.GetKey( KeyCode.RightArrow ) || Input.GetKey( KeyCode.LeftArrow ) )
{
if(!cubeRotWait)
{
transform.parent = null;
float h = Input.GetAxis("Horizontal"); // setup h variable as our horizontal input axis
float v = Input.GetAxis("Vertical"); // setup v variables as our vertical input axis
Vector3 moveVectorX = mainCam.forward * v;
Vector3 moveVectorY = mainCam.right * h;
Vector3 moveVector = ( moveVectorX + moveVectorY ).normalized * Time.deltaTime;
print (moveVector.normalized);
if( ( transform.up == upStateTop ) || ( transform.up == -upStateTop ) )
{
cubeRotDir = new Vector3( moveVector.x, 0.0f, moveVector.z ).normalized;
transform.LookAt( transform.position + cubeRotDir );
print ("Top");
}
else if( ( transform.up == upStateForward ) || ( transform.up == -upStateForward ) )
{
cubeRotDir = new Vector3( moveVector.x, moveVector.y, 0.0f ).normalized;
transform.LookAt( transform.position + cubeRotDir );
print ("Forward");
}
else if( ( transform.up == upStateRight ) || ( transform.up == -upStateRight ) )
{
cubeRotDir = new Vector3( 0.0f, moveVector.y, moveVector.z ).normalized;
transform.LookAt( transform.position + cubeRotDir );
print ( "Right" );
}
print (cubeRotDir);
anim.SetFloat("Speed", 100);
}
else if( Input.GetKey( KeyCode.UpArrow ) && !(Input.GetKey( KeyCode.DownArrow )) && !(Input.GetKey( KeyCode.RightArrow )) && !(Input.GetKey( KeyCode.LeftArrow )) )
{
if( UpCubeRotate != null )
{
UpCubeRotate( this.gameObject );
}
//cubeRotWait = false;
}
else if( Input.GetKey( KeyCode.DownArrow ) && !(Input.GetKey( KeyCode.UpArrow )) && !(Input.GetKey( KeyCode.RightArrow )) && !(Input.GetKey( KeyCode.LeftArrow )) )
{
if( DownCubeRotate != null )
{
DownCubeRotate( this.gameObject );
}
//cubeRotWait = false;
}
else if( Input.GetKey( KeyCode.RightArrow ) && !(Input.GetKey( KeyCode.DownArrow )) && !(Input.GetKey( KeyCode.UpArrow )) && !(Input.GetKey( KeyCode.LeftArrow )) )
{
if( RightCubeRotate != null )
{
RightCubeRotate( this.gameObject );
}
//cubeRotWait = false;
}
else if( Input.GetKey( KeyCode.LeftArrow ) && !(Input.GetKey( KeyCode.DownArrow )) && !(Input.GetKey( KeyCode.RightArrow )) && !(Input.GetKey( KeyCode.UpArrow )) )
{
if( LeftCubeRotate != null )
{
LeftCubeRotate( this.gameObject );
}
//cubeRotWait = false;
}
}
else
{
anim.SetFloat("Speed", 0);
}
}
}