1
\$\begingroup\$

I have a ball, that has been moved via addForce. It colides in the scene with objects, leading to rotation in all axes.

I want a camera to follow this ball but from the side (direction ortogonal to the move vector and UP vector). No matter what I do, I cannot get this working. The closest I get is almost correct, but camera "bumps" during frames.

I have used this in camera script to attach camera to the ball and move it with it:

this.transform.LookAt(this.cannonBall.transform.position, Vector3.up);

this.transform.position = ball.transform.position;

No I need to rotate camera 90 degree aroun up axis and move it a little backward to track ball from the side.

How can I achive this? If I use

this.transform.Rotate(Vector3.up, 90, Space.World);

It mess up lookAt

\$\endgroup\$
1
\$\begingroup\$

Try something like:

public GameObject ball;
public float distanceBack = 6;

private void LateUpdate() {
    //Manually change to the starting angle you want here:
    transform.rotation = Quaternion.identity;

    transform.position = ball.transform.position - transform.TransformDirection(Vector3.back * distanceBack);
    transform.LookAt(ball.transform);
}
\$\endgroup\$
  • \$\begingroup\$ Thank you for the tip with reseting camera rotation to identity. That does the trick. \$\endgroup\$ – Martin Perry Sep 26 '15 at 14:59
2
\$\begingroup\$

You can add this script to the camera, and plug in the ball as the target to be followed. Then just set your offsets.

FYI, I wrote this script in Unity 4.x, so it will ask you if it's all good, so just say yes and it will auto-update.

using UnityEngine;
using System.Collections;

public class TargetFollower : MonoBehaviour
{

    [Tooltip("Target to be followed")]
    public Transform Target;
    [Tooltip("Mimic the Target's changes in x coordinate")]
    public bool FollowTargetX = true;
    [Tooltip("Mimic the Target's changes in y coordinate")]
    public bool FollowTargetY = true;
    [Tooltip("Mimic the Target's changes in z coordinate")]
    public bool FollowTargetZ = true;

    // Update is called once per frame
    void FixedUpdate()
    {
        if (Target)
        {
            Vector3 targetMovement = GetTargetMovement();
            UpdatePosition(targetMovement);
        }
    }

    private Vector3 oldTargetPosition;
    private Vector3 GetTargetMovement()
    {
        if (oldTargetPosition == Vector3.zero)
        {
            oldTargetPosition = Target.transform.position;
        }
        Vector3 newTargetPosition = Target.transform.position;
        Vector3 targetMovement = newTargetPosition - oldTargetPosition;
        oldTargetPosition = new Vector3(newTargetPosition.x, newTargetPosition.y, newTargetPosition.z);
        return targetMovement;
    }

    private void UpdatePosition(Vector3 targetMovement)
    {
        float xPosition = transform.position.x;
        float yPosition = transform.position.y;
        float zPosition = transform.position.z;
        if (FollowTargetX)
        {
            xPosition += targetMovement.x;
        }
        if (FollowTargetY)
        {
            yPosition += targetMovement.y;
        }
        if (FollowTargetZ)
        {
            zPosition += targetMovement.z;
        }
        Vector3 updatedPosition = new Vector3(xPosition, yPosition, zPosition);
        transform.position = updatedPosition;
    }
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.