- Home /
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);
}
}
}
I'm not quite familiar with this script but doesn't this line you commented out rotate the player/object?
//myTransform.rotation = targetRotation;
Yes, I'm try to get only the mesh to rotate. That line causes the whole thing to rotate.
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.
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?
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.
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;
}
}
Try using LateUpdate() ins$$anonymous$$d of Update(). If that does not work, try changing the script execution order.
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;
}
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?
"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.
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
Your answer
Follow this Question
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