- Home /
Trouble having character face direction moving
I've seen a few posts about this but I'm new to scripting and having trouble figuring out how to implement some of the different solutions. I have a basic tap-to-touch function that I grabbed from here: http://www.thegamecontriver.com/2014/10/move-to-mouse-click-touch-position-unity.html
But when the character moves, it doesn't change direction or orientation, regardless of where I tap. I'm trying to figure out if I need transform.LookAt or Quaternion.LookRotation, and how I would even implement it - or maybe it's neither but I can't figure it out regardless.
**I should clarify - it's a 3d game with an isometric-style follow camera.
Here's my current code - again, it's just straight from the above link, and works great outside of the rotation issue. Thanks in advance.
public class TouchMovement : MonoBehaviour {
private bool flag = false;
private Vector3 endPoint;
public float duration = 50.0f;
private float yAxis;
void Start(){
yAxis = gameObject.transform.position.y;
}
void Update () {
if((Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began) || (Input.GetMouseButtonDown(0)))
{
RaycastHit hit;
Ray ray;
#if UNITY_EDITOR
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
#elif (UNITY_ANDROID || UNITY_IPHONE || UNITY_WP8)
#endif
if(Physics.Raycast(ray,out hit))
{
flag = true;
endPoint = hit.point;
endPoint.y = yAxis;
Debug.Log(endPoint);
}
}
if(flag && !Mathf.Approximately(gameObject.transform.position.magnitude, endPoint.magnitude)){ //&& !(V3Equal(transform.position, endPoint))){
gameObject.transform.position = Vector3.Lerp(gameObject.transform.position, endPoint, 1/(duration*(Vector3.Distance(gameObject.transform.position, endPoint))));
}
else if(flag && Mathf.Approximately(gameObject.transform.position.magnitude, endPoint.magnitude)) {
flag = false;
}
}
}
Answer by b1gry4n · Sep 03, 2016 at 03:50 AM
If you are tapping to move the character, that means you have a Vector3 as the "destination" or in your case "endpoint". This will be the critical part of making the object turn towards that point.
If you are using lookat, the object you are applying lookat to will have its Z axis face the point youve plugged in. that means any rotation value as it will look directly at it. If you only want its Y axis to rotate do something like this:
player.transform.LookAt(endpoint);
player.transform.localEulerAngles = new Vector3(0,player.transform.localEulerAngles.y,0);
For Quaternion.LookRotation youll want to plug in the direction to the destination. You get that by doing something like this.
Vector3 dir = (endpoint- player.transform.position).normalized;
player.transform.rotation = Quaternion.LookRotation(dir);
//Again you will have to zero out any rotation that you do not want to happen
player.transform.localEulerAngles = new Vector3(0,player.transform.localEulerAngles.y,0);
Thanks! Both options rotated accurately. It's super choppy a and no longer translates all the way to touched position, but hey, one problem at a time.
Thanks again!
Youll want to lerp the rotation using either Vector3.Lerp or Quaternion.Lerp
Answer by SoraMahiro · Sep 03, 2016 at 05:22 AM
I don't know much about android development, but I would try to set Transform.LookAt to equal the position in which you touch. If that doesn't work, you could always try setting up the transform.rotation to equal a particular Quaternion.Slerp when you touch.