- Home /
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 :)
Warm Regards Trent (castleforce)
Answer by aldonaletto · Oct 01, 2011 at 06:59 AM
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); } }
THAN$$anonymous$$ YOU THAN$$anonymous$$ YOU THAN$$anonymous$$ 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 program$$anonymous$$g, 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.
How are the axes set in your game? X = left-right, Y = up-down, Z = constant?
Yes X = Left/right Y = Up/Down and Z = forwards/backwards
So 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.
The method I used is "physically correct" - the projectile goes out in the tangent direction. $$anonymous$$aking 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; ...
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, Force$$anonymous$$ode.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; } }
)"
Your answer
Follow this Question
Related Questions
LookAt once 0 Answers
I want to rotate the model to face player, not bend? 1 Answer
Changing Player & Enemy Material when a button is pressed. 1 Answer
Instantiate enemies around player 1 Answer