Im trying to make a 2d platformer-like game, and when it comes to projectiles they kinda follow the player movement. If I shoot in the right direction and I move to the left, the projectile will change its position and start moving to the left from the GameObject assigned as "gun". The "gun" is a child of the player so I always shoot from the same position.
Edit:
Here is the code for shooting:
Edit2: if() statements are for ammo
void OnFire(InputValue value)
{
if(kopanScore.kopanNumber > 0)
{
Instantiate(projectile, projectileSpawn);
kopanScore.MinusKopanScore();
}
else if(kopanScore.kopanNumber <= 0)
{
return;
}
}
Related
My enemy just can't move past a point and it tries to push the player. If my player moves the enemy just flies off the grid.
There is only a Rigidbody component added to my enemy and a character controller component added to my player. There is not a single other Rigidbody or collider component.
This is the code that does my enemy movement:
if (direction.magnitude > 0.35 && zombie.GetCurrentAnimatorClipInfo(0)[0].clip.name != "Zombie Attack")
{
pos = this.transform.position;
pos.z = pos.z + speed;
this.transform.GetComponent<Rigidbody>().MovePosition(pos);
zombie.SetBool("isWalking", true);
zombie.SetBool("isAttacking", false);
}
I think there is something in the Player GameObject that pushes my enemy but I removed every collider and Rigidbody.
Your problem is that: Transform.Translate is moving the transform component while ignoring physics.
If you move them via. Rigidbody.MovePosition they should be able to collide.
I have one kinematic object (player) which i move very fast (it's need), also i have another objects(obstacles) - they have collider (trigger), and when collider of player enter obstacle collider and trigger event, game freeze to half or one second.
if i reduce speed, then everything is fine
i've tried make player not kinematic and mark obstacles as not trigger (for use OnCollisionEnter event)
how i move player:
void FixedUpdate() {
if (currentWalk != Direction.NONE)
{
checkDirection(); // when distance <0.1f stop moving
transform.position = Vector3.MoveTowards(transform.position, nextPosition, speed * Time.fixedDeltaTime); //speed = 25f;
}
}
nextPosition = Vector3(0f,0f,7f); // just for example, initially player stand in 0,0,0
void OnTriggerEnter(Collider collider)
{
if (collider.tag == "Finish")
{
success.SetActive(true); //success - any object which initially
inactive
}
}
I expect the game won't be freeze, without reduce speed, because i really don't know, what is problem, there are many game with high speed.
I am working on a Unity 2D fps multiplayer game. Whenever a player presses Space they instantiate a bullet and when a bullet hits any player it has to destroy itself and push the player in the bullet direction. My code looks like this:
void OnTriggerEnter2D(Collider2D col)
{
if (isServer == false)
return;
if (col.gameObject.tag != "bullet")
return;
CmdTrigger(col.gameObject);
}
[Command]
void CmdTrigger(GameObject col)
{
RpcTrigger(col);
}
[ClientRpc]
void RpcTrigger(GameObject col)
{
Rigidbody2D rb;
rb = col.GetComponent<Rigidbody2D>();
if (rb.velocity.x > 0)
RpcExplode(Mathf.Sign(1));
else
RpcExplode(Mathf.Sign(-1));
//Network.Destroy(col);
}
The problem is that when a bullet collides a player I can't transmit well the GameObject to the other players so every player can destroy the bullet on their client. When a bullet hits a player it doesn't destroy itself, it just passes through and the Console shows me this error: "NullRefrenceException: Object reference not set to an instance of an object", and if I click on it, it brings me to this line:
rb = col.GetComponent<Rigidbody2D>();
Here is my character control full code: https://pastebin.com/L1DEmQv1
Any ideas? It is very important for me to fix it as soon as possible. Thanks.
what I want to achieve is showing a sprite when the screen is touched by the player and make it disappear when a character reached the sprite's position, I made a pic to picture my words if I was not clear :
Thanks for your help
To add previous answer:
you should raycast from you touch position:
void Update()
{
Ray raycast = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
RaycastHit raycastHit;
if (Physics.Raycast(raycast, out raycastHit))
{
if (raycastHit.transform.GetComponent<BoxCollider2D>())
{
sprite.SetActive(false);
}
}
}
1) To show sprite use
if (Input.touchCount > 0)
{ gameObjectToShow.SetActive(true); }
2) To disapper it: add Collider to gameObjectToShow (2D Box or Circle), make it trigger, increase radius of that Collider and use OnTriggerEnter with gameObjectToShow.SetActive(false);
I've got a player and I would like to throw an item what is "attached" to the player.
The problem is that both has rigidbody2D and Collider components. The item what I would like to throw is with the player and it has to collide with the ground and stuff. (except the player)
Here is what I tried:
if (Input.GetButtonDown ("Fire1") && canThrowCandle) {
Candle.rigidbody2D.isKinematic = false;
if (faceingRight)
Candle.rigidbody2D.AddForce(new Vector2(400f, 400f));
else if (!faceingRight)
Candle.rigidbody2D.AddForce(new Vector2(-400f, 400f));
Candle.collider2D.enabled = true;
canThrowCandle = false;
}
And then if the player collides with the item (Candle), it is with the player again:
void OnCollisionEnter2D(Collision2D coll){
if (coll.gameObject.name == "Candle") {
canThrowCandle = true;
Candle.rigidbody2D.isKinematic = true;
Candle.collider2D.enabled = false;
}
And the the code (CandleController) what controls the position of the ithem that I would like to throw:
void Update () {
if (GameObject.Find ("Player").GetComponent<PlayerController> ().canThrowCandle)
transform.position = new Vector3 (player.transform.position.x, player.transform.position.y, -0.01f);
}
So the question is, that how can I make this work?
If I understand correctly that you want the candle to go inside the player but both of them need to collide with the ground.
This can be done using collision layers. Just put ground, player and candle to different layers. Then adjust that both player and candle are colliding with ground, but not with each other.
If you want both trigger collider and physics collider to be attached to same gameObject, I think you need to use child gameObject for that as mentioned here.