I’m quite new to Unity, and i’m tryng to make a camera following a marble.
I have a sphere with a rigid body attached to it.
Attached to the sphere, i have the following script :
function Update ()
{
var x = Input.GetAxis("Horizontal") * Time.deltaTime * speed;
var z = Input.GetAxis("Vertical") * Time.deltaTime * speed;
// Read the mouse input axis
rotX1 += Input.GetAxis("Mouse X") * sensitiveX1;
rotY1 += Input.GetAxis("Mouse Y") * sensitiveY1;
TObject.AddForce(Vector3(x*Mathf.Sin(rotX1),0,z*Mathf.Cos(rotY1) ));
}
The script has the Sphere rigid body connected in the inspector, so TObject
is the sphere.
Attached to the camera i have the mouselook script and the smoothfollow script.
What is happening is that i can rotate around the sphere, the camera follows the sphere, i can move the sphere, but the movement of the sphere is not locked to the rotation of the mouse.
Any idea on how to do this ?
I think it should be somehow similar to a 3rd person camera, however the 3rd camera script in Unity is attached to a capsule.
Maybe changing that capsule to a sphere would work, but the 3rd person camera has no rotation or Y component in the physic world.
Still, i don’t get how this script works, no matter how much speed or velocity i use in those variables, it seems it’s clamped somewhere.
I tried with several physical materials, but no big change in speed.
How are you doing your camera\movement ?
Hi.
The max velocity will be clamped to the drag, in some way, so if you decrease the drag the marble will have a greater max velocity.
I can send the scripts to you, but I don’t got all of them here… But for now you can play around with this script:
//true or false
var notgoal : boolean = true;
//drag
var drag = 2.0;
//speeds
var superspeed2 = 1000;
var jumpspeed = 10.0;
var power = 50.0;
var airpower = 10;
var axepower = 0;
//volym
var JumpSoundPrefab : GameObject;
var ball : Rigidbody;
var auido : AudioSource;
//private
private var grounded = false;
function FixedUpdate () {
if (!grounded notgoal == true) {
//sound
auido.volume = 0;
//movement
var airForward = Camera.main.transform.TransformDirection(Vector3.forward);
airForward.y = 0;
airForward = airForward.normalized;
var airForwardForce = airForward * Input.GetAxis("Vertical") * airpower;
rigidbody.AddForce(airForwardForce);
var airRight = Camera.main.transform.TransformDirection(Vector3.forward);
airRight.y = 0;
airRight = airRight.normalized;
var airRightForce = airRight * Input.GetAxis("Vertical") * airpower;
rigidbody.AddForce(airRightForce);
}
var rightdir = Camera.main.transform.TransformDirection(Vector3.forward);
rightdir.y = 0;
rightdir = rightdir.normalized;
var rightrot = rightdir * Input.GetAxis("Horizontal") * power * 0.8;
rigidbody.AddTorque (rightrot);
var fordir = Camera.main.transform.TransformDirection(Vector3.right);
fordir.y = 0;
fordir = fordir.normalized;
var forwardrot = fordir * Input.GetAxis("Vertical") * power;
rigidbody.AddTorque (forwardrot);
// Only apply forces if the ball is on the floor
if (grounded notgoal == true) {
// sound
auido.volume = rigidbody.velocity.magnitude / 300;
//sound end
var up = Camera.main.transform.TransformDirection(Vector3.up);
up.x = 0;
up.z = 0;
var jump = up * Input.GetAxis("Jump") * jumpspeed * 20;
//if () {
// var instantiatedProjectile : Rigidbody= Instantiate (JumpSoundPrefab, transform.position, transform.rotation);
//}
rigidbody.AddForce(jump);
// Apply drag only if the user is not pressing any keys
if (Mathf.Approximately (Input.GetAxis("Horizontal"), 0) Mathf.Approximately(Input.GetAxis("Vertical"), 0) Mathf.Approximately (Input.GetAxis("Jump"), 0) )
rigidbody.drag = drag;
else
rigidbody.drag = drag;
}
else
rigidbody.drag = 0;
// Every frame set grounded to false. OnCollisionStay will enable it again if
// the rigidbody collides with anything
grounded = false;
}
function OnCollisionStay() {
grounded = true;
}
PS: it can be an error or two in the code… I removed some useless things and I haven’t compiled it yet.
I played around with your script, however it suffers from the same problem, you cannot acelerate with it, the speed is clamped somewhere.
Lowering Drag to zero won’t let you accelerate either., I had my drag to zero before anyways.
Did you achieved some other way to let the sphere go faster ?
I would like to see your scripts yes, if possible (brunomtc AT hotmail.com)
yes i did, angular drag to zero makes it go faster, but doens’t increase the speed.
The force seems to do nothing actually, either at 20 or 200, its the same thing