Touch Controls to make an object interested in another object
OK, so I am working on a mobile game idea and yes I am pretty new to programming, but I want to learn!
Anyways I have this game where a character will follow another but I want the character to lose interest in following over time and have the player tap the character to keep it interested in following.
Maybe this is just way out of my understanding, but hey figure I would give it a shot!
So far here is my code for the character to notice another character near it and begin following it:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Follow : MonoBehaviour {
public Transform player;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update ()
{
if(Vector3.Distance(player.position, this.transform.position) < 10) // 10 is the distance before noticing.
{
Vector3 direction = player.position - this.transform.position;
direction.y = 0;
this.transform.rotation = Quaternion.Slerp(this.transform.rotation,
Quaternion.LookRotation(direction), 0.1f);
if(direction.magnitude > 5)
{
this.transform.Translate(0, 0, 0.05f);
}
}
}
}
Answer by JoeriVDE · Oct 18, 2017 at 08:34 PM
Your follow code is written well for a new programmer :)
Tested this and it worked. Also added a little cooldown feature. If anything's unclear feel free to ask! Cheers
public class Follow : MonoBehaviour {
public Transform player;
enum CharacterStates {
INTERESTED,
NOT_INTERESTED,
COOL_DOWN
}
// track the character interested state
CharacterStates _characterState = CharacterStates.NOT_INTERESTED;
// Timer for keeping track of how long the character has been interested
float _interesetedTimer;
// the time before the character loses interest
public float loseInteresetThreshold = 10f;
void Update() {
switch (_characterState) {
case CharacterStates.NOT_INTERESTED:
// If the character is not interested and the distance between it and the player is 10, it changes the state to interested
if (Vector3.Distance(transform.position, player.position) < 10f) {
_characterState = CharacterStates.INTERESTED;
}
break;
case CharacterStates.INTERESTED:
// our character is now interested and will follow the player, the 'interest' timer starts running
_interesetedTimer += Time.deltaTime; // add the time since the last frame to our timer
// check if we're over the intereseting time threshold
if (_interesetedTimer < loseInteresetThreshold) {
// If not, we update the character position and rotation
Vector3 direction = player.position - transform.position;
// only do this if there is no terrain height difference
direction.y = 0;
this.transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(direction), 0.1f);
if (direction.magnitude > 5) {
this.transform.Translate(0, 0, 0.05f);
}
} else {
// The interested time limit has been reached!
// Reset the timer
_interesetedTimer = 0;
// Go to interest cooldown mode
StartCoroutine("CoolDownInterest");
}
break;
}
}
// Cooldown Ienumator
IEnumerator CoolDownInterest() {
// Character is on interest cooldown now
_characterState = CharacterStates.COOL_DOWN;
// Wait for 5 seconds -> cooldownTime and then reset character state to 'not interested'
yield return new WaitForSeconds(5f);
_characterState = CharacterStates.NOT_INTERESTED;
}
// The method to re-awaken the character's interest i.e. when you tap it
// This used to be OnCharacterTapped
void OnMouseDown() {
// only execute if the character was interested or is on interest cooldown
if(_characterState == CharacterStates.INTERESTED || _characterState == CharacterStates.COOL_DOWN) {
//Only re-enable if the character is still within range
if (Vector3.Distance(transform.position, player.position) < 10f) {
// stop the cooldown if there is any running
StopCoroutine("CoolDownInterest");
// reset the interested timer
_interesetedTimer = 0;
_characterState = CharacterStates.INTERESTED;
}
}
}
}
This is incredible!! Holy cow thank you so much! The cooldown feature is brilliant!
The only thing I cant quite figure out is how to tap to re interest. I see the code at the bottom for it , would I just change the
OnCharacterTapped to OnNameofobjectTapped?
I'm even more new to mobile game stuff and don't fully understand the codes for tap controls.
Np, glad I could be of use :)
Replaced the CharacterTapped() by On$$anonymous$$ouseDown(). If you add a collider to your character gameObject it should execute that method when you click on it and should also work for touch input on mobile platforms if i'm not mistaken.
I made the nethod public because I assumed you already had a touch input script that registered when your pointer/finger touched the character. All you had to do then was call CharacterTapped() when it did.
Touch controls are not that easy to write, but Unity offers great tutorials and great 3rd party plugins to make that a whole lot easier for you. :) A great free touch input control plugin is TouchScript.
Good luck!
Awesome! Thanks for your help man I really appreciate it!
Your answer
Follow this Question
Related Questions
Mobile game screen orientation 0 Answers
Why does my script and collider2D automatically deactivate after reloading the scene? 0 Answers
Cannot Jump while moving, using Touch Controls for Android 1 Answer
Is it possible to access the native step-counter on mobile platforms. 2 Answers
Delayed jump on android 1 Answer