Problem z rotacją w powietrzy Unity C#

Programowanie w Unity.

Problem z rotacją w powietrzy Unity C#

Postprzez Jo5zek » 13 Lip 2019, 12:36

Witam!
z rotacją, gdy mój obiekt oddala się od punktu 0,0,0 po obrocie myszki o oś "y" mój obiekt rotuje mi o ten właśnie punkt... 0, 0 ,0.
Czy mógłby mi ktoś doradzić co zmienić żeby rotował wokół własnej osi?


FLYCAMERA

Kod: Zaznacz wszystko
using UnityEngine;
using System.Collections;

public class FlyCamera : MonoBehaviour
{



    public float mainSpeed = 100.0f; //regular speed
    public float shiftAdd = 250.0f; //multiplied by how long shift is held.  Basically running
    public float maxShift = 1000.0f; //Maximum speed when holdin gshift
    public float camSens = 0.25f; //How sensitive it with mouse
    private Vector3 lastMouse = new Vector3(255, 255, 255); //kind of in the middle of the screen, rather than at the top (play)
    private float totalRun = 1.0f;

    private bool isRotating = false; // Angryboy: Can be called by other things (e.g. UI) to see if camera is rotating
    private float speedMultiplier; // Angryboy: Used by Y axis to match the velocity on X/Z axis

    public float mouseSensitivity = 5.0f;        // Mouse rotation sensitivity.
    private float rotationY = 0.0f;


    void Update()
    {

        // Angryboy: Hold right-mouse button to rotate
        if (Input.GetMouseButtonDown(1))
        {
            isRotating = true;
        }
        if (Input.GetMouseButtonUp(1))
        {
            isRotating = false;
        }
        if (isRotating)
        {
            // Made by LookForward
            // Angryboy: Replaced min/max Y with numbers, not sure why we had variables in the first place
            float rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * mouseSensitivity;
            rotationY += Input.GetAxis("Mouse Y") * mouseSensitivity;
            rotationY = Mathf.Clamp(rotationY, -90, 90);
            transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0.0f);
        }

        //Keyboard commands
        float f = 0.0f;
        Vector3 p = GetBaseInput();
        if (Input.GetKey(KeyCode.LeftShift))
        {
            totalRun += Time.deltaTime;
            p = p * totalRun * shiftAdd;
            p.x = Mathf.Clamp(p.x, -maxShift, maxShift);
            p.y = Mathf.Clamp(p.y, -maxShift, maxShift);
            p.z = Mathf.Clamp(p.z, -maxShift, maxShift);
            // Angryboy: Use these to ensure that Y-plane is affected by the shift key as well
            speedMultiplier = totalRun * shiftAdd * Time.deltaTime;
            speedMultiplier = Mathf.Clamp(speedMultiplier, -maxShift, maxShift);
        }
        else
        {
            totalRun = Mathf.Clamp(totalRun * 0.5f, 1f, 1000f);
            p = p * mainSpeed;
            speedMultiplier = mainSpeed * Time.deltaTime; // Angryboy: More "correct" speed
        }

        p = p * Time.deltaTime;

        // Angryboy: Removed key-press requirement, now perma-locked to the Y plane
        Vector3 newPosition = transform.position;//If player wants to move on X and Z axis only
        transform.Translate(p);
        newPosition.x = transform.position.x;
        newPosition.z = transform.position.z;

        // Angryboy: Manipulate Y plane by using Q/E keys
        if (Input.GetKey(KeyCode.Q))
        {
            newPosition.y += -speedMultiplier;
        }
        if (Input.GetKey(KeyCode.E))
        {
            newPosition.y += speedMultiplier;
        }

        transform.position = newPosition;
    }

    // Angryboy: Can be called by other code to see if camera is rotating
    // Might be useful in UI to stop accidental clicks while turning?
    public bool amIRotating()
    {
        return isRotating;
    }

    private Vector3 GetBaseInput()
    { //returns the basic values, if it's 0 than it's not active.
        Vector3 p_Velocity = new Vector3();
        if (Input.GetKey(KeyCode.W))
        {
            p_Velocity += new Vector3(0, 0, 1);
        }
        if (Input.GetKey(KeyCode.S))
        {
            p_Velocity += new Vector3(0, 0, -1);
        }
        if (Input.GetKey(KeyCode.A))
        {
            p_Velocity += new Vector3(-1, 0, 0);
        }
        if (Input.GetKey(KeyCode.D))
        {
            p_Velocity += new Vector3(1, 0, 0);
        }
        return p_Velocity;
    }
}

SPHERELOOK


Kod: Zaznacz wszystko
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SphereLook : MonoBehaviour
{
    [SerializeField] private string mouseXInputName, mouseYInputName;
    [SerializeField] private float mouseSensitivity;

    [SerializeField] private Transform yAchse;


    private float xAxisClap;

    private void Awake()
    {
        LockCursor();
        xAxisClap = 0.0f;
    }


    private void LockCursor()
    {
        Cursor.lockState = CursorLockMode.Locked;

    }

    private void Update ()
    {
        float mouseX = Input.GetAxis(mouseXInputName) * mouseSensitivity * Time.deltaTime;
        float mouseY = Input.GetAxis(mouseYInputName) * mouseSensitivity * Time.deltaTime;

        xAxisClap += mouseY;

        if(xAxisClap > 90.0f)
        {
            xAxisClap = 90.0f;
            mouseY = 0.0f;
            ClampXAxisRotationToValue(270.0f);
        }
        else if (xAxisClap < -90.0f)
        {
            xAxisClap = -90.0f;
            mouseY = 0.0f;
            ClampXAxisRotationToValue(90.0f);
        }


        transform.Rotate(Vector3.left * mouseY);
        yAchse.Rotate(Vector3.up * mouseX);
    }

    private void ClampXAxisRotationToValue(float value)
    {
        Vector3 eulerRotation = transform.eulerAngles;
        eulerRotation.x = value;
        transform.eulerAngles = eulerRotation;
    }

}


Pozdrawiam
Jo5zek
Jo5zek
 
Posty: 8
Rejestracja: 13 Lip 2019, 12:15
Has thanked: 0 time
Been thanked: 0 time

Wróć do Skrypty

Kto jest na forum

Użytkownicy przeglądający to forum: Google [Bot] oraz 1 gość