Search Unity

Question "Super MonkeyBall" inspired final assignment

Discussion in 'Physics' started by lalopuchet, May 25, 2020.

  1. lalopuchet

    lalopuchet

    Joined:
    May 22, 2020
    Posts:
    1
    So as the title says, I'm making a 3D Super Monkeyball inspired game as my final assignment for school, I'm learning everything I can from youtube and forums but I still have very specific questions, hope I can get through before my due date. My main problem is the following:
    I'm trying to control my player and make the camera rotation relevant to the player movement.. for example, I'm moving forward and I rotate my camera to the left and instead of the ball turning to the left it keeps moving forward, same thing if I rotate the camera to the front, when I move forward it looks like I'm moving backwards...

    My movement script is very very simple
    public class Sc2 : MonoBehaviour
    {
    [SerializeField]
    Vector3 v3Fuerza;
    [SerializeField]
    KeyCode keyPositivo;
    [SerializeField]
    KeyCode keyNegativo;

    void Start()
    {

    }

    void FixedUpdate()
    {
    if (Input.GetKey(keyPositivo))
    GetComponent<Rigidbody>().velocity += v3Fuerza;

    if (Input.GetKey(keyNegativo))
    GetComponent<Rigidbody>().velocity -= v3Fuerza;

    }
    }
    (I copied this same script for the left and right movement)

    And the rotation script that is attached to the main camera is
    public class PlayerFollow : MonoBehaviour
    {
    public Transform PlayerTransform;

    private Vector3 _cameraOffset;

    [Range(0.01f, 1.0f)]
    public float SmoothFactor = 0.5f;

    public bool LookAtPlayer = false;

    public bool RotateAroundPlayer = true;

    public float RotationsSpeed = 5.0f;

    void Start()
    {
    _cameraOffset = transform.position - PlayerTransform.position;
    }

    void LateUpdate()
    {
    if(RotateAroundPlayer)
    {
    Quaternion camTurnAngle =
    Quaternion.AngleAxis(Input.GetAxis("Mouse X") * RotationsSpeed, Vector3.up);

    _cameraOffset = camTurnAngle * _cameraOffset;
    }
    Vector3 newPos = PlayerTransform.position + _cameraOffset;

    transform.position = Vector3.Slerp(transform.position, newPos, SmoothFactor);

    if (LookAtPlayer || RotateAroundPlayer)
    transform.LookAt(PlayerTransform);
    }
    }

    So, if there's a solution for this with what I have already that would be ideal but I am open to learning and any other recommendations, thanks in advance.