we have a dash button in the game. If we press the button dash button is working well but the problem is if we press anywhere on screen it is working too! We only want to work that function with button.
Anyone help us please?
Code:
public void Dash()
{
if (Input.GetMouseButtonDown(0))
{
activeMoveSpeed = dashSpeed;
dashCounter = dashLength;
}
if (dashCounter > 0)
{
canShoot = false;
dashCounter -= Time.deltaTime;
if (dashCounter <= 0)
{
canShoot = false;
activeMoveSpeed = speed;
dashCoolCounter = dashCoolDown;
}
}
if (dashCoolCounter > 0)
{
canShoot = false;
dashCoolCounter -= Time.deltaTime;
}
}
private void FixedUpdate()
{
Movement();
Dash();
}
Button and Game
Button
The function you specify in OnClick will be called whenever the button is clicked.
Currently, you are not using the button at all. I think you meant something like this (StartDash() is the function that should be chosen in OnClick):
public void StartDash()
{
activeMoveSpeed = dashSpeed;
dashCounter = dashLength;
}
private void DoDash()
{
if (dashCounter > 0)
{
canShoot = false;
dashCounter -= Time.deltaTime;
if (dashCounter <= 0)
{
canShoot = false;
activeMoveSpeed = speed;
dashCoolCounter = dashCoolDown;
}
}
if (dashCoolCounter > 0)
{
canShoot = false;
dashCoolCounter -= Time.deltaTime;
}
}
private void FixedUpdate()
{
Movement();
DoDash();
}
Take a look at the related pages in the Unity Documentation:
Manual - Button
Scripting - UI.Button
Scripting - UI.Button.onclick
Related
My player GameObject is AlienPlayer
i want to move player by click of the button
code works on keyboard controls but not on click of a button
This is my update():
void Update()
{
movement = Input.GetAxis ("Horizontal");
if (movement > 0f) {
left();
}
else if (movement < 0f) {
right();
}
else {
rigidBody.velocity = new Vector2 (0,rigidBody.velocity.y);
}
if (moveLeft) { left(); }
else if (moveRight) { right(); }
}
public void left()
{
rigidBody.velocity = new Vector2(movement*speed, rigidBody.velocity.y);
//rigidBody.AddForce(transform.forward * speed, ForceMode2D.Force);
transform.localScale = new Vector2(2f, 2f);
}
public void right()
{
rigidBody.velocity = new Vector2(movement * speed, rigidBody.velocity.y);
transform.localScale = new Vector2(-2f, 2f);
}
And this for event trigger:
public void LeftBtnDown()
{
moveLeft = true;
}
public void LeftBtnUp()
{
moveLeft = false;
}
public void RightBtnDown()
{
moveRight = true;
}
public void RightBtnUp()
{
moveRight = false;
}
**I want to make this control for android **
Your code seems to be logically functional, my first guess would be that you don't have an event system any where in your scene.
You can create one simply by clicking on the + in the hierarchy menu -> UI -> EventSystem.
I don't believe the code is the problem, but if the EventSystem does not turn out to be the issue, you could try changing your Update method to this:
void Update()
{
movement = Input.GetAxis ("Horizontal");
if (movement > 0f || moveLeft) {
left();
}
else if (movement < 0f || moveRight) {
right();
}
else {
rigidBody.velocity = new Vector2 (0,rigidBody.velocity.y);
}
}
If that does not work, I would need additional information to help you.
I'm working on a character controller, everything works fine except two things and I can't find a way to solve this :(
This is the code of my controller script :
using UnityEngine;
public class ControlsManager : MonoBehaviour
{
public Transform playerBody;
private Rigidbody _rigidbody;
private Transform _playerCamera;
private Vector2 _mousePosition;
private Vector2 _precMousePosition;
private float _rJoystickX;
private float _rJoystickY;
private float _sprint;
private bool _jump;
private Vector3 _bodyTranslation;
private bool _bodyTranslationChange;
private Vector3 _bodyRotation;
private bool _bodyRotationChange;
private Vector3 _cameraRotation;
private bool _cameraRotationChange;
private void Awake()
{
_rigidbody = playerBody.GetComponent<Rigidbody>();
_playerCamera = Utilities.mainCamera.transform;
_mousePosition = Input.mousePosition;
}
private void Update()
{
_sprint = 1;
// Cursor lock for camera rotation
if (Input.GetKeyDown(Utilities.controls.lockCursorMouse))
{
if (Cursor.lockState == CursorLockMode.None)
{
Cursor.visible = false;
Cursor.lockState = CursorLockMode.Locked;
}
else
{
Cursor.visible = true;
Cursor.lockState = CursorLockMode.None;
}
}
if (!Utilities.isGamePaused)
{
if (Input.GetJoystickNames().Length > 0 && Input.GetJoystickNames()[0] != "")
{
// Camera rotation for controller
_rJoystickX = Input.GetAxis("RJoystickX");
_rJoystickY = Input.GetAxis("RJoystickY");
if (_rJoystickX != 0) {
_bodyRotation.y += Utilities.controls.controllerSens * _rJoystickX * Time.deltaTime;
_bodyRotationChange = true;
}
if (_rJoystickY != 0)
{
_cameraRotation.x += Utilities.controls.controllerSens * _rJoystickY * Time.deltaTime;
_cameraRotationChange = true;
}
// Movements for controller
if(Input.GetKey(Utilities.controls.sprintController))
{
_sprint = 1.6f;
}
if (Input.GetAxis("LJoystickY") > 0)
{
_bodyTranslation += playerBody.forward * _sprint * Time.deltaTime;
_bodyTranslationChange = true;
}
if (Input.GetAxis("LJoystickY") < 0)
{
_bodyTranslation -= playerBody.forward * Time.deltaTime;
_bodyTranslationChange = true;
}
if (Input.GetAxis("LJoystickX") < 0)
{
_bodyTranslation -= playerBody.right * Time.deltaTime;
_bodyTranslationChange = true;
}
if (Input.GetAxis("LJoystickX") > 0)
{
_bodyTranslation += playerBody.right * Time.deltaTime;
_bodyTranslationChange = true;
}
}
if (Cursor.lockState == CursorLockMode.Locked)
{
// Camera rotation for mouse
_precMousePosition = _mousePosition;
_mousePosition.x = Input.GetAxis("Mouse X");
_mousePosition.y = Input.GetAxis("Mouse Y");
if (_mousePosition.x != _precMousePosition.x)
{
_bodyRotation.y += _mousePosition.x * Utilities.controls.mouseSens;
_bodyRotationChange = true;
}
if (_mousePosition.y != _precMousePosition.y)
{
_cameraRotation.x += -_mousePosition.y * Utilities.controls.mouseSens;
_cameraRotationChange = true;
}
// Movements for mouse
_sprint = 1;
if(Input.GetKey(KeyCode.LeftShift))
{
_sprint = 1.6f;
}
if (Input.GetKeyDown(KeyCode.Space))
{
_jump = true;
}
if (Input.GetKey(Utilities.controls.forward))
{
_bodyTranslation += playerBody.forward * (_sprint * Time.deltaTime);
_bodyTranslationChange = true;
}
if (Input.GetKey(Utilities.controls.backward))
{
_bodyTranslation -= playerBody.forward * Time.deltaTime;
_bodyTranslationChange = true;
}
if (Input.GetKey(Utilities.controls.left))
{
_bodyTranslation -= playerBody.right * Time.deltaTime;
_bodyTranslationChange = true;
}
if (Input.GetKey(Utilities.controls.right))
{
_bodyTranslation += playerBody.right * Time.deltaTime;
_bodyTranslationChange = true;
}
///////////////////////////////// Debug
if (Input.GetKey(KeyCode.DownArrow))
{
_bodyTranslation -= playerBody.up * Time.deltaTime;
_bodyTranslationChange = true;
}
if (Input.GetKey(KeyCode.UpArrow))
{
_bodyTranslation += playerBody.up * Time.deltaTime;
_bodyTranslationChange = true;
}
}
}
// Rotations
if (_bodyRotationChange)
{
playerBody.localRotation *= Quaternion.Euler(_bodyRotation);
}
_bodyRotationChange = false;
_bodyRotation = Vector3.zero;
if (_cameraRotationChange)
{
_playerCamera.localRotation *= Quaternion.Euler(_cameraRotation);
}
_cameraRotationChange = false;
_cameraRotation = Vector3.zero;
if (Input.GetKeyDown(KeyCode.Escape))
{
Application.Quit();
}
}
private void FixedUpdate()
{
if (_bodyTranslationChange)
{
_rigidbody.AddForce(_bodyTranslation * Utilities.controls.playerSpeed, ForceMode.VelocityChange);
}
if (_jump)
{
_jump = false;
_rigidbody.AddForce(playerBody.up * 500, ForceMode.Impulse);
}
_rigidbody.drag = playerBody.position.y > -0.6f ? 1 : 8;
_bodyTranslationChange = false;
_bodyTranslation = Vector3.zero;
}
}
The main two problems are :
As I do rotations in Update, and movements in FixedUpdates, there is some annoying jitter when I both move and rotate at the same time. I tried to show this in a gif but everything seems laggy so I can't really show it.
As I use AddForce() to move my character, I had to increase the drag of my rigidbody (up to 8) si it doesn't "slide" when you stop moving. This works great but now I can't jump properly because of that. I found a workaround, by putting the drag back to 1 when I'm in the air, but then if I jump and move, I move 8x faster in the air which is annoying aswell.
Am I doing thinks completely wrong ? I first did not use AddForce but normal vector translations to move the character, but collisions were buggy as hell, I could go through most walls, objects... etc
Thanks if you read all that !
when working with Pyhsics (RB) NEVER change the transform directly and never ever to this in Update! all changes have to be made inside FixedUpdate!
playerBody.localRotation *= Quaternion.Euler(_bodyRotation); <-- in Update.
Also use Rigidbody.MoveRotation instead of directy change values.
This happens because of pyhsics will work in a different update process and when you change Values within this process unity gets confused and you break the physics -> Jitter
Ok, after hours of trying to make something smooth with Rigidbody, I can't get anything perfect.
I'll just use a basic Character controller and handle collisions myself.
Thanks anyway !
I made the game in Unity Space Shooter. In my Space shooter there is 2 button it work for Left and Right moving. I want when we touch the left button player go to left only in Single Touch same like Right Button also.
This , are the some codes which i used in Game. Please Help me out from this.
TouchControl.cs
using UnityEngine;
using System.Collections;
public class TouchControl : MonoBehaviour {
public GUITexture moveLeft;
public GUITexture moveRight;
public GUITexture fire;
public GameObject player;
private PlayerMovement playerMove;
private Weapon[] weapons;
void Start()
{
playerMove = player.GetComponent<PlayerMovement> ();
}
void CallFire()
{
weapons = player.GetComponentsInChildren<Weapon> ();
foreach (Weapon weapon in weapons) {
if(weapon.enabled == true)
weapon.Fire();
}
}
void Update()
{
// int i = 0;
if(Input.touchCount > 0)
{
for(int i =0; i < Input.touchCount; i++)
{
// if(moveLeft.HitTest(Input.GetTouch(i).position, Camera.main))
// {
// if(Input.touchCount > 0)
// {
// playerMove.MoveLeft();
// }
// }
// if(moveRight.HitTest(Input.GetTouch(i).position, Camera.main))
// {
// if(Input.touchCount > 0)
// {
// playerMove.MoveRight();
// }
// }
// if(moveLeft.HitTest(Input.GetTouch(i).position, Camera.main))
// {
// if(Input.touchCount > 0)
// {
// CallFire();
// }
// }
// Touch t = Input.GetTouch(i);
Touch t = Input.GetTouch (i);
Input.multiTouchEnabled = true;
if(t.phase == TouchPhase.Began || t.phase == TouchPhase.Stationary)
{
if(moveLeft.HitTest(t.position, Camera.main))
{
playerMove.MoveLeft ();
}
if(moveRight.HitTest(t.position, Camera.main))
{
playerMove.MoveRight();
}
}
if(t.phase == TouchPhase.Began)
{
if(fire.HitTest(t.position, Camera.main))
{
CallFire();
}
}
if(t.phase == TouchPhase.Ended)
{
}
}
}
}
}
PlayerMovement.cs
using UnityEngine;
using System.Collections;
public class PlayerMovement : MonoBehaviour {
public float speedMove = 6.0f;
public float bonusTime;
private bool toLeft = false;
private bool toRight = false;
public GameObject shield;
public GUIText bonustimeText;
private bool counting = false;
private float counter;
private Weapon[] addWeapons;
public Sprite strongShip;
public Sprite normalSprite;
public Sprite shieldSprite;
private SpriteRenderer sRender;
private Weapon weaponScript;
void Start () {
counter = bonusTime;
sRender = GetComponent<SpriteRenderer> ();
addWeapons = GetComponentsInChildren<Weapon> ();
foreach (Weapon addWeapon in addWeapons) {
addWeapon.enabled = false;
}
weaponScript = GetComponent<Weapon>();
weaponScript.enabled = true;
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown (KeyCode.A)) {
toLeft = true;
}
if (Input.GetKeyUp (KeyCode.A)) {
toLeft = false;
}
if (Input.GetKeyDown (KeyCode.D)) {
toRight = true;
}
if (Input.GetKeyUp (KeyCode.D)) {
toRight = false;
}
if (counting) {
counter -= Time.deltaTime;
bonustimeText.text = counter.ToString("#0.0");
}
}
void FixedUpdate()
{
if (toLeft) {
MoveLeft();
}
if (toRight) {
MoveRight();
}
}
public void MoveLeft()
{
transform.Translate(Vector2.right * -speedMove* Time.deltaTime);
}
public void MoveRight()
{
transform.Translate(Vector2.right * speedMove * Time.deltaTime);
}
void OnCollisionEnter2D(Collision2D coll)
{
if (coll.gameObject.tag == "StrongMode") {
Destroy (coll.gameObject);
counting = true;
StrongMode();
Invoke ("Downgrade", bonusTime);
}
if (coll.gameObject.tag == "ShieldMode") {
Destroy (coll.gameObject);
counting = true;
ShieldMode();
Invoke("Downgrade", bonusTime);
}
if (coll.gameObject.tag == "Life") {
GUIHealth gui = GameObject.Find ("GUI").GetComponent<GUIHealth> ();
gui.AddHealth();
SendMessage("AddHp");
SoundHelper.instanceSound.PickUpSound();
Destroy(coll.gameObject);
}
if (coll.gameObject.tag == "Enemy") {
SendMessage("Dead");
}
}
void Downgrade()
{
SoundHelper.instanceSound.BonusDownSound ();
counting = false;
bonustimeText.text = "";
counter = bonusTime;
sRender.sprite = normalSprite;
weaponScript.enabled = true;
foreach (Weapon addWeapon in addWeapons) {
addWeapon.enabled = false;
}
weaponScript.enabled = true;
shield.SetActive (false);
}
void StrongMode()
{
SoundHelper.instanceSound.BonusUpSound ();
sRender.sprite = strongShip;
foreach (Weapon addWeapon in addWeapons) {
addWeapon.enabled = true;
}
weaponScript.enabled = false;
}
void ShieldMode()
{
SoundHelper.instanceSound.BonusUpSound ();
sRender.sprite = shieldSprite;
shield.SetActive (true);
}
// void OnDestroy()
// {
// bonustimeText.text = "";
// }
}
In the Player Controller script Create:
public Vector3 playerDirection = Vector3.zero;
Then in touch control instead of:
if (moveLeft.HitTest(Input.GetTouch(i).position, Camera.main))
{
if (Input.touchCount > 0)
{
playerMove.MoveLeft();
}
}
if (moveRight.HitTest(Input.GetTouch(i).position, Camera.main))
{
if (Input.touchCount > 0)
{
playerMove.MoveRight();
}
}
Use:
if (moveLeft.HitTest(Input.GetTouch(i).position, Camera.main))
{
if (Input.touchCount > 0)
{
playerMove.playerDirection = Vector3.left;
}
}
if (moveRight.HitTest(Input.GetTouch(i).position, Camera.main))
{
if (Input.touchCount > 0)
{
playerMove.playerDirection = Vector3.right;
}
}
Then in the Update method of Player Controller use:
transform.Translate(playerDirection * speedMove * Time.deltaTime);
public class PlayerController {
public EPlayerState playerState = EPLayerState.Idle;
void Update () {
// If click right button
playerState = EPlayerState.MoveRight;
// Else if click left button
playerState = EPlayerState.MoveLeft
if (playerState == EPlayerState.MoveRight)
// Move player right;
if (playerState == EPlayerState.MoveLeft
// Move player right;
}
}
public enum EPlayerState {
Idle,
MoveRight,
MoveLeft
}
You can also use something like a boolean called isRight, move right when it's true and left when it's false. Then when you click left or right button just change the variable.
`using UnityEngine;
public class HalfScreenTouchMovement : MonoBehaviour
{
private float screenCenterX;
private void Start()
{
// save the horizontal center of the screen
screenCenterX = Screen.width * 0.5f;
}
private void Update()
{
// if there are any touches currently
if(Input.touchCount > 0)
{
// get the first one
Touch firstTouch = Input.GetTouch(0);
// if it began this frame
if(firstTouch.phase == TouchPhase.Began)
{
if(firstTouch.position.x > screenCenterX)
{
// if the touch position is to the right of center
// move right
}
else if(firstTouch.position.x < screenCenterX)
{
// if the touch position is to the left of center
// move left
}
}
}
}
}`
May be helpfull
create script. for example player.cs
public class PlayerController : MonoBehaviour
{
bool swipeRight = false;
bool swipeLeft = false;
bool touchBlock = true;
bool canTouchRight = true;
bool canTouchLeft = true;
void Update()
{
swipeLeft = Input.GetKeyDown("a") || Input.GetKeyDown(KeyCode.LeftArrow);
swipeRight = Input.GetKeyDown("d") || Input.GetKeyDown(KeyCode.RightArrow);
TouchControl();
if(swipeRight) //rightMove logic
else if(swipeLeft) //leftMove logic
}
void TouchControl()
{
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved && touchBlock == true)
{
touchBlock = false;
// Get movement of the finger since last frame
var touchDeltaPosition = Input.GetTouch(0).deltaPosition;
Debug.Log("touchDeltaPosition "+touchDeltaPosition);
if(touchDeltaPosition.x > 0 && canTouchRight == true)
{
//rightMove
swipeRight = true; canTouchRight = false;
Invoke("DisableSwipeRight",0.2f);
}
else if(touchDeltaPosition.x < 0 && canTouchLeft == true)
{
//leftMove
swipeLeft = true; canTouchLeft = false;
Invoke("DisableSwipeLeft",0.2f);
}
}
}
void DisableSwipeLeft()
{
swipeLeft = false;
touchBlock = true;
canTouchLeft = true;
}
void DisableSwipeRight()
{
swipeRight = false;
touchBlock = true;
canTouchRight = true;
}
}
I have a VR Scene with a C# Script on the camera that allows the user to Click Once to move and again to stop.
public float speed = 1.0f;
public bool startedMoving = true;
public GameObject myCam;
// Update is called once per frame
void Update () {
if (startedMoving) {
transform.position += myCam.transform.forward * speed * Time.deltaTime;
}
// if(Input.GetButtonDown("Fire1")){
if (Input.GetMouseButton(0)){
startedMoving = !startedMoving;
}
}
What I want to know is how I can CLICK & HOLD to move Backwards..?
Thank you!
~ b
Use enum the represent the status of the mouse instead of startedMoving or multiple booleans that will make everything easier to implement. The comment in the code describes how it works.
using UnityEngine;
using System.Collections;
public class ClickAndHeld : MonoBehaviour
{
public GameObject myCam;
CLICK_MODE clickMode = CLICK_MODE.NO_CLICK;
MOVE_DIRECTION moveDir = MOVE_DIRECTION.IDLE;
public float speed = 1.0f;
//If down for 0.5 secods the it is considered Click and Hold instead of Click
float clickHoldDetectTime = 0.5f;
float clickCounter = 0; //Dont not change
void Start()
{
StartCoroutine(mover());
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButton(0))
{
//If in NO_CLICK, set to CLICKED
if (clickMode == CLICK_MODE.NO_CLICK)
{
//Change the mode to CLICKED
clickMode = CLICK_MODE.CLICKED;
}
//If in CLICKED mode, start counting to clickHoldDetectTime
if (clickMode == CLICK_MODE.CLICKED)
{
clickCounter += Time.deltaTime; //Increment counter
//Check if we have reached the clickHoldDetectTime time
if (clickCounter > clickHoldDetectTime)
{
//Reset Counter
clickCounter = 0;
//Change the mode to CLICK_AND_HELD
clickMode = CLICK_MODE.CLICK_AND_HELD;
}
}
//If STILL down and the the current mode is CLICK_AND_HELD then do clickedAndHeldDown stuff
if (clickMode == CLICK_MODE.CLICK_AND_HELD)
{
clickedAndHeldDown();
}
}
else
{
//If released and the current mode is CLICKED then do clicked stuff
if (clickMode == CLICK_MODE.CLICKED)
{
clicked();
}
//If released and the current mode is CLICK_AND_HELD, change to RELEASED then do relased stuff
if (clickMode == CLICK_MODE.CLICK_AND_HELD)
{
//Change the mode to RELEASED
clickMode = CLICK_MODE.RELEASED;
mouseReleasedAfterBeingHeld();
}
//Reset each time mouse button is released
reset();
}
}
IEnumerator mover()
{
while (true)
{
if (moveDir == MOVE_DIRECTION.IDLE)
{
}
//Move Forward
if (moveDir == MOVE_DIRECTION.FORWARD)
{
transform.position += myCam.transform.forward * speed * Time.deltaTime;
}
//Move Backward
if (moveDir == MOVE_DIRECTION.BACKWARD)
{
transform.position -= myCam.transform.forward * speed * Time.deltaTime;
}
yield return null;
}
}
private void clicked()
{
Debug.Log("CLICKED");
//If Idle, become Forward
if (moveDir == MOVE_DIRECTION.IDLE)
{
moveDir = MOVE_DIRECTION.FORWARD;
}
//If forward, moves become idle
else if (moveDir == MOVE_DIRECTION.FORWARD)
{
moveDir = MOVE_DIRECTION.IDLE;
}
//--------------------------------------------------
//If backward, moves become idle
else if (moveDir == MOVE_DIRECTION.BACKWARD)
{
moveDir = MOVE_DIRECTION.IDLE;
}
}
private void clickedAndHeldDown()
{
Debug.Log("CLICKED AND HELD");
//If Idle, becomes backward
if (moveDir == MOVE_DIRECTION.IDLE)
{
moveDir = MOVE_DIRECTION.BACKWARD;
}
}
//Called when released after being RELEASED from CLICKED_HELD
private void mouseReleasedAfterBeingHeld()
{
Debug.Log("RELEASED AFTER CLICKED AND HELD");
//If backward, move becomes idle
if (moveDir == MOVE_DIRECTION.BACKWARD)
{
moveDir = MOVE_DIRECTION.IDLE;
}
//--------------------------------------------------
//If forward, move becomes idle
else if (moveDir == MOVE_DIRECTION.FORWARD)
{
moveDir = MOVE_DIRECTION.IDLE;
}
}
void reset()
{
clickMode = CLICK_MODE.NO_CLICK;
clickCounter = 0;
}
}
public enum CLICK_MODE
{
NO_CLICK, CLICKED, CLICK_AND_HELD, RELEASED
}
public enum MOVE_DIRECTION
{
IDLE, FORWARD, BACKWARD
}
Because you only have one trigger action you're going to have to implement something time based for toggling forward/backwards movement. For example, if you press and release the trigger quickly then forward walk could be toggled, but if you're holding the trigger after N length of time then walk backwards.
He's a practical example to get you starting.
Pressing and releasing the trigger within 300ms will toggle forward movement
Pressing and holding the trigger for longer than 300ms will begin backwards movement, releasing the trigger then will stop backwards movement
This is a theoretical example
public float speed = 1.0f;
bool triggerPressed = false;
float triggerHeldTime = 0f;
public bool movingForwards = false;
public bool movingBackwards = false;
void Update ()
{
// increment hold time if we're still holding trigger
if (Input.GetMouseButton(0) && triggerPressed)
triggerHeldTime += Time.deltaTime;
if (Input.GetMouseButton(0) && !triggerPressed)
{
// reset everything when trigger initially pressed
movingForards = false;
movingBackwards = false;
triggerHeldTime = 0f;
triggerPressed = true;
}
else if (!Input.GetMouseButton(0) && triggerPressed)
{
// upon trigger release
triggerPressed = false;
// if we are not moving backwards, toggle forwards movement
if(!movingBackwards)
movingForwards = !movingForwards;
// always reset backwards movement when we release the trigger
movingBackwards = false;
triggerHeldTime = 0f;
}
// if the trigger has been held for 300ms then move backwards
if(triggerHeldTime > 0.3f)
{
moveForwards = false;
moveBackwards = true;
}
// actually perform the movement
if (moveForwards)
{
transform.position += myCam.transform.forward * speed * Time.deltaTime;
}
else if(moveBackwards)
{
transform.position -= myCam.transform.forward * speed * Time.deltaTime;
}
}
I have script called PlayerCharacter to control a player on the Unity 2D Platform. It's perfect, working as usual.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
[RequireComponent(typeof (Rigidbody2D))]
[RequireComponent(typeof(BoxCollider2D))]
public class PlayerCharacter : MonoBehaviour
{
public float speed = 1.0f;
public string axisName = "Horizontal";
private Animator anim;
public string jumpButton = "Fire1";
public float jumpPower = 10.0f;
public float minJumpDelay = 0.5f;
public Transform[] groundChecks;
private float jumpTime = 0.0f;
private Transform currentPlatform = null;
private Vector3 lastPlatformPosition = Vector3.zero;
private Vector3 currentPlatformDelta = Vector3.zero;
// Use this for initialization
void Start ()
{
anim = gameObject.GetComponent<Animator>();
}
// Update is called once per frame
void Update ()
{
//Left and right movement
anim.SetFloat("Speed", Mathf.Abs(Input.GetAxis(axisName)));
if(Input.GetAxis(axisName) < 0)
{
Vector3 newScale = transform.localScale;
newScale.x = -1.0f;
transform.localScale = newScale;
Debug.Log("Move to left");
}
else if(Input.GetAxis(axisName) > 0)
{
Vector3 newScale = transform.localScale;
newScale.x = 1.0f;
transform.localScale = newScale;
Debug.Log ("Move to Right");
}
transform.position += transform.right*Input.GetAxis(axisName)*speed*Time.deltaTime;
//Jump logic
bool grounded = false;
foreach(Transform groundCheck in groundChecks)
{
grounded |= Physics2D.Linecast(transform.position, groundCheck.position, 1 << LayerMask.NameToLayer("Ground"));
}
anim.SetBool("Grounded", grounded);
if(jumpTime > 0)
{
jumpTime -= Time.deltaTime;
}
if(Input.GetButton("jumpButton") && anim.GetBool("Grounded") )
{
anim.SetBool("Jump",true);
rigidbody2D.AddForce(transform.up*jumpPower);
jumpTime = minJumpDelay;
}
if(anim.GetBool("Grounded") && jumpTime <= 0)
{
anim.SetBool("Jump",false);
}
//Moving platform logic
//Check what platform we are on
List<Transform> platforms = new List<Transform>();
bool onSamePlatform = false;
foreach(Transform groundCheck in groundChecks)
{
RaycastHit2D hit = Physics2D.Linecast(transform.position, groundCheck.position, 1 << LayerMask.NameToLayer("Ground"));
if(hit.transform != null)
{
platforms.Add(hit.transform);
if(currentPlatform == hit.transform)
{
onSamePlatform = true;
}
}
}
if(!onSamePlatform)
{
foreach(Transform platform in platforms)
{
currentPlatform = platform;
lastPlatformPosition = currentPlatform.position;
}
}
}
void LateUpdate()
{
if(currentPlatform != null)
{
//Determine how far platform has moved
currentPlatformDelta = currentPlatform.position - lastPlatformPosition;
lastPlatformPosition = currentPlatform.position;
}
if(currentPlatform != null)
{
//Move with the platform
transform.position += currentPlatformDelta;
}
}
}
A problem arises when I try to modify the script with a touchable controller. I have googled many times and modified the script as I could, and still it gives me no result (btw, I'm new to Unity). Then I found a tutorial from a website about making a touch controller with a GUI Texture (TouchControls). I think that tutorial is easy to learn. Here is the script
using UnityEngine;
using System.Collections;
[RequireComponent(typeof (Rigidbody2D))]
[RequireComponent(typeof(BoxCollider2D))]
public class TouchControls : MonoBehaviour {
// GUI textures
public GUITexture guiLeft;
public GUITexture guiRight;
public GUITexture guiJump;
private Animator anim;
// Movement variables
public float moveSpeed = 5f;
public float jumpForce = 50f;
public float maxJumpVelocity = 2f;
// Movement flags
private bool moveLeft, moveRight, doJump = false;
void Start ()
{
anim = gameObject.GetComponent<Animator>();
}
// Update is called once per frame
void Update () {
// Check to see if the screen is being touched
if (Input.touchCount > 0)
{
// Get the touch info
Touch t = Input.GetTouch(0);
// Did the touch action just begin?
if (t.phase == TouchPhase.Began)
{
// Are we touching the left arrow?
if (guiLeft.HitTest(t.position, Camera.main))
{
Debug.Log("Touching Left Control");
moveLeft = true;
}
// Are we touching the right arrow?
if (guiRight.HitTest(t.position, Camera.main))
{
Debug.Log("Touching Right Control");
moveRight = true;
}
// Are we touching the jump button?
if (guiJump.HitTest(t.position, Camera.main))
{
Debug.Log("Touching Jump Control");
doJump = true;
}
}
// Did the touch end?
if (t.phase == TouchPhase.Ended)
{
// Stop all movement
doJump = moveLeft = moveRight = false;
}
}
// Is the left mouse button down?
if (Input.GetMouseButtonDown(0))
{
// Are we clicking the left arrow?
if (guiLeft.HitTest(Input.mousePosition, Camera.main))
{
Debug.Log("Touching Left Control");
moveLeft = true;
}
// Are we clicking the right arrow?
if (guiRight.HitTest(Input.mousePosition, Camera.main))
{
Debug.Log("Touching Right Control");
moveRight = true;
}
// Are we clicking the jump button?
if (guiJump.HitTest(Input.mousePosition, Camera.main))
{
Debug.Log("Touching Jump Control");
doJump = true;
}
}
if (Input.GetMouseButtonUp(0))
{
// Stop all movement on left mouse button up
doJump = moveLeft = moveRight = false;
}
}
void FixedUpdate()
{
//anim.SetFloat("Speed", Mathf.Abs);
// Set velocity based on our movement flags.
if (moveLeft)
{
rigidbody2D.velocity = -Vector2.right * moveSpeed;
}
if (moveRight)
{
rigidbody2D.velocity = Vector2.right * moveSpeed;
}
if (doJump)
{
// If we have not reached the maximum jump velocity, keep applying force.
if (rigidbody2D.velocity.y < maxJumpVelocity)
{
rigidbody2D.AddForce(Vector2.up * jumpForce);
} else {
// Otherwise stop jumping
doJump = false;
}
}
}
}
But I have no idea how to implement the script from the tutorial (TouchControls) and assign that to my player control script (PlayerCharacter). How can I combine both scripts so that a player can control it with a touchable control?
The best thing you can do is not to drag the touch controls from the touchcontrols tutorial to the playercontroller but the other way around, use the touchcontrols tutorial script as your template.
Since your playercontroller uses floats in its input such as moveleft = 50.0f; and the touchcontrols uses moveleft = true;
the scripts are very different from each other to just merge and work.
so from that in the touchcontrols leave the update function as it is,
and only update the fixedupate function with your controls logic since
the update void, is the condition controller for right, left, up & down so to speak.
and it will also handle the actual input of the touch.
the fixed update could then control some things that the playercontroller has such as
apply force when touching a tagged object or stuff like that.
and the update only does the input condition, good advice would be to wrap the update touch code in its own function so the update is not only touch but also other game logic related code.
You should search use copy the touch control script inside the player controller while changing the right parts. For example, instead of using Input.GetKeyDown you should use the Input.GetTouch but it depends on the game you are creating. You should pay attention to that code and change some parts