From 1315015b94211f1ad7a56fb5679e06768c6e6be3 Mon Sep 17 00:00:00 2001 From: Stefano Calabretti Date: Sat, 1 Dec 2018 19:29:30 +0100 Subject: [PATCH] updated RotateTowardsMouse. You might want to try out with and without using slerp (useSlerp boolean) and by using directly the Input.mousePosition as the lookDirection --- Assets/Scripts/RotateTowardsMouse.cs | 45 +++++++++++++++++++++++----- 1 file changed, 38 insertions(+), 7 deletions(-) diff --git a/Assets/Scripts/RotateTowardsMouse.cs b/Assets/Scripts/RotateTowardsMouse.cs index 78fdfad..22c0447 100644 --- a/Assets/Scripts/RotateTowardsMouse.cs +++ b/Assets/Scripts/RotateTowardsMouse.cs @@ -2,14 +2,45 @@ using System.Collections.Generic; using UnityEngine; -public class RotateTowardsMouse : MonoBehaviour { - //Circa qualcosa così +public class RotateTowardsMouse : MonoBehaviour +{ + /// + /// Velocità a cui far ruotare il braccio. + /// + public int speed = 1; - void Update() { + /// + /// Per testing. + /// Determina se bisogna utilizzare + /// Quaternion.Slerp() o Quaternion.LookDirection(). + /// + public bool useSlerp = false; + + void Update() + { //Trova la direzione tra il transform e il braccio - Vector2 screenPosition = Camera.main.WorldToScreenPoint(transform.position); - Vector2 direction = (Vector2)Input.mousePosition - screenPosition; - //Cambia la rotazione dell'oggetto - transform.rotation = Quaternion.identity; + Vector2 screenPosition = + Camera.main.WorldToScreenPoint(transform.position); + + Vector2 direction = + (Input.mousePosition - screenPosition) as Vector2; + + Vector3 lookDirection = + new Vector3(direction.x, direction.y); + + /* + * In alternativa provare direttamente con + * lookDirection = Input.mousePosition; + */ + if (useSlerp) + { + Quaternion.Slerp ( + transform.rotation, // Rotazione iniziale + Quaternion.LookRotation (lookDirection), // Rotazione finale + speed * Time.deltaTime); // Velocità dell'interpolazione + } + + else // Questo dovrebbe bastare + Quaternion.LookRotation (lookDirection); } }