Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Isometric Character controller help!

Discussion in 'Scripting' started by ramielecho, Dec 6, 2020.

  1. ramielecho

    ramielecho

    Joined:
    Dec 6, 2020
    Posts:
    2
    here's the code I'm using it works fine except I get a message in the console saying

    Look rotation viewing vector is zero
    UnityEngine.Transform:set_forward(Vector3)
    CharController:Move() (at Assets/CharController.cs:41)
    CharController:Update() (at Assets/CharController.cs:31)

    this means my player is facing "forward" left in my current setup every update
    this is a problem because I want it to be facing forward only before a button is pressed once a button, w, a ,s ,d is pressed then it keeps the direction of the key pressed if that makes sense? looks janky as hell going back to "forward position" every update


    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 = 4f, jumpSpeed = 4f;
    14.     Rigidbody rb;
    15.     // Start is called before the first frame update
    16.     void Start()
    17.     {
    18.         forward = Camera.main.transform.forward;
    19.         forward.y = 0;
    20.         forward = Vector3.Normalize(forward);
    21.         right = Quaternion.Euler(new Vector3(0, 90, 0)) * forward;
    22.         rb = GetComponent<Rigidbody>();
    23.     }
    24.  
    25.     // Update is called once per frame
    26.     void Update()
    27.     {
    28.         if (Input.GetButtonDown("Jump") && !jump)
    29.             StartCoroutine(Jump());
    30.         else
    31.             Move();
    32.     }
    33.     void Move()
    34.     {
    35.         Vector3 direction = new Vector3(Input.GetAxis("HorizontalKey"), 0, Input.GetAxis("VerticalKey"));
    36.         Vector3 rightMovement = right * moveSpeed * Time.deltaTime * Input.GetAxis("HorizontalKey");
    37.         Vector3 upMovement = forward * moveSpeed * Time.deltaTime * Input.GetAxis("VerticalKey");
    38.  
    39.         Vector3 heading = Vector3.Normalize(rightMovement + upMovement);
    40.  
    41.         transform.forward = heading;
    42.         transform.position += heading * moveSpeed * Time.deltaTime;
    43.     }
    44.     IEnumerator Jump()
    45.     {
    46.         float originalHeight = transform.position.y;
    47.         float maxHeight = originalHeight + jumpHeight;
    48.         rb.useGravity = false;
    49.  
    50.         jump = true;
    51.         yield return null;
    52.  
    53.         //rb.AddForce(Vector3.up * jumpHeight, ForceMode.Impulse);
    54.         while(transform.position.y < maxHeight)
    55.         {
    56.             transform.position += transform.up * Time.deltaTime * jumpSpeed;
    57.             yield return null;
    58.         }
    59.  
    60.         rb.useGravity = true;
    61.  
    62.         while (transform.position.y > originalHeight)
    63.         {
    64.             yield return null;
    65.         }
    66.         rb.useGravity = true;
    67.         jump = false;
    68.         yield return null;
    69.     }
    70. }
    71.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,767
    Couple of ways to deal with it:

    - Don't set direction unless there is at least a certain amount of movement (ie., greater than 0.1 total perhaps)

    - Another way: calculate the facing angle and slowly tween towards it, which will prevent sharp snapping of direction.

    Mathf.Atan2() and Mathf.Rad2Deg is going to get you a direction from cartesian inputs (x,y)

    Mathf.DeltaAngle() and/or Mathf.MoveTowardsAngle() can help you move the facing direction smoothly.
     
  3. ramielecho

    ramielecho

    Joined:
    Dec 6, 2020
    Posts:
    2
    Yo thank you so much for your help, but I'm having trouble finding where this fits into the code