I've joined a game jam, and I'm making a 2D platformer in Unity for it. For this I've created a Jump Pad. Whenever the player character stands on it they'll jump. The problem is that the jump doesn't look like a jump, it looks more like a teleportation upwards. Any fixes? Code below!
void OnTriggerEnter2D(Collider2D other) {
if (other.tag == "Spike") {
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
} else if (other.tag == "Jump Pad") {
rb.AddForce(new Vector2(0f, jumpSpeed));
}
}
Edit
https://imgur.com/a/7MM4QJF
this code worked for me
private void OnCollisionEnter2D(Collision2D other)
{
if(other.gameObject.tag == "player")
{
//you can change 20 to whatever number you want
other.GetComponent<Rigidbody2D>().AddForce(new Vector2(0, 20), ForceMode2D.Impulse);
}
}
Note : be sure to tag the player "player"
Note 2 : be sure is trigger is off
You can try to add upward force like below
rb.AddForce(transform.up*jumpSpeed);
Note: Set a 'realistic' jumpSpeed.
DUDE. lol so i just tried this and what do u know it was giving me trouble, but what i did was take that trigger script off the player and have the jumppad detect the trigger.
if (other.tag == "Player")
{
other.GetComponent<Rigidbody2D>().AddForce(new Vector2(0f, jumpSpeed));
}
tag ur player player and put this on the jump pad and set to istrigger
Related
I am developing a 2d game in unity, and I am trying to add friction to slow the player down when he is on the ground. I launch the player into the air with the rigidbody's add force. I have heard of physics materials, and am currently trying to use them. I am using the 2d version of them, I made one material which had high friction. I add the material to the floor, and then I play the game, and my character is still sliding on the floor. I thought the problem might be with the player not having a physics material, so I added one. It still didn't work.
I tried doing different combinations with the different materials.
I tried attaching a rigidbody, but kinematic to the floor.
I tried looking it up, and couldn't find an answer.
Here is the code that moves the player:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
public bool isGrounded = true;
public float fireForce;
Rigidbody2D rb;
void Start()
{
rb = GetComponent<Rigidbody2D>();
camera = camObj.GetComponent<Camera>();
}
void Update()
{
RotateToPoint();
Fire();
}
void OnCollisionEnter2D(Collision2D obj)
{
if (obj.gameObject.tag == "surface")
{
isGrounded = true;
}
}
void OnCollisionExit2D(Collision2D obj)
{
if (obj.gameObject.tag == "surface")
{
isGrounded = false;
}
}
void Fire()
{
if (Input.GetKeyDown(KeyCode.Mouse0) && isGrounded)
{
rb.AddForce(transform.up * fireForce, ForceMode2D.Impulse);
}
}
}
I do not know if I am approaching the problem correctly. If I am not, does anyone know how to avoid slippery game objects. Any help would be appreciated.
Adding a higher angular drag was the solution to my problem. Angular drag is what slows the object down along its rotation. Friction may not always be the problem.
Notice the two highlighted variables. You might want to raise these values (I changed angular drag from its default) to something higher. Adding the physics material might help, you should try this if this first solution doesn't work.
The problem here is that you are not limiting the velocity of the player, so what you can do is set an if statement if the player is touching the floor and the velocity is higher than what you want it to be.
Example:
void FixedUpdate()
{
if (isGrounded == true && rb.velocity.x > 1 || isGrounded == true && rb.velocity.z >1)
{
rb.velocity = Vector3.zero;
rb.angularVelocity = Vector3.zero;
}
}
I have a GameObject that I only want to collide with certain objects, I added a trigger and checked for OnTriggerEnter. However, the rocket doesn't detect collisions with the Turret Object.
Thanks in advance for anyone who helps!
Rocket Script:
void OnTriggerEnter2D(Collider2D other)
{
UnityEngine.Debug.Log("Collision Detected with" + other);
if (other.gameObject.tag == "Player")
{
PlayerController.health -= 75.0f;
Die();
}
if (other.gameObject.tag == "Boss 1")
{
Boss1Controller.health -= 100;
Die();
}
if (other.gameObject.tag == "Turret")
{
Destroy(other);
}
}
Never mind, I figured it out, it turns out that triggers don't work with Edge Colliders2D so I just used a Polygon Collider2D instead. The shape is isn't as perfect as before but it's good enough.
Rookie Mistake, sorry.
So I am taking a class in basic game development and am currently working on a game with Unity. My game worked perfectly up until now when I updated my unity version. In order for the player to actually take damage I have a method that can only be reached if explicitly called by a script. Somehow the ground, that has no script attached, damages my player.
I have posted the code below.
This one is part of the player script
3 references
public void Hurt(int dmg, string yep)
{
HP -= dmg;
Debug.Log($"took {dmg} damage from{yep}. You now have {HP} HP left");
if (HP <= 0)
{
SceneManagement.Death();
}
}
This one is part of the script attatched to my flame object
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag != "FlameTurret" && collision.gameObject.tag != "flame")
{
player.GetComponent<PlayerController>().Hurt(damage, collision.gameObject.tag);
Destroy(this.gameObject);
}
}
This one is attatched to a projectile fired by the enemy
if (collision.gameObject.CompareTag("Player"))
{
if (!called)
{
collision.gameObject.GetComponent<PlayerController>().Hurt(damage, collision.gameObject.tag);
called = true;
}
}
This one is attatched to an enemy
if (collision.gameObject.CompareTag("Player"))
{
if (!called)
{
collision.gameObject.GetComponent<PlayerController>().Hurt(damage, collision.gameObject.tag);
called = true;
}
Destroy(this.gameObject);
}
Console output
Nothing with the tag "Ground" has a script and nothing else than the methods i've posted are supposed to reference my Hurt() method but the ground still damages my player. Any help would be greatly appreciated!
Your problem seems to be that this function here
private void OnCollisionEnter2D(Collision2D collision)
Will be run whenever any collider (game object with a collider component attached - the game object does not need to have any scripts attached to it) intersects with your player
This code here
if (collision.gameObject.tag != "FlameTurret" && collision.gameObject.tag != "flame")
Will pass so long as the object that collides does not have the tag of "FlameTurret" or "flame", which I assume your ground does not.
It seems to me a little odd that everything in your game will damage your player except for flames... is this an error?
Either way a simple fix would be to tag your ground with something like environment and then add under your OnCollisionEnter2D() have a check along the lines of
if (collision.gameObject.tag == "environment"){
return; // do nothing
}
So I have a sphere for the player, and whenever the player is damaged by an object it should knock the ball into the opposite direction, so I am using AddExplosionForce to do the effect, but sometimes it knocks the player back and sometimes it doesn't do anything. Is there something I can do to fix my code (below) or an alternative to doing this effect? Thank you!
private void OnTriggerEnter(Collider other){
if(other.tag == "Pickup")
{
if(other.GetComponent<pickupScript>().ReturnSize() >= score)
{
score--;
//explodeforce in oposite direction
GetComponent<Rigidbody>().AddExplosionForce(500.0f, other.transform.position, .501f, .5f, ForceMode.Impulse);
//add effects
}
}
I am trying to make the player to climb a ladder. (2D game)
I used this code for that,
void OnTriggerEnter2D(Collider2D collider){
if(collider.gameObject.tag=="Ladder"){
_canClimb = true;
_anim.SetBool("Climb",true);
}
}
void OnTriggerExit2D(Collider2D collider){
if(collider.gameObject.tag=="Ladder"){
_canClimb = false;
_anim.SetBool("Climb",false);
}
}
In the update() ,
if(Input.GetKey(KeyCode.UpArrow) && _canClimb == true){
transform.position = Vector3.Lerp(transform.position,ladderTop.transform.position,Time.deltaTime);
}
I have put a child game object to ladder to get the position of the top of the ladder.
But When the player jumps and hit with the box collider of the ladder I can see the climbing animation. And after that the player falls down to the ground again. It doesn't move up. What is the reason for that?
Now it is working as I expected, Here is the code,
if(Input.GetKey(KeyCode.UpArrow) && _canClimb == true){
_myRigidBody.isKinematic = true;
transform.position = Vector3.Lerp(transform.position,ladderTop.transform.position,Time.deltaTime);
}
I just added this line and it works perfectly :
_myRigidBody.isKinematic = true;
Rigidbody.isKinematic
If isKinematic is enabled, Forces, collisions or joints will not affect the rigidbody anymore.