- Home /
Question by
Vekosava · Mar 20, 2016 at 03:14 AM ·
2drotationscripting problemobject
Arm Rotation toward Cursor Script Help
Hey guys! I’ve been doing some game where arm needs to follow cursor, it works,but I don’t want arm to follow cursor 360 degrees, I want about 180, so not to go under player. I don’t know how to get it work. Here is the script:
using UnityEngine;
using System.Collections;
public class WeaponRotationScript : MonoBehaviour {
void Update () {
Vector3 mousePos = Input.mousePosition;
mousePos.z = 5.23f;
Vector3 objectPos = Camera.main.WorldToScreenPoint(transform.position);
mousePos.x = mousePos.x - objectPos.x;
mousePos.y = mousePos.y - objectPos.y;
float angle = Mathf.Atan2(mousePos.y, mousePos.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(new Vector3(0, 0, angle));
}
Script Image: http://prntscr.com/agyykp
Can someone help me?
Comment
Answer by Darkwing00Duck · Mar 22, 2016 at 04:03 AM
hello @Vekosava, maybe add a check for the current degree
Vector3 mousePos = Input.mousePosition;
mousePos.z = 5.23f;
Vector3 objectPos = Camera.main.WorldToScreenPoint(transform.position);
mousePos.x = mousePos.x - objectPos.x;
mousePos.y = mousePos.y - objectPos.y;
float angle = Mathf.Atan2(mousePos.y, mousePos.x) * Mathf.Rad2Deg;
if ((angle > 0) || (angle < 180))
{
transform.rotation = Quaternion.Euler(new Vector3(0, 0, angle));
}