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
there are 2 players that's why i have hand1 and hand2
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);
}
}