- Home /
Trying to make a third person camera, whats wrong with this transform.position?
im trying to make it so that if the mouse is moved up, the camera moves back, moves down, and rotates up. and the opposite when the mouse moves down. This variable is applied to the localposition:
CameraPos = new Vector3 (transform.localPosition.x, Mathf.Clamp (CameraPos.y + (MouseY - MouseYSet) * Sensitivity * 10, CameraTarget.transform.position.y + CameraTarget.transform.localScale.y / 2, CameraTarget.transform.position.y + CameraTarget.transform.localScale.y / 2 + MaxPos), Mathf.Clamp (CameraPos.z + (MouseY - MouseYSet) * Sensitivity * 10, CameraTarget.transform.position.z - MaxPos, CameraTarget.transform.position.z));
try making a player, making the camera a child, and applying this script to the camera:
using UnityEngine;
using System.Collections;
public class CameraController : MonoBehaviour {
public float Sensitivity;
public float MaxPos;
public float MaxAngle;
private float MouseY;
private float MouseYSet;
private float CameraAngle;
private Vector3 CameraPos;
public GameObject CameraTarget;
void Start () {
}
void Update () {
// Rotation controls
MouseY -= Input.GetAxisRaw ("Mouse Y");
CameraAngle = Mathf.Clamp (CameraAngle + (MouseY - MouseYSet) * Sensitivity * 10, -MaxAngle, MaxAngle);
CameraPos = new Vector3 (transform.localPosition.x, Mathf.Clamp (CameraPos.y + (MouseY - MouseYSet) * Sensitivity * 10, CameraTarget.transform.position.y + CameraTarget.transform.localScale.y / 2, CameraTarget.transform.position.y + CameraTarget.transform.localScale.y / 2 + MaxPos), Mathf.Clamp (CameraPos.z + (MouseY - MouseYSet) * Sensitivity * 10, CameraTarget.transform.position.z - MaxPos, CameraTarget.transform.position.z));
if (MouseY != 0)
{
transform.localRotation = Quaternion.Euler (CameraAngle, 0, 0);
transform.localPosition = new Vector3 (0, CameraPos.y, CameraPos.z);
MouseYSet = MouseY;
}
}
}
and this to the player:
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
public int Speed;
public int RunSpeed;
public int Sensitivity;
public int JumpHeight;
public int Force;
private float TerrainHeight;
private float TerrainHeightSet;
private float PlayerFoot;
private float MouseX;
private float MouseXSet;
private bool Jumping;
private bool Running;
void Start () {
Jumping = false;
Running = false;
}
void Update () {
PlayerFoot = transform.position.y - transform.lossyScale.y / 2;
TerrainHeight = Terrain.activeTerrain.SampleHeight (transform.position);
// Translation controls
if (Input.GetKey (KeyCode.W) && Running == false)
{
transform.position += transform.forward * Speed * 20 * Time.deltaTime;
}
if (Input.GetKey (KeyCode.S) && Running == false)
{
transform.position -= transform.forward * Speed * 20 * Time.deltaTime;
}
if (Input.GetKey (KeyCode.D) && Running == false)
{
transform.position += transform.right * Speed * 20 * Time.deltaTime;
}
if (Input.GetKey (KeyCode.A) && Running == false)
{
transform.position -= transform.right * Speed * 20 * Time.deltaTime;
}
// Run controls
if (Input.GetKey (KeyCode.W) && Input.GetKey (KeyCode.Q))
{
Running = true;
}
if (Input.GetKey (KeyCode.W) && Running == true)
{
transform.position += transform.forward * RunSpeed * 20 * Time.deltaTime;
}
if (Input.GetKeyUp (KeyCode.Q) && Input.GetKeyUp (KeyCode.W))
{
Running = false;
}
// Rotation controls
MouseX += Input.GetAxisRaw ("Mouse X");
if (MouseXSet != MouseX)
{
transform.Rotate (new Vector3 (0,(MouseX - MouseXSet) * Sensitivity * 10, 0));
MouseXSet = MouseX;
}
// Jump controls
if (Input.GetKey (KeyCode.Space) && PlayerFoot < TerrainHeight + 1 && PlayerFoot < TerrainHeight + JumpHeight * 10)
{
TerrainHeightSet = TerrainHeight;
StartCoroutine (JumpDelay ());
}
if (Jumping == true)
{
rigidbody.velocity = new Vector3 (0, (Force * 10), 0);
}
if (Input.GetKeyUp (KeyCode.Space))
{
Jumping = false;
}
if (PlayerFoot >= TerrainHeightSet + JumpHeight * 10)
{
Jumping = false;
}
if (Jumping == false)
{
rigidbody.velocity = new Vector3 (0, -(Force * 20), 0);
}
// Keep player upright
if (PlayerFoot < TerrainHeight + 1)
{
transform.position += Vector3.up * (TerrainHeight - PlayerFoot);
}
}
// Check if player really wants to jump
IEnumerator JumpDelay () {
yield return new WaitForSeconds (0.05f);
if (Input.GetKey (KeyCode.Space))
{
Jumping = true;
}
StopCoroutine (JumpDelay ());
}
}
I forgot to mention, what it is doing is putting the camera on the ground, which made me think that the localscale / 2 part would help, but it didnt.
The transform.localPosition part. in the first script. it just puts the camera on the ground behind the characters feet
Answer by WillNode · Dec 29, 2014 at 07:34 AM
change your Camera localPosition
with this code :
float distance=4f;
void Update(){
transform.position = transform.rotation * new Vector3 (0.0f, 0.0f, -distance) + CameraTarget.transform.position;
}
by using this code you don't need to make parent to your camera.
Your answer
![](https://koobas.hobune.stream/wayback/20220613175043im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Parsing error called but can't figure it out. please help! 0 Answers
Why is iOS app size 10x larger than Android? 1 Answer
Soo wierd!!! Clamp doesnt work when moving and goes past clamp point! 1 Answer
Physics.OverlapSphere returns a strange "centerSphere" collider? 1 Answer
Input Scroll Wheel And Vertical Axis Work Together? 0 Answers