Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by TheFatherOfGames · Jan 29, 2020 at 08:23 PM · rotationphysics2dvectoritems

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! :)

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by unity_ek98vnTRplGj8Q · Jan 29, 2020 at 09:30 PM

Please attach your code so we can help you out

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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); {

@unity_ek98vnTRplGj8Q

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image unity_ek98vnTRplGj8Q · Jan 29, 2020 at 11:05 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

231 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges