Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Ochreous · May 31, 2013 at 10:56 PM · c#mouseclick

C# moveOnMouseClick Rotation

Hi everyone, I'm trying to edit the moveOnMouseClick from the Unify wiki so it won't rotate the camera. But if I make it so it only rotates the object then the object doesn't rotate. Any ideas?

 /* 
  * Esse Script movimenta o GameObject quando você clica ou
  * mantém o botão esquerdo do mouse apertado.
  * 
  * Para usá-lo, adicione esse script ao gameObject que você quer mover
  * seja o Player ou outro
  * 
  * Autor: Vinicius Rezendrix - Brasil
  * Data: 11/08/2012
  * 
  * This script moves the GameObeject when you
  * click or click and hold the LeftMouseButton
  * 
  * Simply attach it to the gameObject you wanna move (player or not)
  * 
  * Autor: Vinicius Rezendrix - Brazil
  * Data: 11/08/2012 
  *
  */
  
 using UnityEngine;
 using System.Collections;
  
 public class moveOnMouseClick : MonoBehaviour {
     private Transform myTransform;                // this transform
     private Vector3 destinationPosition;        // The destination Point
     private float destinationDistance;            // The distance between myTransform and destinationPosition
     public float moveSpeed;                        // The Speed the character will move
  
  
  
     void Start () {
         myTransform = transform;                            // sets myTransform to this GameObject.transform
         destinationPosition = myTransform.position;            // prevents myTransform reset
     }
     void Update () {
         // keep track of the distance between this gameObject and destinationPosition
         destinationDistance = Vector3.Distance(destinationPosition, myTransform.position);
  
         if(destinationDistance < .5f){        // To prevent shakin behavior when near destination
             moveSpeed = 0;
         }
         else if(destinationDistance > .5f){            // To Reset Speed to default
             moveSpeed = 6;
         }
  
  
         // Moves the Player if the Left Mouse Button was clicked
         if (Input.GetMouseButtonDown(0)&& GUIUtility.hotControl ==0) {
  
             Plane playerPlane = new Plane(Vector3.up, myTransform.position);
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             float hitdist = 0.0f;
  
             if (playerPlane.Raycast(ray, out hitdist)) {
                 Vector3 targetPoint = ray.GetPoint(hitdist);
                 destinationPosition = ray.GetPoint(hitdist);
                 Quaternion targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
                 //myTransform.rotation = targetRotation;
             }
         }
  
         // Moves the player if the mouse button is hold down
         else if (Input.GetMouseButton(0)&& GUIUtility.hotControl ==0) {
  
             Plane playerPlane = new Plane(Vector3.up, myTransform.position);
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             float hitdist = 0.0f;
  
             if (playerPlane.Raycast(ray, out hitdist)) {
                 Vector3 targetPoint = ray.GetPoint(hitdist);
                 destinationPosition = ray.GetPoint(hitdist);
                 Quaternion targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
                 //myTransform.rotation = targetRotation;
             }
         //    myTransform.position = Vector3.MoveTowards(myTransform.position, destinationPosition, moveSpeed * Time.deltaTime);
         }
  
         // To prevent code from running if not needed
         if(destinationDistance > .5f){
             myTransform.position = Vector3.MoveTowards(myTransform.position, destinationPosition, moveSpeed * Time.deltaTime);
         }
     }
 }
Comment
Add comment · Show 4
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 ExTheSea · May 31, 2013 at 11:15 PM 0
Share

I'm not quite familiar with this script but doesn't this line you commented out rotate the player/object?

 //myTransform.rotation = targetRotation;
avatar image Ochreous · Jun 01, 2013 at 12:26 AM 0
Share

Yes, I'm try to get only the mesh to rotate. That line causes the whole thing to rotate.

avatar image robertbu · Jun 01, 2013 at 01:14 AM 0
Share

This script does not rotate the camera. So if the camera is rotating, it is not this script that is doing the rotation. Or maybe I just don't understand the issue you are trying to solve.

avatar image Ochreous · Jun 01, 2013 at 02:55 AM 0
Share

The Camera is parented to the object that is being rotated and that is what is causing the camera to rotate. I can't unparent it to the object otherwise it will give me a null reference error. Is there a way I could freeze the camera so it can't rotate?

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by robertbu · Jun 01, 2013 at 03:18 AM

Try this:

 using UnityEngine;
 using System.Collections;
 
 public class NoRotation : MonoBehaviour {
 
     private Quaternion q;
     
     void Start () {
         q = transform.rotation;
     }
     
     void Update () {
         transform.rotation = q;
     }
 }

Or you could figure out and fix the null reference error.

Comment
Add comment · Show 5 · 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 Ochreous · Jun 01, 2013 at 04:32 AM 0
Share

The script still rotates the camera. I changed your script so it would only affect the main camera's rotation specifically. Is there something wrong with my script?

     using UnityEngine;
     using System.Collections;
      
     public class NoRotation : $$anonymous$$onoBehaviour {
      
     private Quaternion q;
      
     void Start () {
     q = Camera.main.transform.rotation;
     }
      
     void Update () {
     Camera.main.transform.rotation = q;
     }
     }
avatar image robertbu · Jun 01, 2013 at 05:06 AM 0
Share

Try using LateUpdate() ins$$anonymous$$d of Update(). If that does not work, try changing the script execution order.

avatar image Ochreous · Jun 01, 2013 at 05:11 AM 0
Share

I figured out that only the z axis is rotating. How do I specifically set the main camera's z axis so it can't rotate? I tried the following below but the console gives me an error saying I can't assign it.

 void Update () {
 Camera.main.transform.rotation.z = 0;
 }
avatar image Ochreous · Jun 01, 2013 at 06:37 AM 0
Share

I finally got it to work with Camera.main.transform.eulerAngles . But now the mesh doesn't rotate where I click. Is there a way to make it so the z axis faces wherever I click?

avatar image robertbu · Jun 01, 2013 at 06:43 AM 0
Share

"Wherever you click" can have many meanings given that your mouse is on a 2D surface, but your game objects live in a 3D world. You can use Camera.main.ScreenToWorldPoint() to transform the mouse position into a world coordinate. You cannot just pass Input.mousePosition. The 'Z' parameter of the vector you pass in is the distance in front of the camera, and it cannot be 0;

After you've made the translation, you can use Transform.LookAt() to have the object look at that point. Where the object looks will vary depending on how close the point you calculated is to the object you are rotating.

There are many posts on getting an object to follow/face the mouse position.

avatar image
0

Answer by MutualTeats · Aug 13, 2013 at 02:09 PM

If I got your question right you want to rotate the gameObject, but not the camera (it should be fixed like in Pokémon Colosseum or Diablo). That means that you'll need to use one script for the gameObject and one for the camera. The script you posted for the gameObject will probably work fine (though it could be shortened). Here is the script for the camera: http://pastebin.com/eaaJSz30

Note: The camera should NOT be a child to the player

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

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

15 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

Related Questions

Get GameObject That Was Last Clicked? 2 Answers

C# Emit Particle Upon Left Mouse Click 2 Answers

C# Track Mouse Cursor Movement Speed 2 Answers

C# Decrease Mouse Sensitivity Camera Move 0 Answers

C# Gameobject Rigidbody Mouse Collision 1 Answer


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