Search Unity

Bug How can I stop my ball from moving sideways when it turns?

Discussion in 'Scripting' started by mmgwold, Mar 1, 2023.

  1. mmgwold

    mmgwold

    Joined:
    Mar 1, 2023
    Posts:
    3
    I'm making a third person game where you roll a ball around, and have gotten some help in coding it. I'm not very good at it though, and I came across an issue where the ball you play as moves to the sides when you turn it. Here's the code.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class BallControl : MonoBehaviour
    6. {
    7.     public float speed = 10f;
    8.     public float rotationSpeed = 100f;
    9.     public Camera mainCamera;
    10.  
    11.     private Rigidbody rb;
    12.  
    13.     private void Start()
    14.     {
    15.         rb = GetComponent<Rigidbody>();
    16.     }
    17.  
    18.     private void FixedUpdate()
    19.     {
    20.         float horizontalInput = Input.GetAxis("Horizontal");
    21.         float verticalInput = Input.GetAxis("Vertical");
    22.  
    23.         Vector3 movement = mainCamera.transform.forward * verticalInput + mainCamera.transform.right * horizontalInput;
    24.         movement.y = 0f; // remove the vertical component
    25.         movement = Quaternion.AngleAxis(mainCamera.transform.rotation.eulerAngles.z, mainCamera.transform.forward) * movement;
    26.  
    27.         rb.AddForce(movement.normalized * speed);
    28.  
    29.         if (movement != Vector3.zero)
    30.         {
    31.             Quaternion targetRotation = Quaternion.LookRotation(movement);
    32.             Quaternion newRotation = Quaternion.RotateTowards(rb.rotation, targetRotation, rotationSpeed * Time.fixedDeltaTime);
    33.             rb.MoveRotation(newRotation);
    34.  
    35.             // Update the ball's rotation to match the camera's rotation
    36.             Vector3 cameraForward = mainCamera.transform.forward;
    37.             cameraForward.y = 0f;
    38.             Quaternion cameraRotation = Quaternion.LookRotation(cameraForward);
    39.             transform.rotation = Quaternion.Euler(0, mainCamera.transform.rotation.eulerAngles.y, 0);
    40.         }
    41.     }
    42. }
    Please help?
     
  2. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,647
    How is your camera controlled?
     
  3. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,404
  4. mmgwold

    mmgwold

    Joined:
    Mar 1, 2023
    Posts:
    3
    Sorry for taking a while to respond.
    The camera moves by being parented to an empty gameobject, which is parented to the ball. The gameobject then uses this script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CameraFollow : MonoBehaviour
    6. {
    7.     public Transform target;
    8.     public Vector3 offset;
    9.     public float rotationSpeed = 1f;
    10.  
    11.     private float currentRotationAngle = 0f;
    12.  
    13.     private void LateUpdate()
    14.     {
    15.         // Update the position of the target object to match the ball's position
    16.         transform.position = target.position + offset;
    17.  
    18.         // Rotate the camera around the ball based on A and D input
    19.         float rotationInput = Input.GetAxis("Horizontal");
    20.         currentRotationAngle += rotationInput * rotationSpeed;
    21.         Quaternion rotation = Quaternion.Euler(0f, currentRotationAngle, 0f);
    22.         transform.position = target.position + rotation * offset;
    23.  
    24.         // Ensure that the target object does not roll with the ball
    25.         transform.rotation = Quaternion.Euler(0f, currentRotationAngle, 0f);
    26.     }
    27. }
    28.  
     
  5. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,647
    You're using the same Horizontal Input for both of them. Just don't use it on the ball if you don't want it to move.
     
  6. mmgwold

    mmgwold

    Joined:
    Mar 1, 2023
    Posts:
    3
    Thanks, ball turns how I wanted it now :>
     
    Last edited: Mar 2, 2023