How to make a camera following an object in unity 3D C #?

I have a Ball object, and I added interactivity to it (WASD to move the ball) I need a camera to stay behind and follow the ball, but I get errors.

using UnityEngine; using System.Collections; public class ballmain : MonoBehaviour { public bool isMoving = false; public string direction; public float camX; public float camY; public float camZ; // Use this for initialization void Start () { Debug.Log("Can this run!!!"); } // Update is called once per frame void Update () { camX = rigidbody.transform.position.x -=10; camY = rigidbody.transform.position.y -=10; camZ = rigidbody.transform.position.z; camera.transform.position = new Vector3(camX, camY, camZ); //followed by code that makes ball move } } 

I get the error message "Assets / ballmain.cs (18,44): error CS1612: cannot change the return value of the value type" UnityEngine.Transform.position ". Consider saving the value in the temporary variable" Does anyone know the answer? If I comment on the camera code, the ball can move.

+4
source share
6 answers

here you go. full code.

Simple After

 using UnityEngine; using System.Collections; public class Follow: MonoBehaviour { public Transform target; public float smooth= 5.0f; void Update (){ transform.position = Vector3.Lerp ( transform.position, target.position, Time.deltaTime * smooth); } } 

Advanced Search

 using UnityEngine; using System.Collections; public class SmoothFollowScript: MonoBehaviour { // The target we are following public Transform target; // The distance in the xz plane to the target public int distance = 10.0; // the height we want the camera to be above the target public int height = 10.0; // How much we public heightDamping = 2.0; public rotationDamping = 0.6; void LateUpdate (){ // Early out if we don't have a target if (TargetScript.russ == true){ if (!target) return; // Calculate the current rotation angles wantedRotationAngle = target.eulerAngles.y; wantedHeight = target.position.y + height; currentRotationAngle = transform.eulerAngles.y; currentHeight = transform.position.y; // Damp the rotation around the y-axis currentRotationAngle = Mathf.LerpAngle (currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime); // Damp the height currentHeight = Mathf.Lerp (currentHeight, wantedHeight, heightDamping * Time.deltaTime); // Convert the angle into a rotation currentRotation = Quaternion.Euler (0, currentRotationAngle, 0); // Set the position of the camera on the xz plane to: // distance meters behind the target transform.position = target.position; transform.position -= currentRotation * Vector3.forward * distance; // Set the height of the camera transform.position.y = currentHeight; // Always look at the target transform.LookAt (target); } } } 
+4
source

If you just want to follow the target, align the camera position the way you want and make the camera a child of the target, and the rest will do

+2
source

Include a standard mobile asset in your project. It contains the SmoothFollow2D.js code in the script section. Attach this code to the game object and initialize the public variables. It just does the job for you.

+1
source

I found this simple and useful single 2-dimensional camera following the script.

 using UnityEngine; using System.Collections; public class FollowCamera : MonoBehaviour { public float interpVelocity; public float minDistance; public float followDistance; public GameObject target; public Vector3 offset; Vector3 targetPos; // Use this for initialization void Start () { targetPos = transform.position; } // Update is called once per frame void FixedUpdate () { if (target) { Vector3 posNoZ = transform.position; posNoZ.z = target.transform.position.z; Vector3 targetDirection = (target.transform.position - posNoZ); interpVelocity = targetDirection.magnitude * 5f; targetPos = transform.position + (targetDirection.normalized * interpVelocity * Time.deltaTime); transform.position = Vector3.Lerp( transform.position, targetPos + offset, 0.25f); } } } 

source unit2d camera script

+1
source

-= in these lines:

  camX = rigidbody.transform.position.x -=10; camY = rigidbody.transform.position.y -=10; 

wrong. -= will try to change rigidbody.transform.position . You just want - .

However, be that as it may, the camera will not track changes in the target position Z and will not be properly tracked if the camera is rotated. To get the desired position, you need (in vectors): -

 cam_pos = target_pos - dist_to_target * cam_look_at 
0
source

Here is one of the script that I found useful during the development of my game. I have not created them, so I give credit to wiki.unity3d.com for providing this amazing script.

Smooth:

 using UnityEngine; using System.Collections; public class SmoothFollow2 : MonoBehaviour { public 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 Update () { 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); } } 

Additional information about my work

0
source

Source: https://habr.com/ru/post/1414343/


All Articles