- Home /
The question is answered, right answer was accepted
Character rolls consistently in all directions, but right.
First of all thank you for reading this. I modified the Penelope controls to move the character and have him face either right or left with one joystick. I have the character moving at another speed with the press of a button which will be a roll animation. When I press the roll button it works fine, except when the character is facing right and moving right. When I try to roll to the right works probably 25 percent of the time. I have no idea where the problem lies and it's getting a pit frustrating. Thank you for all and any help in advance.
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(CharacterController))]
public class BaseControlsCC22 : MonoBehaviour {
public MPJoystick moveJoystick;
public float speed = 6.0f;
public Transform cameraTransform;
public float rollSpeed = 10.0f;
public float gravity = 10.0f;
public GUITexture rollButton;
private Transform myTransform; //our characters transform
private CharacterController character; //getting our characters controller
// Use this for initialization
public virtual void Awake(){
myTransform = transform;
character = GetComponent<CharacterController>();
}
// Update is called once per frame
void Update () {
movement (); //how we move
FaceMovementDirection ();//this is us facing a new direction
}
public virtual void movement(){
Vector3 moveDirection = cameraTransform.TransformDirection(new Vector3( moveJoystick.position.x, 0.0f, moveJoystick.position.y ) );
// We only want the camera-space horizontal direction
moveDirection.y = 0.0f;
moveDirection.Normalize(); // Adjust magnitude after ignoring vertical movement
// Let's use the largest component of the joystick position for the speed.
Vector2 absJoyPos = new Vector2( Mathf.Abs( moveJoystick.position.x ), Mathf.Abs( moveJoystick.position.y ) );
moveDirection *= speed * ( ( absJoyPos.x > absJoyPos.y ) ? absJoyPos.x : absJoyPos.y );
// how to roll
foreach(Touch touch in Input.touches)
{
if ( character.isGrounded )
{
if ( touch.phase == TouchPhase.Began && rollButton.HitTest(touch.position) )
{
// Apply the current movement to roll
moveDirection *= rollspeed * ( ( absJoyPos.x > absJoyPos.y ) ? absJoyPos.x : absJoyPos.y );
}
}
}
moveDirection.y -= gravity * Time.deltaTime;
// Actually move the character
character.Move( moveDirection * Time.deltaTime );
}
public virtual void FaceMovementDirection()
{
Vector3 horizontalGravity = character.velocity;
horizontalGravity.y = 0; // Ignore vertical movement
horizontalGravity.z = 0; // ignore looking on z axis
// If moving significantly in a new direction, point that character in that direction
if ( horizontalGravity.magnitude > 0.1 )
myTransform.forward = horizontalGravity.normalized;
}
}
Answer by fullme7al · Dec 09, 2013 at 12:31 AM
I have a knack for solving my own problems. I fixed it. The floor was slanted so the character wasn't grounded.