1
\$\begingroup\$

I am making an isometric game using unity 3d and 3d graphics. I have been able to figure out how to create and isometric camera that follows the player, and a basic isometric moving script for the player, and a script that rotates the player towards the mouse. Instead of moving in the same direction when moving forward, I want forward to be the direction that the player is facing when I press that key.

Here is the code for the player movement:

using UnityEngine;
using System.Collections;

public class BasicIsometricMove : MonoBehaviour
{
    void Update()
    {
        int speed = 2;
        Vector3 amount = Vector3.zero;

        if (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift))
            speed *= 2;

        if (Input.GetKey(KeyCode.W))
            amount.x += amount.z = speed;

        if (Input.GetKey(KeyCode.S))
            amount.x += amount.z = -speed;

        if (Input.GetKey(KeyCode.A))
        {
            amount.x += -speed;
            amount.z += speed;
        }

        if (Input.GetKey(KeyCode.D))
        {
            amount.x += speed;
            amount.z += -speed;
        }

        //amount *= Time.deltaTime;
        //transform.Translate(amount);
        transform.position = Vector3.Lerp(transform.position, transform.  + amount, 0.5f * Time.deltaTime);
    }
}

Here is the code for the camera:

using UnityEngine;
using System.Collections;

public class IsometricCamera : MonoBehaviour
{
    public GameObject target;
    public float size = 10;
    public float scrollSpeed = 30;

    Vector3 pos; 
    private Camera cam;

    void Start()
    {
        this.cam = (Camera)this.gameObject.GetComponent("Camera");
        this.cam.orthographic = true;
        this.cam.transform.rotation = Quaternion.Euler(30, 45, 0); 

        pos = target.transform.position;
    }

    void Update()
    {
        this.cam.orthographicSize -= Input.GetAxis("Mouse ScrollWheel") * scrollSpeed * Time.deltaTime;

        float distance = 30;

        //transform.position = target.transform.position + new Vector3(-distance, distance, -distance);

        transform.position = Vector3.Lerp(transform.position, target.transform.position + new Vector3(-distance, distance, -distance), 0.5f * Time.deltaTime);
        this.cam.transform.LookAt(target.transform);
    }

    void OnGUI()
    {
        //    GUI.Label(new Rect(10, 100, 200, 50), "" + target.transform.position.x + ", " + target.transform.position.y + ", " + target.transform.position.z);
        //    GUI.Label(new Rect(10, 130, 200, 50), "" + cam.transform.position.x + ", " + cam.transform.position.y + ", " + cam.transform.position.z);
    }
}

And here is the code for the player rotating to face the position of the mouse:

using UnityEngine;
using System.Collections;

public class LookAtMouse : MonoBehaviour {

    // Use this for initialization

    Ray cameraRay;                // The ray that is cast from the camera to the mouse position
    RaycastHit cameraRayHit;    // The object that the ray hits

    void Update () 
    {
        // Cast a ray from the camera to the mouse cursor
        cameraRay = Camera.main.ScreenPointToRay(Input.mousePosition);

        // If the ray strikes an object...
        if (Physics.Raycast(cameraRay, out cameraRayHit)) 
        {
            // ...and if that object is the ground...
            if(cameraRayHit.transform.tag=="Ground")
            {
                // ...make the cube rotate (only on the Y axis) to face the ray hit's position 
                Vector3 targetPosition = new Vector3(cameraRayHit.point.x, transform.position.y, cameraRayHit.point.z);
                transform.LookAt(targetPosition);
            }
        }
    }

}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.