Search Unity

When moving backwards, my sphere flies upwards instead of straight backwards.

Discussion in 'Scripting' started by poolofclay33, Mar 23, 2018.

  1. poolofclay33

    poolofclay33

    Joined:
    Oct 4, 2016
    Posts:
    51
    I'm trying to make a Super Monkey Ball style movement for my ball so when you change the direction of the camera, the player's position changes to that camera's direction. When I move forward, the camera tilts towards the forward position and the ball moves forward. However, when I move the ball backwards, the ball flies straight up into the air at a 45 degree angle (so not exactly straight up).

    Here's an attachment of my code for the camera:

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class CameraController : MonoBehaviour
    6. {
    7.  
    8.     private GameObject player;
    9.     private Vector3 offset;
    10.     public float boardTiltMax = 15f; // Maximum angle to tilt the camera to fake the level tilting
    11.     private Vector3 desiredPosition;
    12.     private GameObject desiredPositionObject;
    13.  
    14.     private float rotationDamping = 10f;
    15.     public float movementDamping = 150f;
    16.     private float turnSpeed = 300f;
    17.  
    18.     private float turnAngle = 0.0f;
    19.  
    20.     // Use this for initialization
    21.     void Start()
    22.     {
    23.         offset = transform.position;
    24.         desiredPosition = transform.position;
    25.         desiredPositionObject = new GameObject("cameraFollow");
    26.         DontDestroyOnLoad(desiredPositionObject);
    27.         desiredPositionObject.transform.position = transform.position;
    28.  
    29.         // fine the player object
    30.         player = GameObject.Find("Player");
    31.  
    32.         if (player == null)
    33.         {
    34.  
    35.             Debug.LogError("Could not find object \"Player\" ! Aborting GameCamera load.");
    36.             DestroyImmediate(gameObject);
    37.         }
    38.     }
    39.  
    40.     void Update()
    41.     {
    42.         // Rotate the camera around the ball to adjust movement when Q or E are pressed (left/right movement)
    43.         turnAngle += Input.GetAxis("Horizontal") * turnSpeed * Time.deltaTime;
    44.     }
    45.  
    46.     void LateUpdate()
    47.     {
    48.         // find the ZX direction from the player to the camera
    49.         var heading = transform.position - player.transform.position;
    50.         heading.y = 0f;
    51.         var distance = heading.magnitude;
    52.         var direction = heading / distance;
    53.  
    54.         // Find the right vector for the diretion
    55.         var rotationVectorRight = Vector3.Cross(direction, Vector3.up);
    56.  
    57.         // Move the camera
    58.         desiredPositionObject.transform.position = player.transform.position + offset;
    59.  
    60.         // Rotate around the players Y axis by the turn value
    61.         desiredPositionObject.transform.RotateAround(player.transform.position, Vector3.up, turnAngle);
    62.  
    63.         // Deal with forward/backward board rotation
    64.         desiredPositionObject.transform.RotateAround(player.transform.position, rotationVectorRight, -Input.GetAxisRaw("Vertical") * boardTiltMax);
    65.  
    66.         // Ensure we're looking at the player before the roll rotation is applied
    67.         desiredPositionObject.transform.LookAt(player.transform.position);
    68.  
    69.         // Apply the roll rotation
    70.         desiredPositionObject.transform.RotateAround(desiredPositionObject.transform.position, direction, -Input.GetAxisRaw("Horizontal") * boardTiltMax);
    71.  
    72.         // Lerp the cameras position to match the target object
    73.         transform.position = Vector3.Slerp(transform.position, desiredPositionObject.transform.position, Time.deltaTime * movementDamping);
    74.  
    75.         // Lerp the cameras rotation to match the target object
    76.         transform.rotation = Quaternion.Lerp(transform.rotation,
    77.             desiredPositionObject.transform.rotation,
    78.             Time.deltaTime * rotationDamping);
    79.  
    80.         // Re-center the camera on the object to account for new roll rotation
    81.         CenterCameraOnTarget();
    82.  
    83.     }
    84.  
    85.     private void CenterCameraOnTarget()
    86.     {
    87.         Plane plane = new Plane(transform.forward, player.transform.position);
    88.         Ray ray = GetComponent<Camera>().ScreenPointToRay(new Vector3(Screen.width / 2, Screen.height / 2, 0.0f));
    89.         float distance;
    90.         plane.Raycast(ray, out distance);
    91.  
    92.         var point = ray.GetPoint(distance);
    93.         var offset = player.transform.position - point;
    94.         transform.position += offset;
    95.     }
    96. }
    97.  
    Code for the simple movement script:

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class PlayerController : MonoBehaviour {
    6.  
    7.     public float speed;
    8.  
    9.     public GameObject camera;
    10.  
    11.     private Rigidbody rb;
    12.  
    13.     void Start()
    14.     {
    15.         rb = GetComponent<Rigidbody> ();
    16.     }
    17.  
    18.     void FixedUpdate()
    19.     {
    20.         float movementHorizontal = Input.GetAxis("Horizontal");
    21.         float movementVertical = Input.GetAxis("Vertical");
    22.  
    23.         Vector3 movementVector = new Vector3(movementHorizontal, 0.0f, movementVertical);
    24.         movementVector = camera.transform.TransformDirection(movementVector);
    25.  
    26.         //GetComponent<Rigidbody>().AddForce(movementVector * speed * Time.deltaTime);
    27.  
    28.         rb.AddForce (movementVector * speed);
    29.     }
    30. }
    31.  
     
    Last edited by a moderator: Mar 27, 2018
  2. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,196
    Just edit your post and put code tags around it. More people will read it if you do.
     
    poolofclay33 likes this.