FPS Controller
So I'm working on scripting a dashing/blinking ability for an FPS game. I want this ability to be limited to the X axis, I don't want the player to be able to dash/blink forward. I have intended for this ability to require the player to hold the left shift key and press 'A' or 'D'. I've tried a few methods as best as I could understand them like mathf.lerp, vector3.lerp, transform.position += blah * blah blah.
Eventually I reached a point where I seemed to be getting a reaction from the engine as when I tried to make my character blink/dash to the right (left shift + D) my screen flickered but my character had not moved aside ofcourse having taken one step in my attempt. I noticed the flicker seemed most prominent to the left side of my scene, which is a very basic platform with a single wall and a point light.
I tried producing the dash/blink a few more times but with my character facing the left side of my scene. I then noticed that the flicker was a duplicate of my scene appearing down the negative end of the X axis and quickly disappearing.
Not sure if this DOES mean I was getting close but I sure like to take it as a sign of such. Unfortunately I don't have a script to attach as I've completely scrapped the one I had in progress thankfully my question, atleast for now, is not so much about the exact implementation of such a mechanic but rather if trying to create this mechanic in a script separate from the unity FPS controller script could be conflicting with this? Or better yet if using the FPS character prefab at all is conflicting with the implementation of custom behaviors? Would I have to create my own 'character' with a character controller component and script?
Answer by SageGlaze · Feb 25, 2019 at 12:51 PM
Alright I seem to have figured it out and now have a working blink mechanic. I guess the issue was the fact that the FPS character in the standard assets pack has a character controller component and that was indeed conflicting with the methods I was trying to use to apply the movement to the character. I had to access the character controller component and use controller.Move
I would like to point out one thing I noticed with testing it. Every time I started the game the first time I blinked seemed to travel farther than any blinks that followed, not a huge issue, but definitely noticeable. On a good note though the player doesn't seem to be able to blink through walls! Here's the script I really hope this helps someone. Happy Coding!
public class Blink : MonoBehaviour
{
public float blinkForce = 200f;
public float blinkStoppingSpeed = 0.1f;
public float maxBlinkTime = 1f;
public bool hasBlinked;
private CharacterController controller;
private Vector3 blinkDirection;
private float currentBlinkTime;
void Start()
{
controller = GetComponent<CharacterController>();
hasBlinked = false;
}
void Update()
{
if (Input.GetKey(KeyCode.LeftControl) && Input.GetKeyDown(KeyCode.D) && hasBlinked == false)
{
hasBlinked = true;
BlinkRight();
Debug.Log("Blinked Right");
StartCoroutine(BlinkCoolDown());
}
if (Input.GetKey(KeyCode.LeftControl) && Input.GetKeyDown(KeyCode.A) && hasBlinked == false)
{
hasBlinked = true;
BlinkLeft();
Debug.Log("Blinked Left");
StartCoroutine(BlinkCoolDown());
}
if (Input.GetKey(KeyCode.LeftControl) && Input.GetKeyDown(KeyCode.W) && hasBlinked == false)
{
hasBlinked = true;
BlinkAhead();
Debug.Log("Blinked Ahead");
StartCoroutine(BlinkCoolDown());
}
}
void BlinkRight()
{
currentBlinkTime = 0.0f;
if (currentBlinkTime < maxBlinkTime)
{
blinkDirection = controller.transform.right;
currentBlinkTime += blinkStoppingSpeed;
}
else
{
blinkDirection = Vector3.zero;
}
controller.Move(blinkDirection * Time.deltaTime * blinkForce);
}
void BlinkLeft()
{
currentBlinkTime = 0.0f;
if (currentBlinkTime < maxBlinkTime)
{
blinkDirection = controller.transform.right * -1;
currentBlinkTime += blinkStoppingSpeed;
}
else
{
blinkDirection = Vector3.zero;
}
controller.Move(blinkDirection * Time.deltaTime * blinkForce);
}
void BlinkAhead()
{
currentBlinkTime = 0.0f;
if (currentBlinkTime < maxBlinkTime)
{
blinkDirection = controller.transform.forward;
currentBlinkTime += blinkStoppingSpeed;
}
else
{
blinkDirection = Vector3.zero;
}
controller.Move(blinkDirection * Time.deltaTime * blinkForce);
}
public IEnumerator BlinkCoolDown()
{
yield return new WaitForSeconds(2f);
hasBlinked = false;
}
}