Question by
Bananik_007 · Sep 18, 2016 at 06:04 PM ·
scripting problemerrorstringfind
ERROR 15,36 An object reference is required to access non-static members UnityEngine.Transform.Find(string)
Script:
using UnityEngine;
using System.Collections;
public class CarCamera2 : MonoBehaviour {
private Transform target;
public float distance = 3.0f;
public float height = 3.0f;
public float damping = 5.0f;
public bool smoothRotation = true;
public bool followBehind = true;
public float rotationDamping = 10.0f;
void Start ()
{
target = Transform.Find("CameraTarget");
}
void FixedUpdate () {
Vector3 wantedPosition;
if(followBehind)
wantedPosition = target.TransformPoint(0, height, -distance);
else
wantedPosition = target.TransformPoint(0, height, distance);
transform.position = Vector3.Lerp (transform.position, wantedPosition, Time.deltaTime * damping);
if (smoothRotation) {
Quaternion wantedRotation = Quaternion.LookRotation(target.position - transform.position, target.up);
transform.rotation = Quaternion.Slerp (transform.rotation, wantedRotation, Time.deltaTime * rotationDamping);
}
else transform.LookAt (target, target.up);
}
}
Comment
In the future when posting code please use the 101/010 button above to correctly format your script (lieave an empty line between text and code). For more information on creating post please see the user guide and FAQ.
I did this for you this time.
Best Answer
Answer by TBruce · Sep 18, 2016 at 06:19 PM
This will correct the error
using UnityEngine;
using System.Collections;
public class CarCamera2 : MonoBehaviour
{
private Transform target = null;
public float distance = 3.0f;
public float height = 3.0f;
public float damping = 5.0f;
public bool smoothRotation = true;
public bool followBehind = true;
public float rotationDamping = 10.0f;
void Start ()
{
if (GameObject.Find("CameraTarget"))
{
target = GameObject.Find("CameraTarget").transform;
}
}
void FixedUpdate ()
{
if (target != null)
{
Vector3 wantedPosition;
if(followBehind)
wantedPosition = target.TransformPoint(0, height, -distance);
else
wantedPosition = target.TransformPoint(0, height, distance);
transform.position = Vector3.Lerp (transform.position, wantedPosition, Time.deltaTime * damping);
if (smoothRotation)
{
Quaternion wantedRotation = Quaternion.LookRotation(target.position - transform.position, target.up);
transform.rotation = Quaternion.Slerp (transform.rotation, wantedRotation, Time.deltaTime * rotationDamping);
}
else
transform.LookAt (target, target.up);
}
}
}
Your answer
Follow this Question
Related Questions
(31,16): error CS1525: Unexpected symbol `(', expecting `)', `,', `;', `[', or `=' 2 Answers
PostProcessingFactory.cs 2 Answers
Missing ',' when there is no need? 0 Answers
parsing error 1 Answer