Rotation starts freaking out when I put my cursor on it.
So basically I am making a platformer with a orbiting spear mechanic and I encountered an error.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpearRotationController : MonoBehaviour
{
public GameObject player;
public GameObject spear;
public float offset;
void Update()
{
Vector3 difference = Camera.main.ScreenToWorldPoint(Input.mousePosition) -
spear.transform.position;
float rotZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(0f, 0f, rotZ + offset);
}
}
I am using this code to rotate an empty game object by the difference as an angle between the mouse position and spear position (with the added offset). The spear is parented to this object so it rotates around the player (because the game object is parented to the player and moves with it). And everything works fine until the mouse hovers over the spear and then the spear starts freaking out. I don't exactly understand the radian code because I am a beginner but I understand what is doing to a basic degree. If anyone has any suggestions please reply.
Many thanks in advance.
add a $$anonymous$$imum distance limit to the rotation code....like this
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpearRotationController : MonoBehaviour {
public GameObject player;
public GameObject spear;
public float offset;
public float rotationThreshold;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
Vector3 difference = Camera.main.ScreenToWorldPoint(Input.mousePosition) -
spear.transform.position;
if(Vector3.Distance(Camera.main.ScreenToWorldPoint(Input.mousePosition), spear.transform.posittion) > rotationThreshold)
float rotZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(0f, 0f, rotZ + offset);
}
}
set the value to something small and you should be good to go...hope this helps
No sorry it didn't. I should also mention that it also does this when the mouse cursor gets close and not just when it hovers directly over it but I'm pretty sure your solution would of fixed this.
If you measure an angle from spear.transform.position
then move it - this offset becomes rotation per frame and not an offset at all.
Answer by andrew-lukasik · Jul 27, 2021 at 05:53 PM
using UnityEngine;
public class SpearRotationController : MonoBehaviour
{
[SerializeField] GameObject player;
[SerializeField] GameObject spear;
[SerializeField] float offset;
void Update ()
{
Vector2 dir = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
float rotZ = Mathf.Atan2( dir.y , dir.x ) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler( 0f , 0f , rotZ+offset );
}
}
Jedyny problem w tym kodzie był taki, że mierzyłeś kąt względem spear.transform.position
(który był obracany) a nie transform.position
(stał w miejscu). Powstawała przez to pętla zwrotna, która co klatkę obracała wszystko jak wiatrak.
Your answer
Follow this Question
Related Questions
Unity lookat at finger position 3D C# 0 Answers
How can i rotate an object using LookAt with only 1 axis? 0 Answers
Know when the object is rotated? 1 Answer
Object rotates normaly while moving, then does zig zags? 0 Answers
How to move an object on a 2d plane forward, backward, and in a rotation about the mouse position? 0 Answers