// actual movement will go on here, since we're using velocity changes with the movement // and velocity is part of the physics engine, which updates in FixedUpate. void FixedUpdate() { if (inputVector.z != 0f) { rBody.velocity = transform.forward * movementSpeed + Physics.gravity * gravityScale; } RayThing fwdCast = CastRay(transform.position, transform.position, 3f, true); }
// function for casting rays, which we'll be doing a lot of in this script RayThing CastRay(Vector3 origin, Vector3 direction, float distance, bool visualize) { // initialize ray Ray myRay = new Ray(origin, direction); // visualize the ray if desired if (visualize) { Debug.DrawRay(origin, direction, Color.yellow, distance); } RayThing output = new RayThing(); output.didHit = Physics.Raycast(myRay, out output.rayHit, distance); // fill the output class return(output); // return the raything output }
void LateUpdate() { RayThing backRayThing = new RayThing(); backRayThing = CastRay(transform.position, new Vector3(-transform.forward.x, 0f, -transform.forward.z), 5f, true); if (backRayThing.didHit) { Vector3 hitLocation = backRayThing.rayHit.point; float dist; if ((dist = Vector3.Distance(transform.position, hitLocation)) < 1f) { t = 0f; transform.position += transform.forward * (1f - dist); } else if (Vector3.Distance(transform.localPosition, normalRelativePos) > 0.01) { t += Time.fixedDeltaTime; transform.localPosition = Vector3.Lerp(transform.localPosition, normalRelativePos, t); } } }