- Home /
Question by
disslah26 · Jul 27, 2020 at 05:06 PM ·
childrotate objectmouseposition
How to rotate a child object vertically with the mouse ?
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Camera_player : MonoBehaviour {
public float speed;
void Update()
{
Lookatmouse();
}
void Lookatmouse(){
Plane playerplane = new Plane(Vector3.up,transform.position);
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
float hitdist;
if(playerplane.Raycast(ray, out hitdist)){
Vector3 targetpoint = ray.GetPoint(hitdist);
Quaternion targetrotation = Quaternion.LookRotation(targetpoint-transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation,targetrotation,speed*Time.deltaTime*speed);
}
}
}
the above code rotate the player with mouse around Y axis. How do I modify this code to rotate the object around Z or X axis. Thank You.
Comment
Best Answer
Answer by Llama_w_2Ls · Jul 27, 2020 at 05:13 PM
Instead of Vector3.Up, try Vector3.Right?
Thank you. That is Ok But it rotates around Y axis too. How to stop that