- Home /
Blink Ability Issues with Angles and Certain Objects
Hello everyone, I'm relatively new to using Unity and have been attempting to program a blink ability for a character. I've been trying to get it to work for 2-3 days now, but I'm pretty much stumped on what I should be doing.
Explanation: My player model is using the Character Controller system rather than Rigidbody as I first attempted to use the other system, but couldn't program basic movements so instead I switch to the Character Controller. Currently I'm using Raycasts to detect collisions to let my player know if their teleport path is obstructed. However, I need the ability to trigger as well even if there is no hit, and it must be capable of both vertical and horizontal teleportation. This works fairly well, but there are two issues. The first is that at certain angles my player falls through the ground when teleporting on no Raycast hit. I'm guessing this is due to them teleporting, but clipping into the ground causing them to fall through. The second is sometimes the teleport location isn't aligned with the forward direction as it should be. Instead they would be teleported sideways or not move at all. This only seems to occur when attempting to teleport towards a wall or slope.
I'm honestly not sure what to do, should I just try to switch back to Rigidbody? Any help would be appreciated. Also I only started out coding about three days ago so if there are significant faults in my work here please let me know.
Code:
public class WarperAbilities : MonoBehaviour
{
public int uses = 5;
public int maxUses = 5;
public float cooldown;
public float distance = 5f;
public Transform cameraPlayer;
public LayerMask layerMask;
public CharacterController controller;
public int blinkDelay = 5;
protected float blinkCooldown;
RaycastHit hit;
// Start is called before the first frame update
void Start()
{
}
void Blink()
{
Vector3 origin = transform.position;
Vector3 direction = cameraPlayer.transform.forward;
if (Physics.Raycast(origin, direction, out hit, distance, layerMask))
{
Vector3 endPoint = hit.point + hit.normal * 1.5f;
GetComponent<CharacterController>().enabled = false;
transform.position = endPoint;
GetComponent<CharacterController>().enabled = true;
print(hit.transform.name);
}
else
{
GetComponent<CharacterController>().enabled = false;
transform.position = cameraPlayer.transform.forward * distance + new Vector3(0f, 1f, 0f);
GetComponent<CharacterController>().enabled = true;
}
}
void Update()
{
blinkCooldown += Time.deltaTime;
if (Input.GetKeyDown(KeyCode.Q) && uses >= 1)
{
uses -= 1;
Blink();
}
else if (uses < maxUses && blinkCooldown >= blinkDelay)
{
blinkCooldown = 0;
uses++;
}
}
}
Your answer
Follow this Question
Related Questions
Camera collision detection. 0 Answers
Enemy not jumping using Raycast 1 Answer
Dashing through enemies, but not walls. 1 Answer