- Home /
Camera collision while using accelerometer
Hello I am very new to unity and coding and need someones help! I am currently making a 3rd person game with a ball and a floating head on top of the ball. The head rotates based on the direction that the ball is moving in. The camera follows the rotation of the head so then the camera is always facing the direction that the ball is rolling in. The issue that I have is the camera will go through walls and i do not want this to happen. I need some sort of camera collision script but I have not been able to find on anywhere that uses the accelerometer. If someone could please help me figure this out that would be great! I think I need a ray cast script on the camera but I am not 100% sure. Thanks!
Ball Roll script: {
public float moveSpeed = 10;
public bool isFlat = true;
private Quaternion localRotation; //
public float speed = 1.0f; // ajustable speed from Inspector in Unity editor
// Use this for initialization
void Start()
{
// copy the rotation of the object itself into a buffer
localRotation = transform.rotation;
}
void Update() // runs 60 fps or so
{
// find speed based on delta
float curSpeed = Time.deltaTime * speed;
// first update the current rotation angles with input from acceleration axis
localRotation.y += Input.acceleration.x * curSpeed;
// then rotate this object accordingly to the new angle
transform.rotation = localRotation;
Vector3 tilt = Input.acceleration;
if (isFlat)
transform.Translate(0,0,-Input.acceleration.z);
}
}
Head rotate script: { public Transform ballObject; public float distance = 0.1f; // min distance to refresh view direction
Vector3 lastPos;
Vector3 viewDir;
void Start()
{
// ensure forward direction at start
lastPos = ballObject.transform.position - transform.forward * distance;
}
void Update()
{
Vector3 ballPos = ballObject.transform.position;
Vector3 newDir = ballPos - lastPos; // direction from last position
newDir.y = 0; // keep the camera on horizontal plane
if (newDir.magnitude > distance)
{ // only recalculate after min distance
viewDir = newDir;
lastPos = ballPos;
}
transform.position = ballPos;
transform.forward = Vector3.Slerp(transform.forward, viewDir.normalized, Time.deltaTime);
}
} Camera follow head script: {
[SerializeField]
private Transform target;
[SerializeField]
private Vector3 offsetPosition;
[SerializeField]
private Space offsetPositionSpace = Space.Self;
[SerializeField]
private bool lookAt = true;
private void LateUpdate()
{
Refresh();
}
public void Refresh()
{
if (target == null)
{
Debug.LogWarning("Missing target ref !", this);
return;
}
// compute position
if (offsetPositionSpace == Space.Self)
{
transform.position = target.TransformPoint(offsetPosition);
}
else
{
transform.position = target.position + offsetPosition;
}
// compute rotation
if (lookAt)
{
transform.LookAt(target);
}
else
{
transform.rotation = target.rotation;
}
}
}
Answer by UnityCoach · Oct 01, 2018 at 01:41 PM
You want to use Cinemachine.
Yup, volume confinement and collision avoidance.