How can i make my grid controller stop after finding a "wall"
Hello, so i manage to get this script working for me, which gives the exact character movement i want, but the problem is that even after adding a box collider to my character and the same for a wall, the scrip makes the player go through it sorry if this is a silly question but im new to unity and programming. thanks. i leave the code in here.
using System.Collections;
using UnityEngine;
class tilemovment : MonoBehaviour {
private float moveSpeed = 4f;
private float gridSize = 1f;
private enum Orientation {
Horizontal,
Vertical
};
private Orientation gridOrientation = Orientation.Horizontal;
private bool allowDiagonals = true;
private bool correctDiagonalSpeed = true;
private Vector3 input;
private bool isMoving = false;
private Vector3 startPosition;
private Vector3 endPosition;
private float t;
private float factor;
public void Update() {
if (!isMoving) {
input = new Vector3 (Input.GetAxis ("Horizontal"), Input.GetAxis ("forward"));
if (!allowDiagonals) {
if (Mathf.Abs (input.x) > Mathf.Abs (input.y)) {
input.y = 0;
} else {
input.x = 0;
}
}
if (input != Vector3.zero) {
StartCoroutine (move (transform));
}
}
// if (Input.GetKey (KeyCode.W)) {
// transform.Translate (Vector3.forward);
// }
// if (Input.GetKey(KeyCode.S)){
// transform.Translate(Vector3.back);
// }
}
public IEnumerator move(Transform transform) {
isMoving = true;
startPosition = transform.position;
t = 0;
if(gridOrientation == Orientation.Horizontal) {
endPosition = new Vector3(startPosition.x + System.Math.Sign(input.x) * gridSize,
startPosition.y, startPosition.z + System.Math.Sign(input.y) * gridSize);
} else {
endPosition = new Vector3(startPosition.x + System.Math.Sign(input.x) * gridSize,
startPosition.y, startPosition.z + System.Math.Sign(input.y) * gridSize);// + System.Math.Sign(input.y) * gridSize, startPosition.z);
}
if(allowDiagonals && correctDiagonalSpeed && input.x != 0 && input.y != 0) {
factor = 0.4071f;
} else {
factor = 1f;
}
while (t < 1f) {
t += Time.deltaTime * (moveSpeed/gridSize) * factor;
transform.position = Vector3.Lerp(startPosition, endPosition, t);
yield return null;
}
isMoving = false;
yield return 0;
}
}
Answer by Vanidash-Studios · Mar 24, 2016 at 01:46 AM
Solved by myself.
I believe this is the worst answer, maybe try explaining the solution please?
Your answer
Follow this Question
Related Questions
How to make gridbased movement while also using collision? 0 Answers
Help with adding collision to GridMove script 0 Answers
[Unity 2D]Grid Based Character Movement Issues with Vector3D.movetowards 0 Answers
How do I make a Grid check for objects and make them move his possition. 1 Answer
Select a gameobject and make clones of it when clicking on scene view? 0 Answers