- Home /
How to Follow By Getting KeyDown?
Hello Unity3D.I have a question about how to following?How i can make it that whenever i Get a key down i can follow a object and after a certain amount of time the player stops following that object?For example.When i GetKeyDown"b" my character follows this object.Then,when it has been atleast 5 seconds or over 5 seconds the character stops following the object.Then when i GetKeyDown"b" again it follows the object from the new position that it was stopped at before.If anyone knows how i can do this.Can you please tell me how?
P.S Heres a script if anyone still doesn't understand
var target : Transform;
var moveSpeed = 20;
var rotationSpeed = 0;
var myTransform : Transform;
function Start() {
target = GameObject.FindWithTag("Dummy").transform;
}
function LateUpdate () {
if(Input.GetKeyDown("1"))
myTransform = transform;
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
}
The sequence of events is wrong here, LateUpdate happens after Update, hence you want to register the $$anonymous$$eyPress in Update, Count to 5 using Couroutines (during these 5 secs, the LateUpdate's code does its thing) and then comes out.
Pseudo Code:
stopFollowing = false;
Update{
Register $$anonymous$$eyDown
Start couroutine, yield at 5 secs
stopFollowing = true
}
LateUpdate {
if(!stopFollowing) { myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime); myTransform.position += myTransform.forward moveSpeed Time.deltaTime;
}
Couroutine {
yield (5); }
}
Ins$$anonymous$$d of coroutines you could also just set a timestamp as you press a key. if (Time.time < timestamp + followTime) $$anonymous$$ove();
Im Sorry...But do you mean this?Im trying to figure out what you mean by timestamp
var target : Transform;
var moveSpeed = 20;
var rotationSpeed = 0;
var myTransform : Transform;
var timeBetweenShots = 10.0;
private var nextFire : float =0.0;
private var timestamp = -10.0;
private var followTime : float =0.0;
function Start() {
target = GameObject.FindWithTag("Dummy").transform;
}
function Update () {
if (Input.Get$$anonymous$$eyDown("1")&& Time.time < timestamp + followTime) {
if (Time.time > timestamp + timeBetweenShots ) {
Debug.Log("I'm firing now");
timestamp = Time.time;
}
}
}
function LateUpdate () {
myTransform = transform;
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
}`
Your answer
Follow this Question
Related Questions
Camera rotation around player while following. 6 Answers
Player Going through colliders 1 Answer
Enemy following Player in range 2 Answers
Which is better for player movement: physics or translation 1 Answer
Endless runner track script 0 Answers