Rotate object around player when space is pressed.

Hey guys, first time poster here, though I do visit this site reguarly to help me. Anyway to get to the point, I’m currently making a game and I’m at a point where I’m trying to have the player catch a projectile that is shot by an enemy by pressing space when the projectile enters the players radius. Then have the projectile rotate around the player untill the player presses space again which will then luanch the projectile in the direction that it was at when space was hit again.

If anyone could help me it would be greatly thanked :slight_smile:

Warm Regards
Trent (castleforce)

This isn’t an easy task: you must get the projectile velocity and use it to calculate the rotation speed, then rotate it until the key is pressed again - at this point you will have to calculate the exiting speed according to the current projectile position.

This script must be assigned to a trigger object; when some rigidbody enters the trigger, its transform is stored in projectile :

var projectile: Transform;
public var maxRps: float = 3; // max revolutions per second
private var rps: float; // revolutions per second
private var speed: float; // velocity value
private var projPos: Vector3;
private var got = false; // indicates if projectile got

function OnTriggerEnter(hit: Collider){
	if (!got && hit.rigidbody) projectile = hit.transform;
}

function Update(){
	if (Input.GetKeyDown("space")){
		got = !got; // toggle flag
		if (got){
			projPos = projectile.position - transform.position;
			var distance = projPos.magnitude;
			var vel = projectile.rigidbody.velocity;
			speed = vel.magnitude;
			// find the direction of rotation
			var cross = Vector3.Cross(projPos, vel);
			if (cross.y < 0) speed = -speed;
			// find RPS equivalent to the projectile speed
			rps = speed/(2*Mathf.PI*distance);
			// clamp RPS to acceptable limits
			rps = Mathf.Clamp(rps, -maxRps, maxRps);    
			// zero rigidbody velocity and disable gravity
			projectile.rigidbody.velocity = Vector3.zero;
			projectile.rigidbody.useGravity = false;
		}
		else {
			projPos = projectile.position - transform.position;
			var velOut = Vector3.Cross(Vector3.up, projPos);
			// set rigidbody.velocity to the same speed saved
			projectile.rigidbody.velocity = speed*velOut.normalized;
			projectile.rigidbody.useGravity = true;
		}
	}
	if (got){
		projectile.RotateAround(transform.position, Vector3.up, rps*360*Time.deltaTime);
	}
}