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();
distToGround = GetComponent().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().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"));
}
}
}
↧