- Home /
Question by
ShiNoKitsune · Oct 17, 2017 at 11:17 AM ·
cameraraycastsmoothhow3rd person camera
How do I can make this 3rd Person camera Smoother when RayCast?
I want to make the Camera Smoother when RayCast and dont go foward like a snap, just make a smoother transition;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Camer : MonoBehaviour {
public bool lockCursor;
public float mouseSensitivity = 10;
public Transform Target;
public float distFromTarget=30;
public Vector2 pitchMaxMin = new Vector2 (0, 85);
public float rotationSmoothTime = 0.12f;
Vector3 rotationSmoothVelocity;
Vector3 currentRotation;
public float MoveCamera;
RaycastHit hit = new RaycastHit ();
float yaw;
float pitch;
void Start () {
if(lockCursor)
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
}
void LateUpdate () {
yaw += Input.GetAxis ("Mouse X") * mouseSensitivity;
pitch -= Input.GetAxis ("Mouse Y") * mouseSensitivity;
pitch = Mathf.Clamp (pitch, pitchMaxMin.x, pitchMaxMin.y);
currentRotation = Vector3.SmoothDamp (currentRotation, new Vector3 (pitch,yaw),ref rotationSmoothVelocity,rotationSmoothTime);
transform.eulerAngles = currentRotation;
transform.position = Target.position - transform.forward* distFromTarget;
if (Physics.Linecast(Target.position,transform.position,out hit)){
transform.position = hit.point + transform.forward * MoveCamera;
}
}
}
Comment
hi , make the Camera Smoother when RayCast and dont go foward like a snap, just make a smoother transition ?§ what exactly you want ? a zoom transition ?
Best Answer
Answer by Desoro · Oct 31, 2017 at 11:31 AM
Use Vector3.SmoothDamp for position as well.
Vector3 targetPosition = Target.position - transform.forward* distFromTarget;
if (Physics.Linecast(Target.position,targetPosition,out hit))
{
targetPosition = hit.point + transform.forward * MoveCamera;
}
transform.position = Vector3.SmoothDamp(transform.position, targetPosition, ref velocity, smoothTime);