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

Question EMERGENCY HELP!!!!

Discussion in 'Documentation' started by hussainshahhashmi, Jul 16, 2022.

  1. hussainshahhashmi

    hussainshahhashmi

    Joined:
    Feb 4, 2018
    Posts:
    1
    Hi Everyone! Im a beginner at Unity and C#. Ive wrote this script from a tutorial. To make a cube move in all 4 directions. if you put this script on a cube it will work. I just dont understand why :
    transform.eulerAngles + (Vector3.right * rotFactor) :
    transform.eulerAngles + (Vector3.left * rotFactor);
    isnt working? the cube moves fine but the rotation is all messed up, if you put a material on it. Vector3.up and Vector3.down are working if i replace right and left but this just doesnt work. please help

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class PlayerController : MonoBehaviour
    {
    public float inputThreshold = 0.2f;
    public float duration = 0.3f;
    public float rotFactor = 90f;

    bool isRolling = false;
    float scale;

    // Start is called before the first frame update
    void Start()
    {
    scale = transform.localScale.x / 2;
    transform.eulerAngles = Vector3.zero;
    }

    // Update is called once per frame
    void Update()
    {
    float x = Input.GetAxis("Horizontal");
    float y = Input.GetAxis("Vertical");

    if (!isRolling && ((x > inputThreshold || x < -inputThreshold) || (y > inputThreshold || y < -inputThreshold)))
    {
    isRolling = true;
    StartCoroutine(RollingCube(x, y));
    }


    }

    IEnumerator RollingCube(float x, float y)
    {
    float elapsed = 0.0f;

    Vector3 point = Vector3.zero;
    Vector3 axis = Vector3.zero;
    float angle = 0.0f;

    Vector3 direction = Vector3.zero;

    Vector3 rotation = Vector3.zero;

    if (x != 0)
    {
    axis = Vector3.forward;
    point = x > 0 ?
    transform.position + (Vector3.right * scale) :
    transform.position + (Vector3.left * scale);
    angle = x > 0 ? -90 : 90;
    direction = x > 0 ? Vector3.right : Vector3.left;
    rotation = x > 0 ?
    transform.eulerAngles + (Vector3.forward * -rotFactor) :
    transform.eulerAngles + (Vector3.back * -rotFactor);
    }
    else if (y != 0)
    {
    axis = Vector3.right;
    point = y > 0 ?
    transform.position + (Vector3.forward * scale) :
    transform.position + (Vector3.back * scale);
    angle = y > 0 ? 90 : -90;
    direction = y > 0 ? Vector3.forward : Vector3.back;
    rotation = y > 0 ?
    transform.eulerAngles + (Vector3.right * rotFactor) :
    transform.eulerAngles + (Vector3.left * rotFactor);
    }

    point += new Vector3(0, -scale, 0);



    Vector3 adjustPos = point + direction * scale - new Vector3(0f, -0.5f, 0f);
    while(elapsed < duration)
    {
    elapsed += Time.deltaTime;

    transform.RotateAround(point, axis, angle / duration * Time.deltaTime);

    yield return null;
    }

    transform.position = adjustPos;
    transform.eulerAngles = rotation;


    isRolling = false;
    }
    }