- Home /
Unity 5 2d Thorw Object
I'm trying to make a simple dodgeball game. I need the player to throw a gameobject as one would do when playing Dodgeball. I don't need it to be showing or anything. Just kind of spawn as it's being thrown and play an animation.
Here's what I have so far (errors listed after code):`enter code here` public float throwDistance;
// Use this for initialization
void OnCollisionEnter2D (Collision2D collision) {
Destroy (collision.transform.gameObject);
}
// Update is called once per frame
void Update () {
if (Input.GetKeyUp (KeyCode.W)) {
var thrownItem = Instantiate(transform.position, Quaternion.identity) as Transform;
thrownItem.Translate (0, 0, throwDistance);
Animation.Play ("DodgeballP1Throw");
}
}
And I get these errors:
Assets/Scripts/throwDodgeball.cs(19,42): error CS1501: No overload for method
Instantiate' takes
2' argumentsAssets/Scripts/throwDodgeball.cs(20,36): error CS1061: Type
object' does not contain a definition for
Translate' and no extension methodTranslate' of type
object' could be found (are you missing a using directive or an assembly reference?)
Assets/Scripts/throwDodgeball.cs(21,35): error CS0120: An object reference is required to access non-static member `UnityEngine.Animation.Play()'
I'm not a pro nor have I messed with this type of scripting so It may be a simple solution (I can figure out the animation so don't worry about that).
first of all, in your instantiate you also need to declare which object to instantiate.
var thrownItem = Instantiate(yourObject, transform.position, Quaternion.identity) as Transform;
in your start function for the animation try this
private Animation anim;
anim = gameObject.GetComponent<Animator>();
//if the script is attached on the gameObject that has the animator
anim.Play("DodgeballP1Throw"); //blabla
Answer by AlexTheHollow · Jan 02, 2016 at 01:19 PM
Only a couple problems.
Using 'private' in the start function creates MANY errors so I put it where you normally would and put the "anim = gameObject.GetComponent();" in the start function.
I get this error : Assets/Scripts/throwDodgeball.cs(12,60): error CS0201: Only assignment, call, increment, decrement, and new object expressions can be used as a statement
Taking that out makes the ball appear but it is not thrown. I also tried making the Update an IEnumerator so I could have a WaitForSeconds then destroy the ball to prevent overloading. It said the Update Function cannot start a Coroutine (or something like that) and wouldn't spawn the ball anymore.
Do you know a way around that?
Any help is greatly appreciated.
the private I had put in there because it was self explanatory ofcourse you wouldn't put it in the start function.. and are you sure you copied the anim = gameObject.GetComponent<Animator>();
correctly? $$anonymous$$aybe using a rigidbody and adding force is better than using translate aswel?
Okay, so whatever I typed wasn't right and what you had worked.
Since I'm using seperate sprite sheets, I just used the spriteRenderer to change the sprite onkeydown.
I'm trying to get the AddForce but it's not working. I'm even looking at the Docs and they're not even working.
https://unity3d.com/learn/tutorials/modules/beginner/physics/addforce
http://docs.unity3d.com/ScriptReference/Component-rigidbody.html
It's telling me that an object reference is required. I'm working on that right now.
unity 5 changed the way you access it, You have to get its component just like the animator
GetComponent<Rigidbody2D>().AddForce
you can also do this in the start function
rb2d = gameObject.GetComponent<Rigidbody2D>();
and use rb2d.AddForce