- Home /
I've got a lock On system Working, except for my camera is not sticking with my player meaning the character can walk out of view, any ideas?
So I've managed to make a targeting system with selecting closest targets and what not and my camera looks at the selected target, and my player movement like orbits around when moving left and right so that's all good, however when my character moves with the lock on active my camera is not following so i can just walk out of view which obviously I don't want. Any suggestions? Thanks. (I've been trying to get this working for a couple days now.
Camera code public class cameracontroller : MonoBehaviour {
public GameObject player; //Public variable to store a reference to the player game object
private Vector3 offset; //Private variable to store the offset distance between the player and camera
private float speed;
public Targeting cameraTarget;
//Roatation Stuff
public float SmoothFactor = 0.5f;
public bool LookAtPlayer = false;
public bool RotateAroundPlayer = true;
public float RotationSpeed = 5.0f;
// Use this for initialization
void Start()
{
//Calculate and store the offset value by getting the distance between the player's position and camera's position.
offset = transform.position - player.transform.position;
}
// LateUpdate is called after Update each frame
void FixedUpdate()
{
if (RotateAroundPlayer && cameraTarget.LockedOn == false)
{
Quaternion camTurnAngle = Quaternion.AngleAxis(Input.GetAxis("CameraHorizontal") * RotationSpeed, Vector3.up);
offset = camTurnAngle * offset;
}
if (RotateAroundPlayer && cameraTarget.LockedOn == false)
{
Quaternion camTurnAngle = Quaternion.AngleAxis(Input.GetAxis("Horizontal") * RotationSpeed, Vector3.up);
offset = camTurnAngle * offset;
}
// Set the position of the camera's transform to be the same as the player's, but offset by the calculated offset distance.
Vector3 newPos = transform.position = player.transform.position + offset;
transform.position = Vector3.Slerp(transform.position, newPos, SmoothFactor);
if (LookAtPlayer || RotateAroundPlayer && cameraTarget.LockedOn == false)
{
transform.LookAt(player.transform);
}
if (LookAtPlayer || RotateAroundPlayer && cameraTarget.LockedOn == true)
{
transform.LookAt(cameraTarget.selectTarget.GetComponent<Transform>());
}
}
}
Your answer
Follow this Question
Related Questions
Making a bubble level (not a game but work tool) 1 Answer
Multiple Cars not working 1 Answer
How To Desync A Player's Camera? 1 Answer
Distribute terrain in zones 3 Answers
Locking on to enemy, player can only move towards enemy, not away? 0 Answers