M
M
MaxXxWell2020-04-15 14:04:32
C++ / C#
MaxXxWell, 2020-04-15 14:04:32

How to fix the spread code?

Hello everyone, can someone help with the code? The code should rotate the object along the Z axis and it works, I click on the object and turn left / right, but every time I remove my finger and click on the object again, it itself unfolds to the place of my touch, and I need so that I can continue U-turn from the place where I released the press, but here it turns out every time to turn a new one. No matter how much you try, I couldn’t set up the code, below is the code itself:

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

public class ToucTurn : MonoBehaviour
{

[Header("Degree of rotation offset. *360")]
publicfloat offset = 0f;

Vector3 startDragDir;
Vector3 currentDragDir;
Quaternion initialRotation;
float angleFromStart;

void OnMouseDown()
{

startDragDir = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;

initialRotation = transform.rotation;
}

void OnMouseDrag()
{
Vector3 difference = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;

difference.Normalize( );

float rotationZ = Mathf.Atan2( difference.y, difference.x) * Mathf.Rad2Deg;

transform.rotation = Quaternion.Euler( 0f, 0f, rotationZ - (0 + offset) );
}
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question