- Home /
Joystick controls - camera rotating problem
I am using the camera relative dual joystick controls and am trying to change the code a little. Mainly, I would like the camera to always be behind the character and there to only be the movement joystick. I tried setting my camera to face forwards and disabled my rotation joystick to get the effect I'm looking for. Here's the problem though - The player will face the direction the move joystick is facing and will walk in that direction, causing the camera to rotate behind the character again. This means that the forward direction has now been updated and if the player is still holding the joystick in that position, the camera and character will rotate again - causing a nonendless spinning fiasco that will only stop once the joystick isn't making the character rotate anymore.. Is there a way to keep the camera behind the player and not have him rotate according to the current camera position or so? Maybe rotate the character in world space and not local space and assign each direction of the joystick to a set (and never changing) direction? I haven't found anything helpful yet and I've been trying for a while now. Could the world space solution work? Thanks in advance
The code works fine, but I would like the camera to always stay behind the character, which would be easy to do were it not for the "spinning problem". The code here makes the player move along the z axes of the camera when the move joystick is pushed fully forwards/upwards. Thus it also makes the player turn left (left seen from camera) when the joystick is turned to the left. This is all good but if the camera also turns to the left to be behind the player again, then the "first left" will now be the cameras z/forward axes and the "second left" will make the player and camera turn again.. Basically every frame where the player is pressing to the left the character will do 90* turn (unless the camera isn't fixed to be behind the charcater at all times)...
Here's the code(rotation joystick has been disabled):
@script RequireComponent( CharacterController )
var moveJoystick : Joystick;
var rotateJoystick : Joystick; //no longer needed
var cameraPivot : Transform; // The transform used for camera rotation
var cameraTransform : Transform; // The actual transform of the camera
var Gravity : int;
var touchZoneWidth : int = 100;
var touchZoneHeight : int = 100;
private var AttackAreaBounds : Rect;
private var AttackAreaCenter : Vector2;
private var AttackAreaDown : boolean = false;
var speed : float = 8; // Ground speed
var jumpSpeed : float = 8;
var inAirMultiplier : float = 0.25; // Limiter for ground speed while jumping
var rotationSpeed : Vector2 = Vector2( 50, 25 ); // Camera rotation speed for each axis
private var thisTransform : Transform;
private var character : CharacterController;
private var animationController : AnimationController;
private var velocity : Vector3; // Used for continuing momentum while in air
private var waited : boolean = true;
function Start()
{
//rotateJoystick.Disable(); //used for now to disable rotation!!!!!
// set the bounds at start, so we can see these in the inspector
// maybe pull the left button up and inwards a bit ??
AttackAreaBounds = Rect(0, 0, touchZoneWidth, touchZoneHeight);
AttackAreaCenter = Vector2 (touchZoneWidth * 0.5, touchZoneHeight * 0.5);
// Cache component lookup at startup instead of doing this every frame
thisTransform = GetComponent( Transform );
character = GetComponent( CharacterController );
animationController = GetComponent( AnimationController );
// Set the maximum speed, so that the animation speed adjustment works
SpikesAnimControls.maxForwardSpeed = speed;
// Move the character to the correct start position in the level, if one exists
var spawn = GameObject.Find( "PlayerSpawn" );
if ( spawn )
thisTransform.position = spawn.transform.position;
}
function FaceMovementDirection()
{
var horizontalVelocity : Vector3 = character.velocity;
horizontalVelocity.y = 0; // Ignore vertical movement
// If moving significantly in a new direction, point that character in that direction
if ( horizontalVelocity.magnitude > 0.1 )
{
thisTransform.forward = horizontalVelocity.normalized ;
}
}
function OnEndGame()
{
// Disable joystick when the game ends
moveJoystick.Disable();
rotateJoystick.Disable();
// Don't allow any more control changes when the game ends
this.enabled = false;
}
function Update()
{
var movement = cameraTransform.TransformDirection( Vector3( moveJoystick.position.x, 0, moveJoystick.position.y ) );
// We only want the camera-space horizontal direction
movement.y = 0;
movement.Normalize(); // Adjust magnitude after ignoring vertical movement
// Let's use the largest component of the joystick position for the speed.
var absJoyPos = Vector2( Mathf.Abs( moveJoystick.position.x ), Mathf.Abs( moveJoystick.position.y ) );
movement *= speed * ( ( absJoyPos.x > absJoyPos.y ) ? absJoyPos.x : absJoyPos.y );
// Check for jump
if ( character.isGrounded )
{
if ( rotateJoystick.tapCount == 2 )
{
// Apply the current movement to launch velocity
velocity = character.velocity;
velocity.y = jumpSpeed;
//Debug.Log(velocity.y);
}
SpikesAnimControls.PlaySlowFallAnim = false;
}
else
{
if ( rotateJoystick.tapCount == 4 )
{
SpikesAnimControls.PlaySlowFallAnim = true;
velocity.y += Gravity * Time.deltaTime;
// Adjust additional movement while in-air
movement.x *= inAirMultiplier;
movement.z *= inAirMultiplier;
}
else
{
// Apply gravity to our velocity to diminish it over time
velocity.y += Physics.gravity.y * Time.deltaTime;
// Adjust additional movement while in-air
movement.x *= inAirMultiplier;
movement.z *= inAirMultiplier;
}
}
movement += velocity;
movement += Physics.gravity;
movement *= Time.deltaTime;
// Actually move the character
character.Move( movement );
if ( character.isGrounded )
// Remove any persistent velocity after landing
velocity = Vector3.zero;
// Face the character to match with where she is moving
FaceMovementDirection();
// Scale joystick input with rotation speed
//var camRotation = transform.position;
var camRotation = rotateJoystick.position;
camRotation.x *= rotationSpeed.x;
camRotation.y *= rotationSpeed.y;
camRotation *= Time.deltaTime;
// Rotate around the character horizontally in world, but use local space
// for vertical rotation
cameraPivot.Rotate( 0, -camRotation.x, 0, Space.World );
cameraPivot.Rotate( camRotation.y, 0, 0 );
}
hey i m looking for same problem is this solved can u help me ?
I'm afraid I never found a solution for this problem. I ins$$anonymous$$d ended up using two joysticks and some gestures on the screen for the controls. http://www.youtube.com/watch?v=cvSnRyaxwug
thanx .. i thought may be there might be a solution but i guess i have to either buy a joystick asset else use two joysticks..
Answer by aldonaletto · Dec 30, 2011 at 01:25 PM
I'm not sure if I understood exactly what you want to do, but if you just want a camera to follow the player from behind, you can child the camera to the player, set its local position (that's what appears in the Inspector Position) to something like 0,0,-4, and disable the camera control scripts. If you want to control the camera direction, add the MouseLook script to it - you will be able to aim to different directions without loosing the position relative to the player.
Actually the problem is that i cant get controls working where the camera is always behind the player and the rotation / movement is controlled via a single joystick, the problem im facing is that the forward direction of the player is deter$$anonymous$$ed by the camera rotation and joystick position - when the player pushes the joystick to the side the character will go rotating in that direction each frame, ins$$anonymous$$d of just once. This causes the player to go spinning round and round.. I ve practically given up now though - it seems it is quite impossible (sorry if this doesn't make much sense its hard to describe)
Can you edit your question and post the script there? It's hard to guess what exactly the script is currently doing.
Answer by AaronSaron · Dec 06, 2012 at 05:20 PM
"when the player pushes the joystick to the side the character will go rotating in that direction each frame, instead of just once" I have the same problem. Does anyone know the solution?
Your answer
Follow this Question
Related Questions
Move towards VR Camera Orientation with Bluetooth Controller (Android) 0 Answers
Rotation of player based on camera direction and joystick direction in 3D world 1 Answer
Rotating a ship with joystick and keeping camera centered. 0 Answers
How to deal with joystick moving and screen swiping colliding? 0 Answers
advanced problem for me regarding transfering rotation from the gizmo to the object 0 Answers