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
using UnityEngine;
using System.Collections;
public class Cameras : MonoBehaviour
{
public Transform ballObject, ballCamera;
public float speed, turnSpeed;
Vector3 forceDirection;
void FixedUpdate()
{
transform.position = ballObject.position;
if(Input.GetAxis("Vertical") != 0)
{
RollBall();
}
//calculate ball turning
transform.Rotate((Vector3.up * Input.GetAxis("Horizontal") * turnSpeed), Space.World);
}
void RollBall()
{
//set force down camera's look direction
forceDirection = ballCamera.transform.forward;
//remove y force direction for angled camera
forceDirection = new Vector3(forceDirection.x, 0, forceDirection.z);
//add force to ball
ballObject.GetComponent<Rigidbody>().AddForce(forceDirection.normalized * speed * (Input.GetAxis("Vertical")));
}
}
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 .
#pragma strict
var forward = 20;
var back = -20;
var left = 20;
var right = 20;
var maxVelocity : int = 100;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function FixedUpdate()
{
if(GetComponent.<Rigidbody>().velocity.sqrMagnitude > maxVelocity)//max speed
{
//0.5f is less smooth, 0.9999f is more smooth
GetComponent.<Rigidbody>().velocity *= 0.99f;
}
if (IsGrounded())
{
if (Input.GetKey("up"))
{
GetComponent.<Rigidbody>().AddForce(Vector3.forward * forward);
}
if (Input.GetKey("down"))
{
GetComponent.<Rigidbody>().AddForce(0,0,1 * back );
}
if (Input.GetKey("left"))
{
GetComponent.<Rigidbody>().AddForce(Vector3.left * left );
}
if (Input.GetKey("right"))
{
GetComponent.<Rigidbody>().AddForce(Vector3.right * right );
}
}
if (!IsGrounded())
{
if (Input.GetKey("up"))
{
GetComponent.<Rigidbody>().AddForce(Vector3.forward * forward / 2);
}
if (Input.GetKey("down"))
{
GetComponent.<Rigidbody>().AddForce(0,0,1 * back / 2 );
}
if (Input.GetKey("left"))
{
GetComponent.<Rigidbody>().AddForce(Vector3.left * left / 2 );
}
if (Input.GetKey("right"))
{
GetComponent.<Rigidbody>().AddForce(Vector3.right * right / 2 );
}
}
if(!Input.anyKey) // if you dont press any key then the ball slowly stops
{
GetComponent.<Rigidbody>().velocity = GetComponent.<Rigidbody>().velocity * 0.99;
GetComponent.<Rigidbody>().angularVelocity = GetComponent.<Rigidbody>().angularVelocity * 0.97;
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
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 !
