Okay, so I can't see where is my problem. I used OnTriggerEnter for my moving platform. it has rigid body component and the box collider is set to isTrigger on both the platform and player, but for some reason when my platform is triggered by the player, only the OnTriggerExit gets called out . My player is tagged as player in unity... I don't know what to do.
Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Moving_Platform : MonoBehaviour
{
[SerializeField]
private float _speed = 1.0f;
[SerializeField]
private Transform _A, _B;
private bool _direction = false;
void FixedUpdate()
{
if(transform.position==_A.position)
{
_direction = false;
}
else if(transform.position== _B.position)
{
_direction = true;
}
if (_direction == false)
{
transform.position = Vector3.MoveTowards(transform.position, _B.position, _speed * Time.deltaTime);
}
if(_direction==true)
{
transform.position = Vector3.MoveTowards(transform.position, _A.position, _speed * Time.deltaTime);
}
}
private void OnTriggerEnter(Collider other)
{
if (other.tag == "Player")
{
other.transform.parent = this.transform;
}
}
private void OnTriggerExit(Collider other)
{
Debug.Log("OMFG");
if (other.tag == "Player")
{
Debug.Log("But why!");
other.transform.parent = null;
}
}
}
All Inspectors
It seems that updating to a newer version (2020.x) made the trick.
Thanks for trying.
Related
I have the problem with collision. Beginning creating the 3D game, I noticed that Player (gameObject) when moving in case of collision either falls down, or passes through the object, or repels from the object if it is inside it. I want to Player in case of collision couldn't pass through objects. Objects have colliders, Player has both collider, and Rigidbody with Physic Material. I'm beginner, so I don not know how to solve this problem. What is wrong?
Below script Player:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
[SerializeField] public int speed;
public bool ground;
public float jumpPower = 2500f;
public Rigidbody rb;
void Start()
{
rb= GetComponent<Rigidbody>();
}
void Update()
{
GetInput();
}
private void GetInput()
{
if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow))
{
transform.position += transform.forward * speed * Time.deltaTime;
}
// Movement Player...
if (Input.GetKeyDown(KeyCode.Space))
{
if (ground == true)
{
rb.AddForce(transform.up * jumpPower);
}
}
}
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "Ground")
{
ground = true;
}
if (collision.gameObject.tag == "Gem")
{
Destroy(collision.gameObject);
}
}
private void OnCollisionExit(Collision collision)
{
if (collision.gameObject.tag == "Ground")
{
ground = false;
}
}
}
I am making my first Game and now I have a problem: I have a jump button when i press it I am jumping but when I am in the air I can press it again and jump in the air again. How can fix that, so I can jump only on the ground. Here is my Code:
using UnityEngine;
using System.Collections;
using UnityStandardAssets.CrossPlatformInput;
public class Move2D : MonoBehaviour
{
public float speed = 5f;
public float jumpSpeed = 8f;
private float movement = 0f;
private Rigidbody2D rigidBody;
// Use this for initialization
void Start()
{
rigidBody = GetComponent<Rigidbody2D>();
}
public void Jump()
{
rigidBody.AddForce(transform.up * jumpSpeed, ForceMode2D.Impulse);
}
// Update is called once per frame
}
Here is the Code
public class Move2D : MonoBehaviour
{
public bool isGrounded = false;
public float speed = 5f;
public float jumpSpeed = 8f;
private float movement = 0f;
private Rigidbody2D rigidBody;
// Use this for initialization
void Start()
{
rigidBody = GetComponent<Rigidbody2D>();
}
private void OnCollisionEnter2D(Collision2D other)
{
if (other.gameObject.tag == "Ground") //you need to add a tag to your Ground, like "Ground"
{
isGrounded = true;
}
}
public void Jump()
{
rigidBody.AddForce(transform.up * jumpSpeed, ForceMode2D.Impulse);
isGrounded = false;
}
void Update()
{
if (Input.GetButtonDown("Jump") && isGrounded == true)
{
isGrounded = false;
Jump();
}
}
}
first of all you need a boolean (isGrounded). Then you have to check the collision between the player and the ground with
private void OnCollisionEnter2D(Collision2D other)
{
if(other.gameObject.tag == "Ground") //you need to add a tag to your Ground, like "Ground"
{
isGrounded = true;
}
}
And then in the Update method add this code:
if (Input.GetKeyDown(KeyCode.Space) && isGrounded == true)
{
isGrounded = false;
Jump();
}
I am making a simple 2D game in Unity, and I'm trying to make it so my character can only jump if it's touching the ground. Here's my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
[SerializeField] private float movementSpeed = 5.0f;
[SerializeField] private bool isGrounded = false;
private Rigidbody2D rb;
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
void Update()
{
move();
jump();
}
private void move()
{
float move = Input.GetAxis("Horizontal");
rb.velocity = new Vector2(movementSpeed * move * Time.deltaTime, rb.velocity.y);
}
private void jump()
{
if(Input.GetKeyDown(KeyCode.Space) && isGrounded == true)
{
Debug.Log("jump");
}
}
void OnCollisionStay2D(Collision2D, col)
{
if(col.gameObject.tag == "Ground")
{
isGrounded = true;
}
}
void OnCollisionExit2D(Collision2D, col)
{
if(col.gameObject.tag == "Ground")
{
isGrounded = false;
}
}
}
When I run this, it gives me this error: "PlayerMovement.cs(45,44): error CS1001: Identifier expected"
I have tried using Collider2D instead of Collision2D in the OnCollisionStay, but that won't work either. Thanks in advance for the help!
(Collision2D, col) change to (Collision2D col)
void OnCollisionStay2D(Collision2D col)
{
if(col.gameObject.tag == "Ground")
{
isGrounded = true;
}
}
void OnCollisionExit2D(Collision2D col)
{
if(col.gameObject.tag == "Ground")
{
isGrounded = false;
}
}
This code resets my camera(character) position if the user tries to exit the game boundaries by using a box collider and triggers. Would there be any way to stop the camera or bounce back the camera slightly when it enters/exits a trigger instead of resetting its position? Thank you
using UnityEngine;
using System.Collections;
public class BoundaryTrigger : MonoBehaviour {
public float hoverForce = 12f;
public Vector3 startPos;
void Start()
{
startPos = transform.position;
}
void OnTriggerEnter(Collider other) {
}
void OnTriggerStay(Collider other)
{
}
void OnTriggerExit(Collider other)
{
transform.position = startPos;
}
}
I would do something like this:
Vector3 m_LastValidPosition = null;
void OnTriggerStay(Collider other)
{
m_LastValidPosition = transform.position;
}
void OnTriggerExit(Collider other)
{
transform.position = m_LastValidPosition;
}
We are at very beginning of studying Unity, so we decided to create a mini-platformer. We've already made coins, platforms and character animation, but when we tried to animate a platform, a huge catastrophe appeared. The problem is that character can't stand on the platform. When the platform moves, he falls (it looks like there's no friction, but we tried to set one - it was helpless).
Maybe, we repeat the question, that have been asked before, but hope you'll help us to solve this paradox. Have a nice day ;)
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class CharControl : MonoBehaviour
{
public float maxSpeed = 10f;
private bool isFacingRight = true;
private Animator anim;
private bool isGrounded = false;
public Transform groundCheck;
private float groundRadius = 0.2f;
public LayerMask whatIsGround;
public Text scoreText;
public float score = 0;
private void Start()
{
anim = GetComponent<Animator>();
}
private void FixedUpdate()
{
isGrounded = Physics2D.OverlapCircle(groundCheck.position, groundRadius, whatIsGround);
anim.SetBool ("Ground", isGrounded);
anim.SetFloat ("vSpeed", rigidbody2D.velocity.y);
if (isGrounded && rigidbody2D.velocity.y != 0)
return;
float move = Input.GetAxis("Horizontal");
anim.SetFloat("Speed", Mathf.Abs(move));
rigidbody2D.velocity = new Vector2(move * maxSpeed, rigidbody2D.velocity.y);
if(move > 0 && !isFacingRight)
Flip();
else if (move < 0 && isFacingRight)
Flip();
}
private void Update()
{
if (isGrounded && (Input.GetKeyDown (KeyCode.Space) || Input.GetKeyDown(KeyCode.Joystick1Button0)))
{
anim.SetBool("Ground", false);
rigidbody2D.AddForce(new Vector2(0, 600));
}
}
private void Flip()
{
isFacingRight = !isFacingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
void OnTriggerEnter2D(Collider2D col){
if(col.gameObject.name == "Skull"){
score++;
Destroy (col.gameObject);
scoreText.text = "" + score;
}
if ((col.gameObject.name == "dead"))
Application.LoadLevel (Application.loadedLevel);
}}
you need to set MovingPlatform is ParentObject of HoldPlayerPlatform.
check below images for that also
Here is Holding Player Script on Moving Platform.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HoldPlayer : MonoBehaviour
{
private GameObject target = null;
private Vector3 offset;
void Start()
{
target = null;
}
void OnTriggerStay(Collider col)
{
target = col.gameObject;
offset = target.transform.position - transform.position;
}
void OnTriggerExit(Collider col)
{
target = null;
}
void LateUpdate()
{
if (target != null)
{
target.transform.position = transform.position + offset;
}
}
}
Here is image for setup HoldPlayerPlatform
Here is Setup for MovingPlatform