- Home /
Force Teleport
Hey Everyone here is a script im working on to add a force to a teleport where you can control how far you go the longer you hold down the right arrow key. I cant seem to find the problem, any advice or help to point outs whats wrong would be greatly appreciated. :)
var character : GameObject;
var target : GameObject; // Target to shoot away.
var timer : float = 1; // 10 means it takes 10 seconds for full force.
var minForce : float = 10; // Minimum force applied on shot.
var maxForce : float = 50; // Maximum force applied on shot.
private var power : float = 0; // Power charge.
function Play() {
power = 0;
while (!Input.GetKeyDown("right")) { yield; } // Wait until press.
while ( Input.GetKeyDown("right")) { Charge(); yield; } // Charge until release.
Shoot();
}
function Charge() {
power = Mathf.Clamp01(power + (1 / timer) * Time.deltaTime);
}
function Shoot() {
var impulse = Mathf.Lerp(minForce, maxForce, power);
target.AddForce(transform.forward * impulse, ForceMode.Impulse);
}
function Update () {
if (Input.GetKeyDown ("right"))
{
transform.Translate(character.transform.right.normalized *-5.0f);
}
if (Input.GetKeyDown ("left"))
{
transform.Translate(character.transform.right.normalized *5.0f);
}
if (Input.GetKeyDown ("up"))
{
transform.Translate(character.transform.up.normalized *5.0f);
}
if (Input.GetKeyDown ("down"))
{
transform.Translate(character.transform.up.normalized *-5.0f);
}
}
Answer by aldonaletto · Oct 09, 2012 at 12:15 PM
There are some weird/wrong things in your script. First: you should use Input.GetKey instead of Input.GetKeyDown in the Play coroutine - GetKeyDown returns true only during the frame the key was pressed. Second: Play isn't being called anywhere in the script you've posted. Third: target is a GameObject, but you can only AddForce to a Rigidbody. If you're assigning to target in the Inspector, just declare it as a Rigidbody and keep your original Shoot code. If target may be assigned at runtime, on the other hand, check whether the target object has a rigidbody, and if so, apply the force to it - like this:
function Shoot() {
if (target.rigidbody){
var impulse = Mathf.Lerp(minForce, maxForce, power);
target.rigidbody.AddForce(transform.forward * impulse, ForceMode.Impulse);
}
}
NOTE: Make sure that Play isn't called again while it's executing! You can use a boolean flag: set it to true when Play starts and to false at the end, and abort Play calls when the flag is true:
var shooting = false; // boolean flag
function Play(){
shooting = true;
...
Shoot();
shooting = false;
}
// in the caller code:
...
if (Input.GetGeyDown(...) && !shooting) Play();
...
Answer by plof27 · Jun 12, 2018 at 09:51 PM
I know I'm 6 years late on this, but I came across this post because I ran into the same problem. There was a thread here which actually had a functional solution. You need to uncheck Apply Root Motion
on the object's Animator
component. For some reason, this causes weird locking on some, but not all, axes even if there is no parent object in the hierarchy. Hopefully this helps any other lost souls who find themselves here years in the future.
Your answer
Follow this Question
Related Questions
Spawning System 1 Answer
Touch teleport script isnt working 2 Answers
Mirror a Vector3 1 Answer
Velocity not resetting to zero? 1 Answer