- Home /
How to insert a touch guitexture into playercontrols
Hi everyone, thank you for taking a look at this and I hope you can help with this problem. I blended together a couple of character movement scripts and I created this third person mobile game one. Everything I have so far works fine(joystick,movement), but I'm having trouble replacing a pc jumpbutton with a touch guitexture. Here is what I have so far.
[RequireComponent (typeof (Rigidbody))]
[RequireComponent (typeof (CapsuleCollider))]
public class CharacterControls : MonoBehaviour {
public MPJoystick movejoystick; //the jostick we use
public Transform cameraTransform; //this uses the camera position for movement
public GUITexture jumpButton; //this will be the jump button, it is not present in game yet
public float speed = 8.0f;
public float gravity = 6.0f;
public float maxVelocityChange = 20.0f;
public bool canJump = true; //can the character jump?
public float jumpHeight = 2.0f; //how high the character can jump
private bool grounded = false;
private Transform thisTransform;
void Awake () {
//thisTransform = (Transform)GetComponent(typeof(Transform));
rigidbody.freezeRotation = true;
rigidbody.useGravity = false;
}
void Update () {
movement ();
}
public virtual void movement(){
if (grounded) {
// Calculate how fast we should be moving
Vector3 targetVelocity = cameraTransform.TransformDirection(new Vector3(movejoystick.position.x,0,movejoystick.position.y));
//Vector3 targetVelocity = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
targetVelocity = transform.TransformDirection(targetVelocity);
targetVelocity *= speed;
Vector2 absJoyPos = new Vector2( Mathf.Abs( movejoystick.position.x ),
Mathf.Abs( movejoystick.position.y ) );
targetVelocity *= speed * ( ( absJoyPos.x > absJoyPos.y ) ? absJoyPos.x : absJoyPos.y );
// Apply a force that attempts to reach our target velocity
Vector3 velocity = rigidbody.velocity;
Vector3 velocityChange = (targetVelocity - velocity);
velocityChange.x = Mathf.Clamp(velocityChange.x, -maxVelocityChange, maxVelocityChange);
velocityChange.z = Mathf.Clamp(velocityChange.z, -maxVelocityChange, maxVelocityChange);
velocityChange.y = 0;
rigidbody.AddForce(velocityChange, ForceMode.VelocityChange);
// Jump
if (canJump && **Input.GetButton("Jump"))** //I'm unsure how to replace the input button here for a guitexture that would use touch and make it jump
{
rigidbody.velocity = new Vector3(velocity.x, CalculateJumpVerticalSpeed(), velocity.z);
}
}
// We apply gravity manually for more tuning control
rigidbody.AddForce(new Vector3 (0, -gravity * rigidbody.mass, 0));
grounded = false;
}
void OnCollisionStay () {
grounded = true;
}
float CalculateJumpVerticalSpeed () {
// From the jump height and gravity we deduce the upwards speed
// for the character to reach at the apex.
return Mathf.Sqrt(2 * jumpHeight * gravity);
}
}
I have a touch button script, but I'm unsure how to implement it.
void Update () {
touch ();
}
void touch(){
if(Input.touchCount > 0 ){
for(int i = 0; i < Input.touchCount; i++){
Touch touch = Input.GetTouch(i);
if(touch.phase == TouchPhase.Began && guiTexture.HitTest(touch.position)){
//This space is where the action happens when you touch your custom GUI button on
//your device.
}
if(touch.phase == TouchPhase.Ended && guiTexture.HitTest (touch.position)){
//this stops the action
}
}
}
}
}
Please, any and all help is welcome. I've done a lot of tutorials and for some reason I'm thinking I have to use getcomponent, but right now my brain is fried. I've tested the touch script with the camera background and it works for at least that. Thank you for taking your time to read this, and thanks in advance for any help.
I already tried the video you suggested. It disabled the joystick and my character couldn't move. The second site didn't have much. And i prefer Bing. :) Thanks though.
Answer by fullme7al · Sep 19, 2013 at 07:14 AM
I figured it out, and I feel great about it.
Good Job :)
Generally, if you know GUITexture is a part of GUILayer, you're ok