- Home /
Rotate Character towards direction in Local Space
I am making a game on a 'Grid system.' Each time the player hits WASD, I keep track of the direction as either a Vector forward, back, left or right.
From this point, I wish to cast a ray towards the position of the grid block, rotate the character in that direction, and then walk forward.
The code I am using right now is:
public void Move(Vector3 direction, bool faceDirection, bool canFallOffEdge, bool force) { c.t.localEulerAngles = new Vector3(); c.t.eulerAngles = new Vector3(); Vector3 destination = c.t.position; if (!c.state.canAct && !force) return;
if (direction.x < 0)
destination -= new Vector3(1, 0, 0);
if (direction.x > 0)
destination += new Vector3(1, 0, 0);
if (direction.z < 0)
destination -= new Vector3(0, 0, 1);
if (direction.z > 0)
destination += new Vector3(0, 0, 1);
destination = c.t.TransformDirection(destination);
Debug.LogWarning(direction.ToString() + " / char pos: " + c.t.position);
Ray ray = new Ray(destination + new Vector3(0, Utility.raycastHeightOffset, 0), Vector3.down);
RaycastHit hit;
Physics.Raycast(ray, out hit, float.MaxValue);
if (hit.collider == null)
return;
Ground g = hit.collider.GetComponent<Ground>();
if (g == null)
return;
if (!g.IsEmpty())
return;
c.t.LookAt(g.transform);
// Move to the point
//isMoving = true;
//c.StartCoroutine(Move_Routine(hit.point));
}
While rotated towards 0,0,0, this works, however it does not respond to directionals.
Your answer
Follow this Question
Related Questions
Making a bubble level (not a game but work tool) 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How do I create a scrolling sky? 4 Answers
C# Movement Script 3 Answers