Search Unity

Problem with my ball game controller/camera

Discussion in 'Scripting' started by i3artyy2222, Oct 6, 2015.

  1. i3artyy2222

    i3artyy2222

    Joined:
    Aug 18, 2013
    Posts:
    274
    Hi all, recently ive been working on my ball game but im stack at this point.
    First the ball could move [left,forward,back,right] and the script i had was SmoothFollow to follow the ball.
    The SmoothFollow script didnt worked as i wanted as i wanted the camera to rotate while the player changes direction [Example : If you roll ball forward and camera is behind, you go left and in smooth follow the camera didnt rotate as the ball went left it just kept staying in same rotation, looked like 2d type of game. What i wanted is when you go forward camera is behind but when you turn left the camera rotates right so its always behind the ball and follows it .]
    So i did it found script that does it
    Code (JavaScript):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Cameras : MonoBehaviour
    5. {
    6.     public Transform ballObject, ballCamera;
    7.     public float speed, turnSpeed;
    8.     Vector3 forceDirection;
    9.     void FixedUpdate()
    10.     {
    11.         transform.position = ballObject.position;
    12.         if(Input.GetAxis("Vertical") != 0)
    13.         {
    14.             RollBall();
    15.         }
    16.         //calculate ball turning
    17.         transform.Rotate((Vector3.up * Input.GetAxis("Horizontal") * turnSpeed), Space.World);
    18.     }
    19.  
    20.     void RollBall()
    21.     {
    22.         //set force down camera's look direction
    23.         forceDirection = ballCamera.transform.forward;
    24.      
    25.         //remove y force direction for angled camera
    26.         forceDirection = new Vector3(forceDirection.x, 0, forceDirection.z);
    27.      
    28.         //add force to ball
    29.         ballObject.GetComponent<Rigidbody>().AddForce(forceDirection.normalized * speed * (Input.GetAxis("Vertical")));
    30.     }
    31. }
    But 1st problem is that, i dont know how to set height and distance of camera because when i play the game its like right behind ball.
    2nd biggest problem is that when i press left the camera rotates right so its behind the ball thats as i want but the controls get messy because now when i go left side the camera rotates but the ball behaves like it controls on world axis, north,east,south,west no matter where the camera is facing. So for example i go left and in world it means im going south lets say but when i press forward to go as camera shows it goes right on north. Sorry i dont know how else to explain this, thats best as i could.

    Here is my ball movement script .
    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var forward = 20;
    4. var back = -20;
    5. var left = 20;
    6. var right = 20;
    7.  
    8. var maxVelocity : int = 100;
    9.  
    10. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    11. function FixedUpdate()
    12. {
    13. if(GetComponent.<Rigidbody>().velocity.sqrMagnitude > maxVelocity)//max speed
    14.      {
    15.             //0.5f is less smooth, 0.9999f is more smooth
    16.              GetComponent.<Rigidbody>().velocity *= 0.99f;
    17.      }
    18. if (IsGrounded())
    19. {
    20. if (Input.GetKey("up"))
    21. {
    22. GetComponent.<Rigidbody>().AddForce(Vector3.forward * forward);
    23. }
    24. if (Input.GetKey("down"))
    25. {
    26. GetComponent.<Rigidbody>().AddForce(0,0,1 * back );
    27. }
    28. if (Input.GetKey("left"))
    29. {
    30. GetComponent.<Rigidbody>().AddForce(Vector3.left * left );
    31. }
    32. if (Input.GetKey("right"))
    33. {
    34. GetComponent.<Rigidbody>().AddForce(Vector3.right * right );
    35. }
    36. }
    37. if (!IsGrounded())
    38. {
    39. if (Input.GetKey("up"))
    40. {
    41. GetComponent.<Rigidbody>().AddForce(Vector3.forward * forward / 2);
    42. }
    43. if (Input.GetKey("down"))
    44. {
    45. GetComponent.<Rigidbody>().AddForce(0,0,1 * back / 2 );                  
    46. }
    47. if (Input.GetKey("left"))
    48. {
    49. GetComponent.<Rigidbody>().AddForce(Vector3.left * left / 2 );
    50. }
    51. if (Input.GetKey("right"))
    52. {
    53. GetComponent.<Rigidbody>().AddForce(Vector3.right * right / 2 );
    54. }
    55. }
    56.  
    57. if(!Input.anyKey) // if you dont press any key then the ball slowly stops
    58. {
    59.    GetComponent.<Rigidbody>().velocity = GetComponent.<Rigidbody>().velocity * 0.99;
    60.     GetComponent.<Rigidbody>().angularVelocity = GetComponent.<Rigidbody>().angularVelocity * 0.97;
    61. }
    62. }
    63. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    64.  
    65.  
    66.  
    In the attached files there is simple drawing to help understand my problem better. Tell me guys if it will be really hard to make it or not cuz i cant imagine other way as interracting with the camera script inside ball script or otherway around for example like if i press right camera rotates left and the direction changes or something.

    Thanks for any help !
     

    Attached Files:

  2. Craig8726

    Craig8726

    Joined:
    Jul 5, 2013
    Posts:
    79
    you could use a empty game object as a camera anchor that follows the position and rotation of the player. put the camera as a child of the anchor so it follows the movement of the anchor but maintains whatever distance it originally has from the anchor.
    you can use Mathf.Lerp for a smooth follow and Quaternion.Slerp for a smooth rotation.
     
  3. i3artyy2222

    i3artyy2222

    Joined:
    Aug 18, 2013
    Posts:
    274
    but as i said the other problem is that if the camera follows ball and its facing left and i press forward to go that left side it goes north in the world map instead left where its facing [see the attachment picture]
     
  4. Craig8726

    Craig8726

    Joined:
    Jul 5, 2013
    Posts:
    79
    try rigidBody.AddRelativeForce instead of rigidBody.AddForce to move in the direction the ball is facing instead of its world Position
     
  5. i3artyy2222

    i3artyy2222

    Joined:
    Aug 18, 2013
    Posts:
    274
    I tried and it just makes the ball bugged out like doesnt roll for long roll a bit and if i keep pressing up arrow it just go in circles
     
  6. Craig8726

    Craig8726

    Joined:
    Jul 5, 2013
    Posts:
    79
    you need to simplify your code. there is too much going on. unity physics can handle most the work you just need to addForce, you dont need to slow the ball down because drag will do it for you. just increase or decrease the drag in the inspector to slow the ball when there is no input.
    try this for some basic movement.

    Code (CSharp):
    1. public float speed = 10;
    2.     public float turnSpeed = 5;
    3.     void FixedUpdate()
    4.     {
    5.         float s = Input.GetAxis("Horizontal");
    6.         float f = Input.GetAxis("Vertical");
    7.         Vector3 moveForward = new Vector3 (s,0,f);
    8.         Vector3 turn = new Vector3 (0, s, 0);
    9.         rigidBody.AddRelativeForce (moveForward * speed * Time.deltaTime);
    10.         rigidBody.AddTorque (turn * turnSpeed * Time.deltaTime);
    11.     }
    to increase speed you can modifiy the floats in the inspector. If you get jittery movement use interpolate in the rigidbody inspector.y may need to freeze rotation in the z axis on the rigid body in the inspector to maintain orientation.
     
    Last edited: Oct 8, 2015
  7. i3artyy2222

    i3artyy2222

    Joined:
    Aug 18, 2013
    Posts:
    274
    Ok thanks for that :) [ Im using slow down line because i have many different ball types to chose from and for example some need to slide and have 0 drag or so ever so when you accelerate and leave it will just constantly go with that speed