- Home /
Set Camera Target Error ..
Hi, im new to programming and to Unity. i created a Gamepad control and a script for the player and for the camera.
In the camera script i try to set the target of the camera but a error appears.
UnityEngine.CharacterController can not converted into Spieler. Of course i know its a mistake between the types. Spieler is the players class.
The mistake appears in the camera script at the method "SetCameraTarget" at the if-statement...
But i dont know how to fix it. Here the script of my player and my camera. it would be awesome if someone would know how to fix the mistake :)
public class Spieler : MonoBehaviour
{
// Deadzone
public float inputDelay = 0.1f;
// Bewegungsgeschwindigkeit
public float forwardVel = 12;
// Geschwindigkeit beim Umdrehen
public float rotateVel = 100;
// Hält die Position für die nächste Rotation
private Quaternion targetRotation;
// Rigidbody
private Rigidbody rBody;
// Input Bewegung
private float forwardInput;
// Input Drehung
private float turnInput;
public Quaternion TargetRotation
{
get
{
return targetRotation;
}
}
private void Start()
{
targetRotation = transform.rotation;
if (GetComponent<Rigidbody>())
rBody = GetComponent<Rigidbody>();
forwardInput = turnInput = 0;
}
private void GetInput()
{
forwardInput = Input.GetAxis("LeftJoystickVertical");
turnInput = Input.GetAxis("LeftJoystickHorizontal");
}
private void Update()
{
GetInput();
Turn();
}
private void FixedUpdate()
{
Run();
}
private void Run()
{
if (Mathf.Abs(forwardInput) > inputDelay)
{
// Bewegen
rBody.velocity = transform.forward * forwardInput * forwardVel;
}
else
{
// 0 Velocity
rBody.velocity = Vector3.zero;
}
}
private void Turn()
{
if (Mathf.Abs(turnInput) > inputDelay)
{
// Faktor lässt den Spieler flüssig drehen
targetRotation *= Quaternion.AngleAxis(rotateVel * turnInput * Time.deltaTime, Vector3.up);
}
transform.rotation = targetRotation;
}
}
public class Kamera : MonoBehaviour
{
// Ziel
public Transform target;
//
public float lookSmooth = 0.09f;
// Kameraentfernung vom Ziel
public Vector3 offSetFromTarget = new Vector3(0, 6, 8);
// Einschränkung der Kamera auf der X-Achse
public float xTilt = 10;
private Vector3 destination = Vector3.zero;
// Referenz zum Spieler
private Spieler charController;
private float rotateVel = 0;
private void Start()
{
SetCameraTarget(target);
}
private void SetCameraTarget(Transform t)
{
target = t;
if (target != null)
{
if (target.GetComponent<CharacterController>())
charController = target.GetComponent<CharacterController>();
}
}
private void LateUpdate()
{
// Bewegen
MoveToTarget();
// Rotieren
LookAtTarget();
}
private void MoveToTarget()
{
// Kamera bewegen
destination = charController.TargetRotation * offSetFromTarget;
destination += target.position;
transform.position = destination;
}
private void LookAtTarget()
{
// Kamera rotieren
float eulerYAngle = Mathf.SmoothDampAngle(transform.eulerAngles.y, target.eulerAngles.y, ref rotateVel, lookSmooth);
transform.rotation = Quaternion.Euler(transform.eulerAngles.x, eulerYAngle, 0);
}
}
What is your problem? You recognise that you can't convert CharacterController to Spieler, so why are you doing it? Why is charController of the type Spieler? Please explain.
Your answer
Follow this Question
Related Questions
How to enable a childs camera 5 Answers
Changing camera target of CarSmoothFollow ( Js to c#) 0 Answers
Show a target above the player 0 Answers