Question by
Imamul125 · Oct 15, 2018 at 07:43 AM ·
errorcamera rotatethird-person-camera
can someone help me out what is wrong in this code?
this script was working fine. untill i modified to add camera collision. my code was fine i got the result, i wanted to achieve i.e my mouse would rotate around the character freely(orbit). then i modified the script to add camera collision. now it is causing shaking camera movement. please note i have also different script in my camera to keep looking to my character.
{
public Transform target;
public Transform target1; // this is for collision purpose use player here
public float distance = 10.0f;
public float xSpeed = 250.0f;
public float ySpeed = 120.0f;
public float yMinLimit = -20f;
public float yMaxLimit = 80f;
private float x = 0.0f;
private float y = 0.0f;
public float moveSpeed = 5;
public float returnSpeed = 9;
public float wallPush = 0.7f;
public float closestDistanceToPlayer = 2f;
public float evenCloserDistanceToPlayer = 1f;
public LayerMask collisionMask;
private bool pitchLock = false;
public Vector2 pitchMinMax = new Vector2(-48, 85);
float pitch;
Vector3 currentRotation;
public float rotationSmoothTime = 8f;
Vector3 rotationSmoothVelocity;
// @script AddComponentMenu("Camera-Control/Mouse Orbit")
void Start()
{
var angles = transform.eulerAngles;
x = angles.y;
y = angles.x;
// Make the rigid body not change rotation
if (GetComponent<Rigidbody>())
GetComponent<Rigidbody>().freezeRotation = true;
}
void LateUpdate()
{
if (target)
{
var position = new Vector3(0.0f, 0.0f, -distance) + target.position;
CollisionCheck(position);
if (!pitchLock)
{
x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
y = ClampAngle(y, yMinLimit, yMaxLimit);
var rotation = Quaternion.Euler(y, x, 0);
// var position = new Vector3(0.0f, 0.0f, -distance) + target.position;
transform.rotation = rotation;
transform.position = position;
}
else
{
x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
pitch = pitchMinMax.y;
currentRotation = Vector3.Lerp(currentRotation, new Vector3(pitch, x), rotationSmoothTime * Time.deltaTime);
}
}
}
private void WallCheck()
{
Ray ray = new Ray(target1.position, -target1.forward);
RaycastHit hit;
if (Physics.SphereCast(ray, 0.5f, out hit, 0.7f, collisionMask))
{
pitchLock = true;
}
else
pitchLock = false;
}
private void CollisionCheck(Vector3 retpoint)
{
RaycastHit hit;
if(Physics.Linecast(target1.position, retpoint, out hit, collisionMask))
{
Vector3 norm = hit.normal * wallPush;
Vector3 p = hit.point + norm;
if(Vector3.Distance(Vector3.Lerp(transform.position, p, moveSpeed * Time.deltaTime), target1.position) <= evenCloserDistanceToPlayer)
{
}
else
{
transform.position = Vector3.Lerp(transform.position, p, moveSpeed * Time.deltaTime);
}
return;
}
transform.position = Vector3.Lerp(transform.position, retpoint, returnSpeed * Time.deltaTime);
pitchLock = false;
}
static float ClampAngle(float angle, float min, float max)
{
if (angle < -360)
angle += 360;
if (angle > 360)
angle -= 360;
return Mathf.Clamp(angle, min, max);
}
}
Comment
Your answer