Having issues with teleporting forward
I have a script that can teleport my character forward, it seems to work on a game object that has no other properties however when I try to implement it with my character that can walk and run with animations it seems not to work.
using System.Collections; using System.Collections.Generic; using UnityEditor; using UnityEngine;
public class Guard_mvmt : MonoBehaviour { float speed = 4; float sprintSpeed = 25; float rotSpeed = 80; float rot = 0f; float gravity = 8; public float distance = 5.0f;
Vector3 movDir = Vector3.zero;
CharacterController controller;
Animator anim;
void Start()
{
controller = GetComponent<CharacterController>();
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(1))
{
BlinkForward();
}
Movement();
GetInput();
}
// This is the telportation part, it's weird cause the I get a print to the console if I do it in front of an object that I shouldn't be able to transport through but the teleport itself won't happen
public void BlinkForward()
{
RaycastHit hit;
Vector3 destination = transform.position + transform.forward * distance;
//if running into obstacle
if (Physics.Linecast(transform.position, destination, out hit))
{
destination = transform.position + transform.forward * (hit.distance - 1f);
print(hit.transform.name);
}
// if path is clear
if (Physics.Raycast(destination, -Vector3.up, out hit))
{
destination = hit.point;
destination.y = 0.1f;
transform.position = destination;
}
}
void Movement()
{
if (controller.isGrounded)
{
if (Input.GetKey(KeyCode.W))
{
if (anim.GetBool("attacking") == true)
{
return;
}
else if (anim.GetBool("attacking") == false)
{
anim.SetBool("running", true);
anim.SetInteger("condition", 1);
movDir = new Vector3(0, 0, 1);
movDir *= speed;
movDir = transform.TransformDirection(movDir);
}
}
if (Input.GetKey(KeyCode.W) && Input.GetKey(KeyCode.LeftShift))
{
anim.SetBool("running", true);
anim.SetInteger("condition", 3);
movDir = new Vector3(0, 0, 1);
movDir *= sprintSpeed;
movDir = transform.TransformDirection(movDir);
}
if (Input.GetKeyUp(KeyCode.W))
{
anim.SetBool("running", false);
anim.SetInteger("condition", 0);
movDir = new Vector3(0, 0, 0);
}
}
rot += Input.GetAxis("Horizontal") * rotSpeed * Time.deltaTime;
transform.eulerAngles = new Vector3(0, rot, 0);
if (Input.GetKeyUp(KeyCode.W))
{
anim.SetInteger("condition", 0);
movDir = new Vector3(0, 0, 0);
}
movDir.y -= gravity * Time.deltaTime;
controller.Move(movDir * Time.deltaTime);
}
void GetInput()
{
if (controller.isGrounded)
{
if (Input.GetMouseButtonDown(0))
{
if (anim.GetBool("running") == true)
{
anim.SetBool("running", false);
anim.SetInteger("condtion", 0);
}
if (anim.GetBool("running") == false)
{
Attacking();
}
}
}
}
void Attacking()
{
StartCoroutine(AttackRoutine());
}
IEnumerator AttackRoutine()
{
anim.SetBool("condition", true);
anim.SetInteger("condition", 2);
yield return new WaitForSeconds(1);
anim.SetInteger("condition", 0);
anim.SetBool("condition", false);
}
}
Comment