Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by castleforce · Oct 01, 2011 at 03:32 AM · playerrotateenemyprojectileradius

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)

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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); } }

Comment
Add comment · Show 9 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image castleforce · Oct 01, 2011 at 09:19 AM 0
Share

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.

avatar image aldonaletto · Oct 01, 2011 at 03:11 PM 0
Share

How are the axes set in your game? X = left-right, Y = up-down, Z = constant?

avatar image castleforce · Oct 02, 2011 at 05:35 AM 0
Share

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.

avatar image aldonaletto · Oct 02, 2011 at 12:27 PM 0
Share

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;
        ...
avatar image castleforce · Oct 02, 2011 at 12:46 PM 0
Share

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; } }

)"

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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

Camera rotation around player while following. 6 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges