Hi all I found this script on line and I was wondering how can I increse the raycast. any help would be appreciated! thank you very much! Code (CSharp): using UnityEngine; using System.Collections; public class Player : MonoBehaviour { public bool drawDebugGizmos = false; public float timeToMove = 1f; Vector3 invalidPosition = new Vector3(9999, 9999, 9999); Vector3 positionToMoveTo; Vector3 currentMove; Vector3 checkDirection; bool checkedDiagonal = false; bool isMoving = false; Vector3 transformUp; Vector3 currentMoveCenter; Ray currentMoveRay; Ray wallCheckRay; void Start() { positionToMoveTo = invalidPosition; } void Update () { if (isMoving) { return; } if (Input.GetKeyDown(KeyCode.W)) { ResetChecks(); currentMove = transform.forward; Move(currentMove); if (positionToMoveTo != invalidPosition) { StartCoroutine("PerformMove"); } } else if (Input.GetKeyDown(KeyCode.A)) { ResetChecks(); currentMove = transform.right * -1; Move(currentMove); if (positionToMoveTo != invalidPosition) { StartCoroutine("PerformMove"); } } else if (Input.GetKeyDown(KeyCode.S)) { ResetChecks(); currentMove = transform.forward * -1; Move(currentMove); if (positionToMoveTo != invalidPosition) { StartCoroutine("PerformMove"); } } else if (Input.GetKeyDown(KeyCode.D)) { ResetChecks(); currentMove = transform.right; Move(currentMove); if (positionToMoveTo != invalidPosition) { StartCoroutine("PerformMove"); } } } IEnumerator PerformMove() { isMoving = true; float elapsedTime = 0f; Vector3 initialPosition = transform.position; Vector3 inititalUp = transform.up; while (elapsedTime < timeToMove) { transform.position = Vector3.Lerp(initialPosition, positionToMoveTo, (elapsedTime / timeToMove)); transform.up = Vector3.Lerp(inititalUp, transformUp, (elapsedTime / timeToMove)); elapsedTime += Time.deltaTime; yield return null; } transform.position = positionToMoveTo; transform.up = transformUp; positionToMoveTo = invalidPosition; isMoving = false; } void Move(Vector3 offset) { Vector3 pointToCheck = transform.position + offset; currentMoveCenter = pointToCheck; currentMoveRay = new Ray(currentMoveCenter, checkDirection); RaycastHit hit; if (Physics.Raycast(currentMoveRay, out hit, 1f)) { if (hit.collider.tag == "Walkable") { transformUp = hit.normal; positionToMoveTo = pointToCheck; } } if (positionToMoveTo != invalidPosition) { return; } if (WallCheck()) { StartCoroutine("ChangeOrientation"); return; } else if (!checkedDiagonal) { checkedDiagonal = true; checkDirection = currentMove * -1; Move(currentMove + transform.up * -1); } } IEnumerator ChangeOrientation() { isMoving = true; float elapsedTime = 0f; Vector3 inititalUp = transform.up; while (elapsedTime < timeToMove) { transform.up = Vector3.Lerp(inititalUp, transformUp, (elapsedTime / timeToMove)); elapsedTime += Time.deltaTime; yield return null; } transform.up = transformUp; isMoving = false; } void ResetChecks() { checkDirection = -1 * transform.up; checkedDiagonal = false; } bool WallCheck() { wallCheckRay = new Ray(transform.position, currentMove); RaycastHit hit; if (Physics.Raycast(wallCheckRay, out hit, 1f)) { if (hit.collider.tag == "Walkable") { transformUp = hit.normal; return true; } } return false; } void OnDrawGizmos() { if (!drawDebugGizmos) { return; } // Draw a gizmo at the center point for our current move. This is where // we raycast from etc. Gizmos.color = Color.yellow; Gizmos.DrawWireSphere(currentMoveCenter, 0.1f); // Draw the ray we use to check if there's a walkable node beneath our feet. Gizmos.color = Color.red; Gizmos.DrawRay(currentMoveRay); // Draw the ray we use to check if there is a wall in the direction we're // trying to move. Gizmos.color = Color.blue; Gizmos.DrawRay(wallCheckRay); } }
if(Physics.Raycast(currentMoveRay, outhit, 1f)) Just increase the 1f Check the scripting documentation on raycast, everything you need to know is in there. If you want to see the ray, use Debug.DrawRay(currentMoveRay.origin, currentMoveRay.direction * 10f) or however long you want it to be.
Ah hehe didn't scroll all the way down. Make some rayLength variables for all your rays so you can test it while running h the code. And just check the docs then see all possible manners for a ray and at which place length is set
Check the docs if you are refering to Gizmos.DrawRay https://docs.unity3d.com/ScriptReference/Gizmos.DrawRay.html
Omg I'm reading everything about it but still can't find an answer...seems like a problem so simple to solve -,- plz I'm going mad
of course is not working...why should I continue posting if it was working? If I change to zero i don't have raycasts anymore, but if I set it to 10 for example I have the same result of setting it to 1
The third parameter of a raycast (given that shape, lots of shapes for the raycast function...) is the distance the raycast is to check. This is very much a "Q: whats 1+1" "A: 2" thing... If it's "not working" you're either asking the wrong question (bmb and jister hedged a little on this by mentioning the changes to Debug.DrawRay and Gizmo.DrawRay in case it's "how can I show the ray as being longer", displaying a ray and raycasting work differently and are totally separate etc.), or something is not setup correctly elsewhere (missing collider so a raycast of any length is never going to hit whatever it is etc.). Given that this is code you've found online and you've not really explained what it is you are trying to do, it might not even be code that is applicable to your purposes Show us your altered code (maybe you altered it incorrectly, we can't know that unless you show it), explain more specifically how it's not working and what you want it to do etc.
You can find here the project. two scripts are missings from the levelcontroller object but they don't affects the raycast problem...all is working fine in the project but if you want to increase the space between the green cubes ( and this means you have to increase the raycast) you'll have difficulties...let me know plz! and thx for the interest!