- Home /
Question by
popugames · Dec 25, 2017 at 12:09 AM ·
lookatlookrotation
LookAt move direction (player direction is relative at camera)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;
public class Contoller : MonoBehaviour {
public float speed = 2.0f;
private Transform m_Cam; // A reference to the main camera in the scenes transform
private Vector3 m_CamForward; // The current forward direction of the camera
private Vector3 m_Move;
// Use this for initialization
void Start () {
m_Cam = Camera.main.transform;
}
void FixedUpdate()
{
//reading the input:
float h = CrossPlatformInputManager.GetAxis("Horizontal");
float v = CrossPlatformInputManager.GetAxis("Vertical");
// calculate camera relative direction to move:
m_CamForward = Vector3.Scale(m_Cam.forward, new Vector3(1, 0, 1)).normalized;
m_Move = v * m_CamForward + h * m_Cam.right;
transform.Translate(m_Move * speed);
//calculate rotation of the player
// ??????????????????????? THIS DON'T WORK ?????????????????
transform.rotation = Quaternion.LookRotation(m_Move.normalized);
}
Comment
Relative direction work fine. Rotation player is problem.
What exactly happens when that value is put in? Does it not rotate at all, or just in the wrong direction?