- Home /
slow look at question
I have a script that points a gun where you click on the screen but right now it just jumps to where you click instead of slowly rotating to it. How can I make it slowly rotate?
public Transform gun;
private Vector3 mouseWorldPoint;
void Update ()
{
if(Input.GetMouseButtonDown(0))
{
mouseWorldPoint = Camera.mainCamera.ScreenToWorldPoint(newVector3(Input.mousePosition.x, Input.mousePosition.y, -Camera.mainCamera.transform.position.z));
mouseWorldPoint.z = gun.position.z;
gun.LookAt(mouseWorldPoint);
}
thanks for your help!
that doesn't work becaue the mouseWorldPoint is a vecto3 and the gun.rotation is a quaternion
"transform.rotation.eulerAngles" gives you the corresponding rotation in Vector3 type.
"Quaternion.Euler(x,y,z)" gives you the quaternion corresponding of a given Vector3.
I meant to complement your suggestion, @cj... Here, try this: Vector3.RotateTowards(gun.eulerAngles, mouseWorldPoint);
Answer by Rod-Green · Dec 28, 2011 at 08:49 AM
Here you go.. change the up vector for the LookRotation as you need... also the speed will of course speed up the look movement
using UnityEngine;
using System.Collections.Generic;
public class SlowLookAt : MonoBehaviour
{
public Transform gun;
public float speed = 1.0f;
private Vector3 mouseWorldPoint;
private Quaternion newLookTarget;
void Awake()
{
newLookTarget = gun.rotation;
}
void Update ()
{
if (Input.GetMouseButtonDown (0)) {
mouseWorldPoint = Camera.mainCamera.ScreenToWorldPoint (new Vector3(Input.mousePosition.x, Input.mousePosition.y, -Camera.mainCamera.transform.position.z));
mouseWorldPoint.z = gun.position.z;
Quaternion curRot = gun.rotation;
newLookTarget = Quaternion.LookRotation(mouseWorldPoint - gun.transform.position, Vector3.up);
}
if(newLookTarget != gun.rotation)
{
gun.rotation = Quaternion.Lerp(gun.rotation, newLookTarget, Time.deltaTime * speed);
}
}
}
Your answer
Follow this Question
Related Questions
Rotating while rotating? 1 Answer
Distribute terrain in zones 3 Answers
Rotation with multiple objects 1 Answer
Object isnt rotating when moving towards an object 1 Answer
How to rotate towards a target but only on the y axis ? 1 Answer