Hey,
i need your help.
I followed the Roll-a-ball Tutorial and took it up a notch, its going very well by now but i have 1 problem.
The camera only sees in one direction, i need the camera to see in that direction, the player is moving.
Here are my Camera and player scripts:
Camera
using UnityEngine;
using System.Collections;
public class CameraController : MonoBehaviour {
public GameObject player;
private Vector3 offset;
void Start()
{
offset = transform.position - player.transform.position;
}
void LateUpdate()
{
transform.position = player.transform.position + offset;
}
}
Player
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class PlayerMovement : MonoBehaviour {
public float speed;
public float maxspeed;
public int NeededPoints;
public Text countText;
public int points;
private Rigidbody rb;
private Vector3 camForward;
private Transform cam;
private Vector3 movement;
void Start()
{
rb = GetComponent<Rigidbody>();
points = 0;
SetCountText();
cam = Camera.main.transform;
}
void FixedUpdate()
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
camForward = Vector3.Scale(cam.forward, new Vector3(1, 0, 1)).normalized;
movement = (moveVertical * camForward + moveHorizontal * cam.right).normalized;
if(rb.velocity.magnitude <= maxspeed)
{
rb.AddForce(movement * speed);
}
Debug.Log(rb.velocity.magnitude);
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("green"))
{
other.gameObject.SetActive(false);
points = points + 5;
}
else if (other.gameObject.CompareTag("red"))
{
other.gameObject.SetActive(false);
points = points + 10;
}
else if (other.gameObject.CompareTag("blue"))
{
other.gameObject.SetActive(false);
points = points + 20;
}
SetCountText();
}
void SetCountText()
{
countText.text = "Points: " + points.ToString() + "/" + NeededPoints.ToString();
if (points >= NeededPoints)
{
}
}
}
I would think you could simply parent the Camera to the Player in the hierarchy and it would just automatically keep the Transform offsets, no? I’m not sure if that works on rotations or not, but if not, you could have your script simply alter the rotation of the camera to match that of the player, instead of worrying about positions.
Do a quick Youtube search for “Chase Cam” or “Follow Cam” tutorials if you want to see some camera setup scripts though.
I haven’t watched it, but it sounds like what you want here:
If you would make the Camera a part of the Sphere the Camera would go in Circles around the ball and you arent able to see anything.
The video don´t show anything that would help me because he used it as a 2D camera and i already have that.
Oh, I thought you were wanting the camera to spin around behind the ball like a shoulder cam.
Ok, consider trying this, maybe…
Set an empty gameobject as a child to the Ball, place it in front of your ball or wherever works best. Then, set your camera script to do a transform.LookAt(objectName) on the empty gameobject. That’s a really simple way of getting the camera to face where you want. You could also do that LookAt on the ball, but I’m assuming you want a better view than just staring at the ball, the empty gameobject lets you direct the camera to a distance/height of your choice by simply moving the empty around.
Another option, if the ball is using physics you should be able to replace the ball’s transform.forward with rigidbody.velocity.normalized and get the correct result. Just remember not to update the camera direction when velocity is zero.
I don’t know if it’s helpful, because I’m not 100% sure how it works as I’ve never used it. But similar to robin’s comment on Rigidbody, you might also check into using bounds/bounding boxes and the collider as they may be a bit more static and not affected by rotation(not certain).