This question was
closed Nov 28, 2021 at 04:49 AM by
JelyMe for the following reason:
Problem is not reproducible or outdated
Raycast 3D length not infinite
I have a weird issue.
My raycast doesn't extend past some unknown distance when I haven't filled in the maxDistance, and if I do, it stops casting!
Here's my code: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class JumpTest : MonoBehaviour
{
public float timeSlowFactor;
public float ogTimeFactor;
public bool slowed;
public PlayerMovement player;
public Transform ghost;
public GameObject ghostVisual;
private Rigidbody rb;
public float ogFixedTime;
public float ogCounter;
// Start is called before the first frame update
void Start()
{
ogCounter = player.counterMovement;
rb = this.GetComponent<Rigidbody>();
ogTimeFactor = Time.timeScale;
ogFixedTime = Time.fixedDeltaTime;
}
// Update is called once per frame
void Update()
{
if (slowed)
{
player.counterMovement = 0;
if (Input.GetButtonDown("Jump"))
{
ghostVisual.active = false;
player.canCrouch = true;
slowed = false;
player.counterMovement = ogCounter;
Time.timeScale = ogTimeFactor;
Time.fixedDeltaTime = ogFixedTime;
if (Input.GetKey(KeyCode.LeftShift) && !player.isCrouched) { player.StartCrouch(); }
if (!Input.GetKey(KeyCode.LeftShift) && player.isCrouched) { player.StopCrouch(); }
return;
}
if (Input.GetButtonDown("Fire1"))
{
EndJump();
return;
}
RaycastHit hit;
if (Physics.Raycast(Camera.main.transform.position, Camera.main.transform.forward, out hit, LayerMask.NameToLayer("Ground")))
{
ghost.position = hit.point;
ghost.eulerAngles = hit.normal * Mathf.Rad2Deg;
}
}
if (Input.GetButtonDown("Jump") && !slowed && player.grounded)
{
BeginJump();
}
}
void BeginJump()
{
player.canCrouch = false;
Time.timeScale = timeSlowFactor;
Time.fixedDeltaTime = ogFixedTime * Time.timeScale;
ghostVisual.active = true;
slowed = true;
}
void EndJump()
{
Time.timeScale = ogTimeFactor;
Time.fixedDeltaTime = ogFixedTime;
player.canCrouch = true;
slowed = false;
ghostVisual.active = false;
JumpToPoint();
StartCoroutine("EndSoon");
}
void JumpToPoint()
{
float gravity = Physics.gravity.y;
float h = ghostVisual.transform.position.y + 3;
float displacementY = ghostVisual.transform.position.y - transform.position.y;
Vector3 displacementXZ = new Vector3(ghostVisual.transform.position.x - transform.position.x, 0, ghostVisual.transform.position.z - transform.position.z);
float time = Mathf.Sqrt(-2 * h / gravity) + Mathf.Sqrt(2 * (displacementY - h) / gravity);
Vector3 velocityY = Vector3.up * Mathf.Sqrt(-2 * gravity * h);
Vector3 velocityXZ = displacementXZ / time;
rb.velocity = velocityXZ + velocityY;
}
IEnumerator EndSoon()
{
for (int i = 0; i < 5; i++)
{
yield return null;
}
player.counterMovement = ogCounter;
}
}
Don't worry about the other code, that shouldn't cause this.
Comment