- Home /
Problem with rotating a character that has faux gravity
Hi, I am using scripts from the following source: http://forum.unity3d.com/threads/8873-Faux-Gravity-making-my-brain-spin...-Help! This basically sets up a faux gravity system where objects will be pulled towards others as if they had their own central field of gravity. Very useful for achieving similar things to Super Mario Galaxy.
But anyway, my problem is that while I may have a moving character that can be moved forward, back, left and right with the arrow keys along my planet, I would love to be able to rotate the character using the left and right arrow keys, which in turn moves the camera attached to the player object. This would allow for better maneuvering of the character.
So far I have made some progress with it, the following code has been added to the project and calls the gravity script attached to the player, with if statements checking when a jump has occurred, so that the player cannot rotate in mid air. The dilemma is that because the player is moving around a planet, its rotation changes quite a lot; trying to change its rotation with simple script is ineffective as it still rotates it in respect to the game axes. My code is as follows:
using UnityEngine; using System.Collections;
public class PlayerTurn : MonoBehaviour { public float speed; public GameObject player; public PlayerGravityBody gravBody;
public void Update()
{
Vector3 turnAngles = player.transform.rotation.eulerAngles;
if(Input.GetKey ("left") && gravBody.doJump == false)
{
Debug.Log(turnAngles.y);
turnAngles.y -= speed;
}
if(Input.GetKey ("right") && gravBody.doJump == false)
{
Debug.Log(turnAngles.y);
turnAngles.y += speed;
}
}
}
this works okay until you start to pass the equator of the planet and then the rotations seem to reverse.
any help with this would be great :)
the player gameObject is the character. the turnAngles Vector3 is declared as the player gameObject's rotation eulerAngles and is then edited in the if statements when both a key is pressed and the character is not jumping
Answer by Dakwamine · Mar 20, 2012 at 12:15 AM
I am not a C# developer, but it seems obvious that you are not setting the player rotation. You are just setting the variable turnAngles.
Besides that, could you try to use transform.Rotate() in Space.Self
in order to rotate the character?
I've tried transform.rotate() like you suggested but I'm not sure how to change the y rotation incrementally with this. The character just seems to spin frantically on the spot.
Scratch that, I've appended the code again and it is getting closer to functioning as I would like. I have the following now:
using UnityEngine; using System.Collections;
public class PlayerTurn : $$anonymous$$onoBehaviour { public float speed; public GameObject player; public PlayerGravityBody gravBody; Vector3 turnAngles = new Vector3(0, 0, 0);
public void Update()
{
if(Input.Get$$anonymous$$ey ("left") && gravBody.doJump == false)
{
Debug.Log(turnAngles.y);
turnAngles.y -= speed;
transform.Rotate(turnAngles, Space.Self);
}
if(Input.Get$$anonymous$$ey ("right") && gravBody.doJump == false)
{
Debug.Log(turnAngles.y);
turnAngles.y += speed;
transform.Rotate(turnAngles, Space.Self);
}
}
}
where speed is set to 0.1 in the inspector.
The problem is though, it still seems to rotate the character in strange ways. It does seem to turn it on its y-axis properly but at points on the planet, the left and right keys have varying effects.
seem to have fixed it now, needed to change the second if statement to else if and then add an else statement below it to reset the y component of turnAngles