How rotate camera to Mouse click - unity3d

I make viewer of models (with Unity3d). Now I make mouse interaction with model. How rotate camera to mouse click position. http://prntscr.com/990q9y

Maybe something like this?
You want "click", right?
Oh, You want to change camera.
if (Input.GetMouseButtonDown(0))
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
Camera.main.transform.localEulerAngles=hit.point;
}
}
//or try to fix the z thing:
if (Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Camera.main.transform.localEulerAngles = ray.origin + (ray.direction * 4.5f);
}
I see this here: http://answers.unity3d.com/questions/376735/get-world-coordinates-from-mouse-click.html
and maybe this can help you a lot: How to constrain rotation from mouse input in unity 5?

This code work, but how smoothly rotate?
if (Input.GetMouseButtonDown(0))
{
RaycastHit hit;
var pos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 10);
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
transform.transform.LookAt(hit.point);
player.UpdatePosition();
}
}

Related

Angle information(degree) between AR camera and target object on a plane

I have a question, please help and thanks!
I want to know how to show the Angle information(degree) between AR camera and target object on a plane. (Using Smartphone)
And I want to know how to "code" with Unity and AR Foundation.
I tried using some code(below), but it seems only work on Distance, not work on Angle...
Thank again!
void Start ()
{
//get the components
private ARRaycastManager RayManager;
private GameObject visual;
public Camera CameraStart;
public Text textField2;
public Text textField;
float distance;
float angle;
RayManager = FindObjectOfType<ARRaycastManager>();
visual = transform.GetChild(0).gameObject;
}
void Update ()
{
// shoot a raycast from the center of the screen
List<ARRaycastHit> hits = new List<ARRaycastHit>();
RayManager.Raycast(new Vector2(Screen.width / 2, Screen.height / 2), hits, TrackableType.Planes);
RaycastHit hit;
//Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
// check if we hit an AR plane, update the position and rotation
if(hits.Count > 0)
{
transform.position = hits[0].pose.position;
transform.rotation = hits[0].pose.rotation;
distance = Vector3.Distance(CameraStart.transform.position, transform.position);
textField.text = distance.ToString("N2") + "meter";
Physics.Raycast(transform.position, out hit);
angle = Vector3.Angle(hit.normal, transform.forward);
textField2.text = angle.ToString("N2") + "degree";
if(!visual.activeInHierarchy)
visual.SetActive(true);
}
}
}
I would check that the ray hits:
if (Physics.Raycast(transform.position, out hit)) {
angle = Vector3.Angle(hit.normal, transform.forward);
textField2.text = angle.ToString("N2") + "degree";
} else {
Debug.LogError("Did not hit")
}
Also I would make sure that the target has a collider and that the layer mask is correct.

Raycast how to deal with more than one object same layer

I am using raycast to detect that the player is grounded after a jump, my problem is that some times the raycast is not detecting that the player is grounded and it is.
This is my code for the raycasting:
void IsGrounded()
{
RaycastHit hit;
int mask = 1 << LayerMask.NameToLayer("Floor");
if (Physics.Raycast(transform.position, Vector3.down, out hit, 0.1f, mask))
{
isGrounded = true;
checkGround = false;
Debug.Log("is grounded");
if (!myAudioSource.isPlaying)
{
myAudioSource.Play();
}
}
Debug.DrawLine(transform.position, hit.point, Color.red);
}
Here is my problem:
The character is in the ground but the ray is still not detecting it.
Sometimes this same location will detect and others it won't.

Raycast rotate help need in unity

I have this code which lets an object rotate on Y axis. But I want to rotate the object on Z axis only. I tried to change the hPlane value, but no luck.
How can I do this?
Thanks
private Ray ray;
private Quaternion target;
private float speed = 1000.0f;
// Update is called once per frame
void Update () {
if (Input.GetMouseButton (0)) {
ray = Camera.main.ScreenPointToRay (Input.mousePosition);
Plane hPlane = new Plane(Vector3.up, Vector3.zero);
float distance = 0;
if(hPlane.Raycast(ray, out distance)){
Vector3 targetPoint = ray.GetPoint(distance);
targetPoint += transform.position;
Quaternion targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
float step = speed * Time.deltaTime;
transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation,step);
}
}
}
Update:
My Goal is to rotate a bar (cube mesh) on Z axis smoothly using mouse click . The rotation will occur only when (this part I could not do) the mouse touch the corners of the bar.
I have already done the Z axis rotation with this code but the rotation very low (even for speed =1000.0f) and it rotates clockwise I suppose. But I want to move the bar in both direction and the movement will be smooth and faster like Quaternion.RotateTowards
Thanks
private Ray ray;
private RaycastHit hit;
private Quaternion target;
private float speed = 1000.0f;
// Update is called once per frame
void Update () {
if (Input.GetMouseButton (0)) {
ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if(Physics.Raycast(ray, out hit)){
//transform.position = new Vector3(hit.point.x, hit.point.y, 0);
target = Quaternion.Euler(0.0f,0.0f, hit.point.z * speed);
transform.rotation = Quaternion.Slerp(transform.rotation, target, Time.deltaTime);
}
}
}
http://i.stack.imgur.com/7qIcs.jpg
I suggest you to change the orientation of the object axis. For a better use of the Quaternion functions, edit your bar in order to have local Y has rotation axis and local Z as look direction.
Then use this code :
if (Input.GetMouseButton(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
Vector3 distance = hit.point - transform.position;
distance.y = 0;
Quaternion targetRotation = Quaternion.LookRotation(distance);
transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation, Time.deltaTime * speed);
}
}
Your bar will only rotate around Y axis.

buttons only react to clicks/touches when clicking/touching to the right of them

I'm making a 2D game in Unity3D for android. Right now I'm making buttons. And this buttons does not react clicks/touched properly. I've got same issue with mouse clicks and touches both. Every button has trigger boxcollider with a same size as an object. BUT buttons react only when I click on area, that is right from a button. I don't understand why is it so. What should I do? Here is my code:
if (Input.GetMouseButtonDown(0)) {
Vector3 i = Camera.main.ScreenToWorldPoint (new Vector3 (Input.mousePosition.x, Input.mousePosition.y, 1));
RaycastHit2D hit = Physics2D.Raycast (i, i);
if (hit.transform != null) {
if (hit.transform.tag == "button") {
hit.transform.gameObject.SetActive(false);
}
}
}
Also, I've instantiated an object on mouse click on "i" position to check does it convert screen position to world correctly, and it works fine.
the first parameter in Physics2D.Raycast is the origin and the second one is direction so you should make the raycast from your ray.origin in the direction of ray.direction
void Update () {
if (Input.GetMouseButtonDown(0)) {
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit2D hit = Physics2D.Raycast (ray.origin, ray.direction, Mathf.Infinity);
if (hit) {
if(hit.collider.gameObject.tag=="button"){
//do something
}
}
}
}
Try to handle it by this way:
if (Input.GetMouseButtonDown(0)) {
Vector3 pos = Camera.main.ScreenToWorldPoint (Input.mousePosition);
Vector2 touchPos = new Vector2(pos.x, pos.y);
Collider2D hit = Physics2D.OverlapPoint(touchPos);
if (hit) {
Debug.Log(hit.transform.gameObject.name);
if (hit.transform.tag == "button") {
hit.transform.gameObject.SetActive(false);
}
}
}

movement on object click

On pressing down the object I want it to move continuously.I am a beginner in Unity 3D.
Please help.
function OnMouseDown()
{
Debug.Log("its a hit");
function Update()
{
transform.Translate(Vector3(0,0,3)*Time.deltaTime);
}
}
Check out this Unity Answer for several approaches on how to drag an object with the mouse:
http://answers.unity3d.com/questions/12322/drag-gameobject-with-mouse.html
Just click the mouse
function Update()
{
if(Input.GetMouseButtonDown(0)) //Left click
{
Debug.Log("Left Mouse Button Click");
transform.Translate(Vector3(0,0,3)*Time.deltaTime);
}
}
Or with select object
function Update()
{
if(Input.GetMouseButtonDown(0)) //Left click
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if(Physics.Raycast(ray, hit))
{
hit.collider.gameObject.Translate(Vector3(0,0,3)*Time.deltaTime);
}
}
}