- Home /
Why the control with gyroscope inverted when I make 180 degrees turn?
Dear all
Newbie here...I've been developing a clone of Super Monkey ball which you controlled a sphere with character inside with gyroscope. I make the camera always sits behind the character with below script. However every time I make 180 degrees turn, the left, right, up, and down control becomes inverted & if I make another 180 degrees turn, the control will come back normal. Need help how to solve this issue. Thanks.
My CameraFollow script:
using UnityEngine;
using System.Collections;
public class CameraFollow : MonoBehaviour {
public GameObject target;
public float speed = 5;
Vector3 offset;
void Start()
{
offset = target.transform.position - transform.position;
}
void LateUpdate()
{
// Look
var newRotation = Quaternion.LookRotation(target.transform.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, speed * Time.deltaTime);
// Move
Vector3 newPosition = target.transform.position - target.transform.forward * offset.z - target.transform.up * offset.y;
transform.position = Vector3.Slerp(transform.position, newPosition, Time.deltaTime * speed);
}
}
Answer by frilanski · May 22, 2020 at 05:06 PM
I'm not sure I follow what's wrong but looking at your script you're moving your camera independently to your ball and hoping the ball stays in frame. The two appear to be disconnected. I wrote this script for a car game to make the camera soft follow the car:
void Update()
{
transform.LookAt(Car);
transform.position = Vector3.SmoothDamp(transform.position, target.position, ref velocity, SmoothTime);
}
'Car' is a public transform which appears in unity. This means you can assign the car (or in your case the ball) to the variable, so in this script 'Car' is the position of the car. 'target' is another transform which is an empty I attached to the car which is a point behind it where I want the camera to sit. In your case the ball is rotating so this point would rotate with it. So if I were you I would make this a global point behind the ball. just take the transform in the 'Car' variable and modify it to get a point behind the ball where you want the camera to sit.
Hi @frilanski, Thank you for your answer. However I'm still experiencing the same issue after adopting your solution.
Is this actually your camera script that is the issue then? Is it not your ball movement? It could be if your ball move is adding force in the balls local 'forward' etc. then that forward direction stays relative to the ball and when upside down, forward is backwards.
If that's the issue, try adding torque ins$$anonymous$$d of force to make the ball roll ins$$anonymous$$d of pushing the ball.
Hi @frilanski, yeah I think it's because of my ball movement. But still can get it solved as I wanted untill now. Shared to you my ball controller script:
public void FixedUpdate()
{
Vector3 fromCameraTo$$anonymous$$e = transform.position - mainCamera.transform.position;
fromCameraTo$$anonymous$$e.y = 0;
fromCameraTo$$anonymous$$e.Normalize();
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = (fromCameraTo$$anonymous$$e * moveVertical + mainCamera.transform.right *
moveHorizontal) * accelFactor * 2;
rb.AddForce(movement * accelFactor * 2);
}