Hi,
I have read every topic about swipe rotation but i still cant do it like what i want.
Here is my script , Third person character movement script and i modified it added camera follow
and i also added swipe rotation but when swipe its just rotate at x very slow
i want to rotate with swipe 90 degree x - axis.
and another problem is swipe rotation working just on object , i want to swipe work on screen anywhere.
i just need 2 things fix but i am so confused pls help.
Here script attached character prefab.
CharacterMovement.js
// Require a character controller to be attached to the same game object
@script RequireComponent(CharacterController)
public var RunAnimation : AnimationClip;
public var SlitherAnimation : AnimationClip;
public var jumpPoseAnimation : AnimationClip;
public var RunMaxAnimationSpeed : float = 1.0;
public var SlitherMaxAnimationSpeed : float = 1.0;
public var jumpMaxAnimationSpeed : float = 1.15;
public var landAnimationSpeed : float = 1.0;
private var _animation : Animation;
// Character States
enum CharacterState {
Run = 0,
Slideing = 1,
Running = 2,
Jumping = 3,
}
private var _characterState : CharacterState;
// The speedup when running
var SlideSpeed = 4.0;
var runSpeed = 6.0;
var inAirControlAcceleration = 3.0;
var jumpHeight = 0.5;
var gravity = 20.0;
var speedSmoothing = 10.0;
var rotateSpeed = 500.0;
var canJump = true;
var swipeSpeed = 0.05F;
var inputX = float;
var inputY : float;
private var jumpRepeatTime = 0.05;
private var jumpTimeout = 0.15;
private var groundedTimeout = 0.25;
private var lockCameraTimer = 0.0;
private var moveDirection = Vector3.zero;
private var verticalSpeed = 0.0;
private var moveSpeed = 0.0;
private var collisionFlags : CollisionFlags;
private var jumping = false;
private var jumpingReachedApex = false;
private var movingBack = false;
private var isMoving = false;
private var SpeedUpTimeStart = 0.0;
private var lastJumpButtonTime = -10.0;
private var lastJumpTime = -1.0;
private var lastJumpStartHeight = 0.0;
private var inAirVelocity = Vector3.zero;
private var lastGroundedTime = 0.0;
private var isControllable = true;
function FixedUpdate()
{
}
function Awake ()
{
moveDirection = transform.TransformDirection(Vector3.forward);
_animation = GetComponent(Animation);
if(!_animation)
Debug.Log("The character you would like to control doesn't have animations.");
if(!RunAnimation) {
_animation = null;
Debug.Log("No run animation found.");
}
if(!SlitherAnimation) {
_animation = null;
Debug.Log("No Slither animation found.");
}
if(!jumpPoseAnimation canJump) {
_animation = null;
Debug.Log("No jump animation found and the character has canJump enabled.");
}
}
function UpdateSmoothedMovementDirection ()
{
var cameraTransform = Camera.main.transform;
var grounded = IsGrounded();
var forward = cameraTransform.TransformDirection(Vector3.forward);
forward.y = 0;
forward = forward.normalized;
var right = Vector3(forward.z, 0, -forward.x);
var v = Input.GetAxisRaw("Vertical");
var h = Input.GetAxisRaw("Horizontal");
var targetDirection = h * right + v * forward;
// This is for swipe rotation i need to fix
if (Input.touchCount > 0 Input.GetTouch(0).phase == TouchPhase.Moved)
{
targetDirection = Input.GetTouch(0).deltaPosition;
inputX == transform.rotation.x;
Debug.Log("X" + transform.rotation.x);
}
if (v < -0.2)
movingBack = true;
else
movingBack = false;
var wasMoving = isMoving;
isMoving = Mathf.Abs (h) > 0.1 || Mathf.Abs (v) > 0.1;
// Grounded Camera controls
if (grounded)
{
lockCameraTimer += Time.deltaTime;
if (isMoving != wasMoving)
lockCameraTimer = 0.0;
if (targetDirection != Vector3.zero)
{
if (moveSpeed < runSpeed * 0.9 grounded)
{
moveDirection = targetDirection.normalized;
}
else
{
moveDirection = Vector3.RotateTowards(moveDirection, targetDirection, rotateSpeed * Mathf.Deg2Rad * Time.deltaTime, 1000);
moveDirection = moveDirection.normalized;
}
}
var curSmooth = speedSmoothing * Time.deltaTime;
var targetSpeed = Mathf.Min(targetDirection.magnitude, 1.0);
// Auto Running
targetSpeed = runSpeed;
_characterState = CharacterState.Run;
moveSpeed = Mathf.Lerp(moveSpeed, targetSpeed, curSmooth);
// Slide Action
if (Input.GetKey (KeyCode.LeftShift) | Input.GetKey (KeyCode.RightShift))
{
targetSpeed = SlideSpeed;
_characterState = CharacterState.Slideing;
moveSpeed = Mathf.Lerp(moveSpeed, targetSpeed, curSmooth);
}
else
{
_characterState = CharacterState.Running;
}
if (moveSpeed < SlideSpeed * 0.3)
SpeedUpTimeStart = Time.time;
}
else
{
if (jumping)
lockCameraTimer = 0.0;
if (isMoving)
inAirVelocity += targetDirection.normalized * Time.deltaTime * inAirControlAcceleration;
}
}
// Jump Section
function ApplyJumping ()
{
if (lastJumpTime + jumpRepeatTime > Time.time)
return;
if (IsGrounded())
{
if (canJump Time.time < lastJumpButtonTime + jumpTimeout)
{
verticalSpeed = CalculateJumpVerticalSpeed (jumpHeight);
SendMessage("DidJump", SendMessageOptions.DontRequireReceiver);
}
}
}
// Gravity Section
function ApplyGravity ()
{
if (isControllable)
{
var jumpButton = Input.GetButton("Jump");
if (jumping !jumpingReachedApex verticalSpeed <= 0.0)
{
jumpingReachedApex = true;
SendMessage("DidJumpReachApex", SendMessageOptions.DontRequireReceiver);
}
if (IsGrounded ())
verticalSpeed = 0.0;
else
verticalSpeed -= gravity * Time.deltaTime;
}
}
function CalculateJumpVerticalSpeed (targetJumpHeight : float)
{
return Mathf.Sqrt(2 * targetJumpHeight * gravity);
}
function DidJump ()
{
jumping = true;
jumpingReachedApex = false;
lastJumpTime = Time.time;
lastJumpStartHeight = transform.position.y;
lastJumpButtonTime = -10;
_characterState = CharacterState.Jumping;
}
function Update()
{
if (!isControllable)
{
Input.ResetInputAxes();
}
if (Input.GetButtonDown ("Jump"))
{
lastJumpButtonTime = Time.time;
}
UpdateSmoothedMovementDirection();
ApplyGravity ();
ApplyJumping ();
var movement = moveDirection * moveSpeed + Vector3 (0, verticalSpeed, 0) + inAirVelocity;
movement *= Time.deltaTime;
var controller : CharacterController = GetComponent(CharacterController);
collisionFlags = controller.Move(movement);
if(_animation)
{
if(_characterState == CharacterState.Jumping)
{
if(!jumpingReachedApex)
{
_animation[jumpPoseAnimation.name].speed = jumpMaxAnimationSpeed;
_animation[jumpPoseAnimation.name].wrapMode = WrapMode.ClampForever;
_animation.CrossFade(jumpPoseAnimation.name);
}
else
{
_animation[jumpPoseAnimation.name].speed = -landAnimationSpeed;
_animation[jumpPoseAnimation.name].wrapMode = WrapMode.ClampForever;
_animation.CrossFade(jumpPoseAnimation.name);
}
}
else
{
if(controller.velocity.sqrMagnitude < 0.1)
{
_animation.CrossFade(RunAnimation.name);
}
else
{
if(_characterState == CharacterState.Running)
{
_animation[RunAnimation.name].speed = Mathf.Clamp(controller.velocity.magnitude, 0.0, RunMaxAnimationSpeed);
_animation.CrossFade(RunAnimation.name);
}
else if(_characterState == CharacterState.Slideing)
{
_animation[SlitherAnimation.name].speed = Mathf.Clamp(controller.velocity.magnitude, 0.0, SlitherMaxAnimationSpeed);
_animation.CrossFade(SlitherAnimation.name);
}
else if(_characterState == CharacterState.Jumping)
{
_animation[jumpPoseAnimation.name].speed = Mathf.Clamp(controller.velocity.magnitude, 0.0, jumpMaxAnimationSpeed);
_animation.CrossFade(jumpPoseAnimation.name);
}
}
}
}
// Animation sector
if (IsGrounded())
{
transform.rotation = Quaternion.LookRotation(moveDirection);
}
else
{
var xzMove = movement;
xzMove.y = 0;
if (xzMove.sqrMagnitude > 0.001)
{
transform.rotation = Quaternion.LookRotation(xzMove);
}
}
if (IsGrounded())
{
lastGroundedTime = Time.time;
inAirVelocity = Vector3.zero;
if (jumping)
{
jumping = false;
SendMessage("DidLand", SendMessageOptions.DontRequireReceiver);
}
}
}
function OnControllerColliderHit (hit : ControllerColliderHit )
{
if (hit.moveDirection.y > 0.01)
return;
}
function GetSpeed ()
{
return moveSpeed;
}
function IsJumping ()
{
return jumping;
}
function IsGrounded ()
{
return (collisionFlags CollisionFlags.CollidedBelow) != 0;
}
function GetDirection ()
{
return moveDirection;
}
function IsMovingBackwards ()
{
return movingBack;
}
function GetLockCameraTimer ()
{
return lockCameraTimer;
}
function IsMoving () : boolean
{
return Mathf.Abs(Input.GetAxisRaw("Vertical")) + Mathf.Abs(Input.GetAxisRaw("Horizontal")) > 0.5;
}
function HasJumpReachedApex ()
{
return jumpingReachedApex;
}
function IsGroundedWithTimeout ()
{
return lastGroundedTime + groundedTimeout > Time.time;
}
function Reset ()
{
gameObject.tag = "Player";
}