- Home /
mouse move rotate object question
So I have two scripts, one is my pickup object script and another is a left click drag object and it rotates script. I would like to know how to implement the rotate action in the rotate script into my pickup object script. I would like it so that I hold down "r" and i can now rotate the object i have picked up in view, around using either mouse movement or left click drag. When I release "r" it goes back to my normal pickup object stuff.
Pickup object script: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class LineOfSight : MonoBehaviour { //we assign this script to the first person character private RaycastHit vision; // this is where ill store the information from the raycast collision public float rayLength; // length of my raycast private bool isGrabbed; // the player is carrying the object private Rigidbody grabbedObject; // ill assign vision to this later as a reference to the objects rigidbody public float throwForce = 1000; // amount of force for throwing the object float rotSpeed = 10;
void Start()
{
rayLength = 4.0f; // set the raycast length from the start
isGrabbed = false; // game just started so the player is not carrying an object
}
void Update()
{
// Im using the first person character as my camera position, cast from there in forwards direction, store what I hit into the variable vision, and the raylength is the length of the raycast ray
if (Physics.Raycast(Camera.main.transform.position, Camera.main.transform.forward, out vision, rayLength))
{
if(vision.collider.tag == "interactable")// each interactable object has a tag called interactable
{
if (Input.GetKeyDown(KeyCode.E) && !isGrabbed) // if I hit E and I am not carrying an object
{
grabbedObject = vision.rigidbody; // assign the object we hit with raycasts rigidbody component to the variable grabbedObject
grabbedObject.isKinematic = true; // avoid recognizing collisions
grabbedObject.transform.SetParent(gameObject.transform); // set the grabbed object as a child of the camera
isGrabbed = true; // set the fact that im carrying something now
}
else if (isGrabbed && Input.GetKeyDown(KeyCode.E)) // if i am carrying something and I hit E, drop the object
{
grabbedObject.transform.parent = null; //detach from parent
grabbedObject.isKinematic = false; // turn collision detection on
isGrabbed = false; // set it so im not shown as carrying anything so that I can hit E again and pick it back up
}
else if (isGrabbed && Input.GetKey(KeyCode.R))
if (Input.GetMouseButtonDown(1)) // if i click right mouse button
{
grabbedObject.isKinematic = false; // bring back collision detection
grabbedObject.transform.parent = null; // remove from parent
isGrabbed = false; // set it so im not carrying anything so i can click E and pick up again
grabbedObject.AddForce(Camera.main.transform.forward * throwForce); // using the objects rigidbody, add force to it in forward direction
}
}
}
}
}
rotate object script:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class myRotate : MonoBehaviour { float rotSpeed = 10;
void OnMouseDrag()
{
float rotX = Input.GetAxis("Mouse X") * rotSpeed * Mathf.Deg2Rad;
float rotY = Input.GetAxis("Mouse Y") * rotSpeed * Mathf.Deg2Rad;
transform.RotateAround(Vector3.up, -rotX);
transform.RotateAround(Vector3.right, rotY);
}
}
Answer by netelak · Feb 22, 2019 at 10:20 PM
first create class for button in my case (ToolButton.cs) then add this script like below
currentButton = Instantiate(tool) as GameObject;
button = currentButton.AddComponent<ToolButton>();
button.tool = btn;
button.counter = counter;
in ToolButton scripts on Start() function you need to add Addlistener to each button
UnityEngine.Events.UnityAction action = () => { Play();}; GetComponent ().onClick.AddListener (action);
Your answer
![](https://koobas.hobune.stream/wayback/20220612192249im_/https://answers.unity.com/themes/thub/images/avi.jpg)