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 /
avatar image
0
Question by dabluezpreacher · Feb 22, 2019 at 08:28 PM · colliderrotateonmousedrag

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

}

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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

ncsecu login

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 dabluezpreacher · Feb 22, 2019 at 10:47 PM 0
Share

what does this have to do with my question?

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

131 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

Related Questions

Camera rotation around player while following. 6 Answers

Stop a door from rotating when it touches player? 1 Answer

How to rotate an object but keep it's collider 2 Answers

Problem rotating spheres around empty parent 1 Answer

How do I rotate a Collider? 3 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