Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by gilgada · Feb 01, 2012 at 10:25 AM · velocitygravityforceprojectilecatapult

Firing projectile in curve

Okay so I am having some trouble with this. I have an object in unity called Catapult arm. This object contains the meshs and colliders for the arm of a catapult which can be controlled using the arrow keys. The catapult arm object also contains a test block that I am trying to launch when the catapult arm reaches a certain angle (normally reached when firing).

By default the projectile block has a rigidbody set to not use gravity and to be kinematic, this is so that it plays nicely when moving the catapult arm object with the arrow keys. This is all fine and I can raise and lower the catapult arm and block.

The problem comes with when the firing angle has been reached. I have a script attached to the projectile called "changeProjectileState". this script is as follows:

gameObject.tag == "projectile";
function Update () 
{
    if(rigidbody.isKinematic == true)
    {        
        if( transform.rotation.eulerAngles.x >= 75
            && transform.rotation.eulerAngles.x <= 85)
            {
            transform.parent = null;
            rigidbody.useGravity = true;
            rigidbody.isKinematic = false;
            }
    }
}

I then have a script attached to take care of the movement of the projectile when it is no longer attached to the arm object.

This is the problem as currently it just adds a force to the object and doesnt stop. I want it to take advantage of the activated gravity and apply a single initial force and then leave the rest to the engine so that the block slows down and stops.

gameObject.tag = "projectile";

function Update () { if(rigidbody.isKinematic ==false) { if(rigidbody.AddForce.z <= 20) { rigidbody.AddForce (0,0,10); } } }

Any help would be really appreciated. :)

Comment
Add comment · Show 2
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 gilgada · Feb 01, 2012 at 11:22 AM 0
Share

i know the use of rigidbody.iskinematic ==false for the condition of the second script isn't ideal. if anyone has a suggestion for a different way of activating the script when the block is free of its parent, then do tell :)

avatar image syclamoth · Feb 01, 2012 at 11:40 AM 0
Share

What I don't understand, is why you are adding forces like this at all! Surely, all you need to do is sample the velocity at the moment of release, and let the physics engine do the rest?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by syclamoth · Feb 01, 2012 at 11:43 AM

How about this-

 function Release()
 {
     yield WaitForFixedUpdate();
     var curPos : Vector3 = transform.position;
     yield WaitForFixedUpdate();
     var deltaPos : Vector3 = transform.position - curPos;
     var calculatedVelocity = deltaPos / Time.fixedDeltaTime;
     transform.parent = null;
     rigidbody.useGravity = true;
     rigidbody.isKinematic = false;

     // this is the key bit-
     rigidbody.velocity = calculatedVelocity;
 }

This samples the movement of the arm at the moment just before the ball should be released, and sets its velocity accordingly.

Comment
Add comment · Show 10 · 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 gilgada · Feb 02, 2012 at 12:33 PM 0
Share

Am I to replace one of my above scripts with your example? Sorry for sounding stupid but I'm unsure as to how to use your script

avatar image gilgada · Feb 02, 2012 at 01:37 PM 0
Share

I've made a few changes according to your answer. I have a script called rotateCatapultArm which allows the player to choose an angle for the arm before firing (which should in turn affect the force on the projectile, raised arm = less swing and less force). I have added your curPos variable to this script as this position is what should be referenced as the 'initial position' before firing. The script is as follows:

var speed : float = 5.0f; // meters per second var curPos : Vector3 = transform.position;

gameObject.tag == "catapult arm";

function Update() { if(Input.Get$$anonymous$$ey ("up") && transform.rotation.eulerAngles.x < 85) { transform.rotation.eulerAngles.x += speed; } if(Input.Get$$anonymous$$ey ("down") && transform.rotation.eulerAngles.x > 10) { transform.rotation.eulerAngles.x -= speed; }

}

I have then used the remainder of your velocity calculation code within the projectile$$anonymous$$ovement script:

var projectile : Rigidbody; var curPos:rotateCatapultArm = GetComponent(rotateCatapultArm);

function Update() {

if(rigidbody.is$$anonymous$$inematic ==false) { curPos.Translate(0,0,0); var deltaPos: Vector3 = transform.position - curPos; var calculatedVelocity = deltaPos / Time.fixedDeltaTime;

rigidbody.velocity = calculatedVelocity;

} }

but this doesnt seem to do anything. For starters, I get an error in the console saying BCE0051: Operator '-' cannot be used with a left hand side of type 'UnityEngine.Vector3' and a right hand side of type 'rotateCatapultArm'.

Could you please assist me with this?

avatar image syclamoth · Feb 02, 2012 at 11:56 PM 0
Share

Well, do you have a 'Translate' method for your rotateCatapultArm script? You can't subtract a 'rotateCatapultArm' from a vector, now, can you? I think you should be using 'curPos.transform.position' for that...

In any case, my 'Release' method should be called from inside Update (or whatever) at the moment that you want to release the ball from the catapult. It manages all of the velocity calculation assu$$anonymous$$g that the arm continues to move. I'm not really sure why people keep changing my scripts and then wondering why they don't work any more.

avatar image gilgada · Feb 03, 2012 at 12:27 PM 0
Share

Sorry, I did not mean to make things more problematic. $$anonymous$$y catapult arm does not carry on moving at the moment the projectile is "unchilded". Do you think it would be best if it was?

avatar image gilgada · Feb 03, 2012 at 12:59 PM 0
Share

I've amended my LaunchSequence.js to

var scriptObject : GameObject;
function Update () {
    if(Input.Get$$anonymous$$ey ($$anonymous$$eyCode.Space) 
        && transform.rotation.eulerAngles.x <= 75)
        {
        transform.rotation.eulerAngles.x += 10;
        }
    if(transform.rotation.eulerAngles.x >=70)
        {
        scriptObject.GetComponent(changeProjectileState).Release();
        }
}

and then put the Release method in to changeProjectileState.js

gameObject.tag == "projectile";
function Release () 
{
    if(rigidbody.is$$anonymous$$inematic == true)
//    yield WaitForFixedUpdate();
    var curPos : Vector3 = transform.position;
//    yield WaitForFixedUpdate();
    var deltaPos : Vector3 = transform.position - curPos;
    var calculatedVelocity = deltaPos / Time.fixedDeltaTime;
    transform.parent = null;
    rigidbody.useGravity = true;
    rigidbody.is$$anonymous$$inematic = false;
    // this is the key bit-
    rigidbody.velocity = calculatedVelocity;
}

and then tried to point the scriptObject component to my wooden projectile in the inspector so that it knows what to call the Release method for, also removing my awkward if statement of is$$anonymous$$inematic == false

Problem is, it now seems that the block doesn't move at all. I'm not sure if the reference to Release is working or not but the block doesn't even seem to fall due to the gravity that should turn on $$anonymous$$aybe I'm making a few mistakes here but I feel like it is getting close

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Setting limits to my trajectory 2 Answers

rigidbody.Velocity stops gravity 2 Answers

Keeping character's movement consistent. 1 Answer

Simulate gravity on rigidbody 1 Answer

Gravity trouble: Falling slow, jumping fast 1 Answer


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