# Rotate object around player when space is pressed.

 0 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 :)Warm Regards Trent (castleforce) more ▼ asked Oct 01 '11 at 03:32 AM castleforce 31 ● 6 ● 6 ● 9 add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users

 0 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); } } ``` more ▼ answered Oct 01 '11 at 06:59 AM aldonaletto 42.5k ● 16 ● 43 ● 202 THANK YOU THANK YOU THANK YOU! It works an well to, I never thought of any of that. Well I guess it might be because I have only just started leanring programming, One other thing though, is their a wway to change it so that the ball can only be realeased from the left or right of the player? As the game is 2D and the player can only move left or right on screen.Thank you again. I would hug you if I could. Haha. Oct 01 '11 at 09:19 AM castleforce How are the axes set in your game? X = left-right, Y = up-down, Z = constant? Oct 01 '11 at 03:11 PM aldonaletto Yes X = Left/right Y = Up/Down and Z = forwards/backwardsSo the Player moves along the X axis from left to right. So the aim would be to have the player realease the projectile only along the X axis and no other. Oct 02 '11 at 05:35 AM castleforce The method I used is "physically correct" - the projectile goes out in the tangent direction. Making it go out only to right or left is actually simpler - you only need to know if the projectile is "before" or "after" the player; modify the velOut calculation to this: ``` ... projPos = projectile.position - transform.position; var velOut = Vector3.right * speed; // velOut can be to right if (projPos.z < 0) velOut = -velOut; // or left projectile.rigidbody.velocity = velOut; ... ``` Oct 02 '11 at 12:27 PM aldonaletto Hey, Thanks that works well, Um one thing thought. It seems that the Projectile won't reset it's data after it is destroyed. (Once again thank you for helping me I just hope it is not a bother to you.)Here is the Projectiles code : "(public var eProjSpd : int function OnEnable(){ // Projectiles Force this.rigidbody.AddForce(eProjSpd+(Random.Range(-20.0, 20.0)),eProjSpd,0, ForceMode.Impulse); } function OnDisable(){ //Reset movement data this.rigidbody.velocity = Vector3.zero; this.transform.position = Vector3.zero; this.transform.rotation = Quaternion.identity; } function OnTriggerEnter (col : Collider){ //If Projectile Collides with world destroy self if(col.gameObject.tag == "WorldBounds") { this.gameObject.active = false; } })" Oct 02 '11 at 12:46 PM castleforce add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users

By Email:

Topics:

x832
x747
x672
x261
x51

asked: Oct 01 '11 at 03:32 AM

Seen: 678 times

Last Updated: Oct 04 '11 at 05:59 PM