Good Afternoon,
As the title says, i’ve been trying to force the character to jump and move with GUITextures in the android, i have it working with a rigidbody player prefab but i wanted to change it into Character Controler, Character Motor one not knowing if FPS Input Controller gets in it but not sure,
using UnityEngine;
using System.Collections;
public class PlayerMovement : MonoBehaviour {
public float speed=30, jumpheight=30;
public bool isMoving;
CharacterAnimationScript pl;
Transform myTrans;
Rigidbody myBody;
Vector3 movement;
public bool isGrounded;
int move;
void Start () {
pl = FindObjectOfType<CharacterAnimationScript> ();
myTrans = this.transform;
myBody = this.rigidbody;
}
void Update () {
#if !UNITY_ANDROID && !UNITY_IPHONE && !UNITY_BLACKBERRY && !UNITY_WINRT
Move (Input.GetAxisRaw("Horizontal"));
if (Input.GetButtonDown ("Jump"))
Jump ();
#endif
}
public void Move(float horizontal){
this.rigidbody.constraints=RigidbodyConstraints.FreezeRotation|RigidbodyConstraints.FreezePositionZ;
if (horizontal == 0)
isMoving = false;
else
isMoving = true;
pl.action = CharacterAnimationScript.AnimationState.run;
movement = myBody.velocity;
movement.x = horizontal * speed * Time.deltaTime;
myBody.velocity = movement;
if (horizontal > 0 && !pl.facingRight)
pl.Flip ();
else if (horizontal < 0 && pl.facingRight) {
pl.Flip ();
}
}
public void Jump(){
if (isGrounded && pl.curState!=CharacterAnimationScript.AnimationState.jump){
myBody.velocity += jumpheight * Vector3.up * Time.deltaTime;
pl.isJumping=true;
pl.action = CharacterAnimationScript.AnimationState.jump;
}
}
void OnCollisionEnter(Collision theCollision){
if(theCollision.gameObject.name == "floor" || theCollision.gameObject.name == "Platform" || theCollision.gameObject.name == "BreakRock"|| theCollision.gameObject.name == "stairs")
{
isGrounded = true;
if(!isMoving)
this.rigidbody.constraints=RigidbodyConstraints.FreezeRotation|RigidbodyConstraints.FreezePositionX|RigidbodyConstraints.FreezePositionZ;
else
this.rigidbody.constraints=RigidbodyConstraints.FreezeRotation|RigidbodyConstraints.FreezePositionZ;
}
}
void OnCollisionStay(Collision theCollision){
if(theCollision.gameObject.name == "floor" || theCollision.gameObject.name == "Platform" || theCollision.gameObject.name == "BreakRock"|| theCollision.gameObject.name == "stairs")
{
isGrounded = true;
if(theCollision.gameObject.name == "stairs"){
if(!isMoving)
this.rigidbody.constraints=RigidbodyConstraints.FreezeRotation |RigidbodyConstraints.FreezePositionX|RigidbodyConstraints.FreezePositionZ;
else
this.rigidbody.constraints=RigidbodyConstraints.FreezeRotation|RigidbodyConstraints.FreezePositionZ;
}
}
if(theCollision.gameObject.name == "Platform"){
transform.parent = theCollision.transform ;
}else
transform.parent = null;
}
//consider when character is jumping .. it will exit collision.
void OnCollisionExit(Collision theCollision){
if(theCollision.gameObject.name == "floor" || theCollision.gameObject.name == "Platform" || theCollision.gameObject.name == "BreakRock"|| theCollision.gameObject.name == "stairs")
{
isGrounded = false;
this.rigidbody.constraints=RigidbodyConstraints.FreezeRotation|RigidbodyConstraints.FreezePositionZ;
}
}
}
This is teh script to move teh rigidbody, beign a standard for pretty much all rigidbodies,
and this is one of my GUITextures
public class TouchJump : TouchLogic {
PlayerMovement player;
CharacterAnimationScript planim;
#if UNITY_ANDROID || UNITY_IPHONE
void Start(){
player = FindObjectOfType<PlayerMovement> ();
planim = FindObjectOfType<CharacterAnimationScript> ();
}
public override void OnTouchBegan(){
if (player == null) {
player = FindObjectOfType<PlayerMovement> ();
planim = FindObjectOfType<CharacterAnimationScript> ();
}
touch2Watch = currTouch;
if (player.isGrounded) {
planim.isJumping=true;
player.Jump ();
planim.action=CharacterAnimationScript.AnimationState.jump;
}
}
#endif
}
How can i make it so that teh touch forces teh character motor to jump ??
Really need some help on this :\