Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question 3D Isometric with mouse aiming + wasd?

Discussion in 'Input System' started by daltonwhurd, Jun 18, 2020.

  1. daltonwhurd

    daltonwhurd

    Joined:
    Jun 18, 2020
    Posts:
    2
    So I just started out and I've got a simple 3D project where I've setup an isometric camera and transformed wasd movements to go along with the perspective. However the Studica tutorial I used includes player model rotation directly into the movement code.

    Is there an easy way to separate the character model rotation into Mouse X/Y position with the addition of changing the perspective to compensate for the isometric angle of the project?


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CharController : MonoBehaviour {
    6.  
    7.     [SerializeField]
    8.     float moveSpeed = 4f;
    9.  
    10.     Vector3 forward, right;
    11.  
    12.     bool jump = false;
    13.     float jumpHeight = 8f, jumpSpeed = 10f;
    14.     Rigidbody rb;
    15.  
    16.     // Start is called before the first frame update
    17.     void Start ()
    18.     {
    19.         forward = Camera.main.transform.forward;
    20.         forward.y = 0;
    21.         forward = Vector3.Normalize(forward);
    22.         right = Quaternion.Euler(new Vector3(0, 90, 0)) * forward;
    23.         rb = GetComponent<Rigidbody>();
    24.  
    25.     }
    26.  
    27.     void Update ()
    28.     {
    29.         if (Input.GetButtonDown("Jump") && !jump)
    30.             StartCoroutine(Jump());
    31.         else
    32.             Move();
    33.     }
    34.  
    35.     void Move()
    36.     {
    37.         //Vector3 direction = new Vector3(Input.GetAxis("HorizontalKey"), 0, Input.GetAxis("VerticalKey"));
    38.         Vector3 rightMovement = right * moveSpeed * Time.deltaTime * Input.GetAxis("HorizontalKey");
    39.         Vector3 upMovement = forward * moveSpeed * Time.deltaTime * Input.GetAxis("VerticalKey");
    40.  
    41.         Vector3 heading = Vector3.Normalize(rightMovement + upMovement);
    42.  
    43.         transform.forward = heading;
    44.         transform.position += rightMovement;
    45.         transform.position += upMovement;
    46.     }
    47.  
    48.     IEnumerator Jump()
    49.     {
    50.         float originalHeight = transform.position.y;
    51.  
    52.         jump = true;
    53.         yield return null;
    54.  
    55.         //rb.useGravity = false; // disable gravity so we can move our transform without resistance
    56.         rb.AddForce(Vector3.up * jumpHeight, ForceMode.Impulse);
    57.  
    58.         //float maxHeight = originalHeight + jumpHeight;
    59.  
    60.         /*while(transform.position.y < maxHeight)
    61.         {
    62.             transform.position += transform.up * Time.deltaTime * jumpSpeed;
    63.             yield return null;
    64.         }*/
    65.         //rb.useGravity = true;
    66.         while (transform.position.y > originalHeight)
    67.         {
    68.             //transform.position -= transform.up * Time.deltaTime * jumpSpeed;
    69.             yield return null;
    70.         }
    71.     }
    72. }