How do i rotate an object towards mouse?
So basically I'm making a game with a stickman that can pick up stuff. I added guns and made pickable items. The gun is attached to the hand of the stickman which follows the mouse cursor. The problem is the gun doesn't rotate as the hand goes right and it flips 180 degrees. I'm really a beginner so i don't really know much. Would appreciate some help! :)
Answer by unity_ek98vnTRplGj8Q · Jan 29, 2020 at 09:30 PM
Please attach your code so we can help you out
Answer by TheFatherOfGames · Jan 29, 2020 at 10:09 PM
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EquipManager : MonoBehaviour
{
private Dictionary<BodyPartType, BodyPart> bodyParts = new Dictionary<BodyPartType, BodyPart>();
public List<Item> inventory = new List<Item>();
public Equipable starting_item;
public Limb handL;
Camera cam;
private bool holding;
void Start()
{
holding = false;
BodyPart[] get_parts = transform.GetComponentsInChildren<BodyPart>();
foreach(var part in get_parts) {
bodyParts.Add(part.type, part);
}
EquipItem(starting_item);
cam = Camera.main;
}
void Pickup(Pickable pick) {
if(!pick)
return;
var item = pick.item;
if(item.GetType() == typeof(Equipable)) {
EquipItem((Equipable)item);
} else {
inventory.Add(item);
}
pick.PickUp();
}
void EquipItem(Equipable item) {
if(!item)
return;
if(bodyParts.TryGetValue(item.type, out var part)) {
part.Equip(item);
// if(handL.transform.rotation.z == 170) {
// item.transform.Rotate(0, 180, 0);
// }
}
}
void Update()
{
if(Input.GetKeyDown(KeyCode.G)) {
Pickup(Pickable.GetItem(transform.position, 10));
holding = true;
}
if(holding) {
var dir = cam.ScreenToWorldPoint(Input.mousePosition) - transform.position;
//handR.setPosition(dir, 2.5f);
handL.setPosition(dir, 2.5f);
}
}
}
basically what i tried is if(handL.transform.rotation.z == 170) { item.transform.Rotate(0, 180, 0); {
Ok, a couple comments about your fix just so you know --
You should never directly reference a rotation's x,y, or z. A rotation is a quaternion, and these variables are not something that should ever be looked at unless you know what you are doing. Usually you want to access the rotations in degrees around each axis, this is rotation.eulerAngles or rotation.localEulerangles.
You should almost never use '==' when comparing an objects position or rotation to some value. That is because these values are stored as floats. For example
transform.position = new Vector3(0.01,0,0);
bool isEqual = (transform.position.x == 0.01); //This will be false
This is false because floats can't express some numbers precisely, just get a very very close approximation. When comparing floats, you should either convert to an integer first or you should use '>=' or '
void Update()
{
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.G)) {
Pickup(Pickable.GetItem(transform.position, 10));
holding = true;
}
if(holding) {
var dir = cam.ScreenToWorldPoint(Input.mousePosition) - transform.position;
//handR.setPosition(dir, 2.5f);
handL.setPosition(dir, 2.5f);
//ADD THIS CODE
GameObject equippedItem; //You will need to set this equal to your currently equipped item
//Flip if on the left side of the body
if(dir.x < 0) equipped.transform.localScale = new Vector3(1,-1,1);
else equipped.transform.localScale = new Vector3(1,1,1);
}
}
Your answer
Follow this Question
Related Questions
According to Collision Change Rotation of Car 1 Answer
2D Top Down, Enemy facing player slow rotation issue. Vector gets messed up after collision. 3 Answers
I have two RaycastHit2Ds but only one is activating 0 Answers
Rotate Player "slowly" towards default rotation. (2D Rigidbody) 1 Answer
OverlapBoxAll rotation 2 Answers