- Home /
Enemy shoots player (Tower)
Hello!
I have a problem with Enemy (Tower) shooting player
I use this code (which I read that half of the world use to shoot the target :) ):
Vector3 shootPos =(CurrentTarget.transform.position - this.transform.position).normalized;
bullet.rigidbody = shootPos * acceleration;
But unfortunetly what I get as a result is Tower shooting bullet towards player but in wrong "Y" direction. It always goes too high - I mean the bullet is fired from the top of the Tower (let's say y=10) and my player (the target for tower) position y=3. The bullet should shoot him but instead it always go above the player.
Please help me with this issue - I wasted 5 hours for this. Thank you in advance
@kunmateo1993 There are not enough information to detect the problem (i.e. when and where the code you presented is called?). If you are using rigid body to move the bullet game object expect the physics to be applied to this, so things like gravity could be relevant in the process (although I would expect and undershoot).
bullet.rigidbody = shootPos * acceleration;
Don t think this is correct. $$anonymous$$aybe are you updating the velocity? Also
Vector3 shootPos =(CurrentTarget.transform.position - this.transform.position).normalized;
this.transform.position).normalized; should be named shootDirection, but I guess you understand what you are calculating there.
There are two things that you could try in order to solve your problem:
Assu$$anonymous$$g you are doing this on a some Update():
bullet.transform.Translate (shootPos * speed * Time.deltaTime);
Assu$$anonymous$$g you are doing this in a method that is called once I would try to set a force to the rigid body:
bullet.rigidbody.AddForce(shootPos * forcestrength);
Hope it is clear enough.
Andrea
I am sorry - previously I wasn't clear (forgive me it was late in the night :) )
Shoot function is called once, not in update. Gravity is Off.
I have one shoot function which is called by player and by enemy but with two different parameter's value.
For player I use this as parameter:
Ray ray = $$anonymous$$ainCamera.ScreenPointToRay (_tmCursor.position);
shootDirection = new Vector3 (ray.direction.x, ray.direction.y, ray.direction.z);
And it works just fine - bullet goes where the Cursor was pointing.
In case of Enemy I have this:
Vector3 shootDirection =(CurrentTarget.transform.position - transform.position).normalized;
And it causes that
bullet.ctRigidbody.velocity = (shootDirection * oCharacter.CurrentSkill.Acceleration);
works like I described previously - bullet goes above player so he cant be hit.
Please, if you come up with some idea after this additional information let me know. AddForce doesnt work on "Click" because my bullet wasnt moving. AS I remember correctly addFroce works in Update().
@kunmateo1993 So I am assu$$anonymous$$g you are doing an FPS. The shootdirection seems correct to me. Have you check a very trivial thing:
where the transform of CurrentTarget object is in relation to your main camera position?
Have you changed some parameters to the camera?
Your answer
Follow this Question
Related Questions
Not shooting in desired direction 1 Answer
How to make a player rotate in a direction with a slope of the ground. 0 Answers
Making laser beam point in shooting direction C# 1 Answer
For some akward reason bullet is moving in the wrong direction 1 Answer
How can the player position updates randomly when the player isn't movingl? 0 Answers