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 · Feb 03, 2020 at 04:57 PM · rotationobjectsjoystickcursormouseposition

How do i rotate an object towards joystick cursor position?

I tried making a game object(joystick cursor) and attaching it to the script where my following object(an arm) is. Here's thje script :

 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 Transform Xcursor;
 
     public Limb handL;
     public Limb handL_P2;
     Camera cam;
     public string pickP2 = "pickP2";
     
     
 
     private bool holding;
     private bool holding2;
      //You will need to set this equal to your currently equipped item
 
     void Start()
     {   
         holding = false;
         holding2 = 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);
            
         }
     }
 
    
 
     
     void Update()
     {   
         
         if(Input.GetKeyDown(KeyCode.G) && !holding ) 
         {
            Pickup(Pickable.GetItem(transform.position, 10));
            holding = true;
         }
        
          if(holding) 
         {
             
              var dir = cam.ScreenToWorldPoint(Input.mousePosition) - transform.position;
              handL.setPosition(dir, 2.5f);
             
         }
         if(Input.GetButtonDown(pickP2) && !holding2)
         {
             Pickup(Pickable.GetItem(transform.position, 10));
             holding2 = true;
         }
         if(holding2)
         {
              var dir2 = cam.ScreenToWorldPoint(Xcursor.position) - transform.position;
              handL_P2.setPosition(dir2, 2.5f);
         }
         
      }
 }
 







Comment
Add comment · Show 2
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 TheFatherOfGames · Feb 03, 2020 at 04:59 PM 0
Share

there are 2 players that's why i have hand1 and hand2

avatar image TheFatherOfGames · Feb 03, 2020 at 06:13 PM 0
Share

Here's the cursor script

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class XboxCursor : $$anonymous$$onoBehaviour
 {
     public Texture2D cursorImage;
     private int cursorWidth = 32;
     private int cursorHeight = 32;
     public float horizontalSpeed = 200f;
     public float verticalSpeed = 200f;
     private Vector2 cursorPosition;
 
     private void Start()
     {
     Cursor.visible = false;
 
     // optional place it in the center on start
     cursorPosition = new Vector2(Screen.width/2f, Screen.height/2f);
     }
 
   private void OnGUI()
   {
     // these are not actual positions but the change between last frame and now
     float h = horizontalSpeed * Input.GetAxis("Joystick X") * Time.deltaTime;
     float v = verticalSpeed * Input.GetAxis("Joystick Y") * Time.deltaTime;
 
     // add the changes to the actual cursor position
     cursorPosition.x += h;
     cursorPosition.y += v;
 
     GUI.DrawTexture(new Rect(cursorPosition.x, Screen.height - cursorPosition.y, cursorWidth, cursorHeight), cursorImage);
   }
 }


0 Replies

· Add your reply
  • Sort: 

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

233 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 avatar image avatar image

Related Questions

ScreenToViewportPoint(Input.mousePosition) to Rotate Object Requires Cursor to Rotate around (0, 0) of Viewport 0 Answers

Rotate weapon with the crossair and cursor lock 1 Answer

Left Joystick Translation Depending on Facing Direction 1 Answer

Rotation on moving using virtual joystick is not working as expected in 3D game. 0 Answers

Rotating a texture with an object 0 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