Camera Follow Behind A Rolling Ball
I have a player that consists of a standard Unity 3D sphere, with a rigidbody, and a robot/player that sits on top of that ball. The ball rolls and the player follows. The player on top rotates left or right and the ball moves in the direction that the player is facing. My problem is I don't know how to go about creating a camera that will stay behind the player above the ball, not jitter, and rotate with the player. I've tried numerous scripts with my own modifications and nothing seems to work. Whenever I try to lerp a rotation it just spins back and forth between 0 and 90 degrees on the y axis.
If it helps this is the movement script for my ball and the rotation script for the player on top.
moving.cs
using UnityEngine;
using System.Collections;
public class moving : MonoBehaviour {
private Rigidbody pRigid;
private float distToGround;
public Transform pivot;
public GameObject target;
public float speed = 10;
public float jumpForce = 10;
public float SLOPE_MULTIPLIER;
void Start()
{
pRigid = GetComponent<Rigidbody>();
distToGround = GetComponent<Collider>().bounds.extents.y;
}
void FixedUpdate()
{
int moveV = (int)Input.GetAxisRaw("Vertical");
int moveH = (int)Input.GetAxisRaw("Horizontal");
if (Input.GetAxis("Jump") > 0 && isGrounded())
{
pRigid.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
target.GetComponent<BodyController>().anim.SetTrigger("Jump");
}
RaycastHit hit;
if (Physics.Raycast(pivot.position, transform.forward, out hit, 1.0f))
{
if (Vector3.Dot(Vector3.up, hit.normal) > -0.5)
{
if (isGrounded())
{
if (moveV < 0)
pRigid.AddForce(target.transform.forward * speed * SLOPE_MULTIPLIER);
else if (moveV > 0)
pRigid.AddForce(-target.transform.forward * speed * SLOPE_MULTIPLIER);
else if (moveH < 0)
pRigid.AddForce(target.transform.right * speed * SLOPE_MULTIPLIER);
else if (moveH > 0)
pRigid.AddForce(-target.transform.right * speed * SLOPE_MULTIPLIER);
}
}
}
if (isGrounded())
{
if (moveV < 0)
pRigid.AddForce(target.transform.forward * speed);
else if (moveV > 0)
pRigid.AddForce(-target.transform.forward * speed);
else if (moveH < 0)
pRigid.AddForce(target.transform.right * speed);
else if (moveH > 0)
pRigid.AddForce(-target.transform.right * speed);
}
}
public bool isGrounded()
{
return Physics.Raycast(transform.position, -Vector3.up, distToGround + .05f);
}
}
PlayerRotation.cs
using UnityEngine;
using System.Collections;
public class PlayerRotation : MonoBehaviour
{
public Transform pivot;
void LateUpdate()
{
if (Input.GetAxisRaw("Turn") != 0)
{
transform.RotateAround(pivot.transform.position, Vector3.up, Input.GetAxisRaw("Turn"));
}
}
}
Answer by Golauszko · Nov 07, 2016 at 07:57 PM
So, to follow it will be:
public GameObject player
private Vector3 offset;
// Use this for initialization
void Start () {
offset = transform.position - player.transform.position;
}
void LateUpdate () {
transform.position = player.transform.position + offset;
}
And then try to set rotation of camera same as the player's.
I need the camera to stay behind the character, even when it turns. This is what I've got so far for a camera script even though it's broken:
using UnityEngine;
using System.Collections;
public class TPcam : $$anonymous$$onoBehaviour
{
public Transform target;
public float smoothing;
public float turnSpeed;
private Vector3 offset;
void Start()
{
offset = transform.position - target.position;
}
void LateUpdate()
{
transform.rotation = Quaternion.Lerp(transform.rotation, target.rotation, Time.deltaTime);
transform.position = Vector3.Lerp(transform.position, target.position + offset, Time.deltaTime * smoothing);
transform.Rotate(target.up, turnSpeed * Input.GetAxis("Turn"));
transform.LookAt(target);
}
}
Your answer
Follow this Question
Related Questions
How do i rotate third person camera around my character while holding down right mouse button? 0 Answers
Third Person Camera - Issues to clamp rotation 0 Answers
No Particle Rotation When Setting rotation3D from C# 0 Answers
Ambisonic rotation not working,Multichannel Ambisonics files not rotating 0 Answers