marble camera

Hi,

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 ?

thanks

What are you trying to do? Do you want the marble as the player and then follow it?

yes, thats it

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.

Um… Is it something like this? http://arongranberg.com/unity/network/marble

(P to pause, Sorry for the ugly gui)

/Aron

Hi sturestone,

Yes, that’s it.
How are you doing it ?
I was able to get it working with the script i found in this thread :

http://forum.unity3d.com/viewtopic.php?t=10282&highlight=marble

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 ?

thanks

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.

Hi surestone,

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) :wink:

thanks

Have you tested setting the Angular Drag to zero? And increasing the force?

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

Hey, now I remember… There is a “Max Angular Velocity” setting in the Physics Settings.
Increase that and I’m sure it will go faster.

/Aron

Hi again.
Sorry I haven’t answered for a while.
but if you still want the script for the cam… Here it is:

PS: There is some code for zooming stuff in the code, but you can remove it if you want.

var target : Transform;
var distance = 10.0;

private var xSpeed = 250.0;
private var ySpeed = 0;
private var ySpeed2 = 250.0;

var yMinLimit = -20;
var yMaxLimit = 80;

var xn = 0.0;
var yn = 0.0;
var zoomable = false;
var zoom = 0;
var max_zoom = 10;
var zoom_speed = 1;
var orbit = false;


@script AddComponentMenu("Camera-Control/Mouse Orbit")
function Start () {
	
    var angles = transform.eulerAngles;
    xn = angles.y;
    yn = angles.x + 25;

	// Make the rigid body not change rotation
   	if (rigidbody)
		rigidbody.freezeRotation = true;
}
function Update () {
	if (orbit) {
		yn += Input.GetAxis("Ybutton") * ySpeed2 * 0.005;
		xn += Input.GetAxis("Xbutton") * xSpeed * 0.02;
	}
}
function LateUpdate () {
	if (orbit) {
		if (zoomable) {
			if (Input.GetButtonDown ("Zoom")) {
				if (zoom < max_zoom) {
					zoom += zoom_speed;
				} else {
					zoom = 0;
				}
			}
		} else {
			zoom = 0;
		}
		gameObject.GetComponent (Camera).fieldOfView = 65 - zoom;
	}
    if (target) {
    	
        xn += Input.GetAxis("Mouse X") * xSpeed * 0.02;
        var position;
        var rotation = Quaternion.Euler(yn, xn, 0);
        if (orbit) {
     		yn -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;
 			yn = ClampAngle(yn, yMinLimit, yMaxLimit);
    		position = rotation * Vector3(0.0, 0.0, -distance) + target.position;
        } else {
        	position = target.position;
        }
        
        transform.rotation = rotation;
        transform.position = position;
        	
        /*var hit_right : RaycastHit;
        if (Physics.Raycast (transform.position,transform.TransformDirection (Vector3.right),hit_right,6)) {
        	var xblockplus = xn + ((hit_right.distance / ((distance * 2) * 3.14)) * 360);
        	xn = ClampAngle(xn, Infinity, xblockplus);
        }*/
    }
}

static function ClampAngle (angle : float, min : float, max : float) {
	if (angle < -360)
		angle += 360;
	if (angle > 360)
		angle -= 360;
	return Mathf.Clamp (angle, min, max);
}

I have been a bit busy, but i will surely try it this weekend.
Thanks again :wink: