I am trying to do ground detection for a 2d platformer, but my physics check is always returning true. I am reusing code that I have used a many times before:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
[Header("MOVEMENT")]
public float speed;
float inputX;
[Header("JUMPING")]
public float jumpForce;
public Transform feet;
public Vector2 checkSize;
public bool isGrounded;
public LayerMask layers;
public int extraJumps = 1;
int jumpTracker;
private Rigidbody2D rb;
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
void Update()
{
inputX = Input.GetAxisRaw("Horizontal");
isGrounded = Physics2D.OverlapBox(feet.position, checkSize, 0f, layers);
if(Input.GetKeyDown(KeyCode.Space))
{
if (isGrounded)
{
rb.velocity = new Vector2(rb.velocity.x, jumpForce);
jumpTracker = extraJumps;
}
else
{
if(jumpTracker > 0)
{
rb.velocity = new Vector2(rb.velocity.x, jumpForce);
jumpTracker--;
}
}
}
}
private void FixedUpdate()
{
rb.velocity = new Vector2(inputX * speed, rb.velocity.y);
}
private void OnDrawGizmos()
{
Gizmos.DrawWireCube(feet.position, checkSize);
}
}
In the scene there is a Tilemap with a composite collider tagged as ground that the player is colliding with.
I have tried everything I can think of and still have no idea what is causing this issue. Any help would be greatly appreciated.
Related
I have a simple player movement script, but I am running into the following error 'RenderBuffer' does not contain a definition for 'velocity', and I don't know how to fix it.
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting.Antlr3.Runtime.Tree;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
private Rigidbody2D rb;
private BoxCollider2D coll;
private bool hasDoubleJumped = false;
[SerializeField] private float moveSpeed = 7f;
[SerializeField] private float jumpForce = 14f;
[SerializeField] private LayerMask jumpableGround;
private void Start()
{
rb = GetComponent<Rigidbody2D>();
coll = GetComponent<BoxCollider2D>();
}
private void Update()
{
float dirX = Input.GetAxisRaw("Horizontal");
rb.velocity = new Vector2(dirX * moveSpeed, rb.velocity.y);
if (Input.GetButtonDown("Jump") && IsGrounded())
{
hasDoubleJumped = false;
rb.velocity = new Vector2(RenderBuffer.velocity.x, jumpForce);
}
else if (Input.GetButtonDown("Jump") && !hasDoubleJumped)
{
hasDoubleJumped = true;
rb.velocity = new Vector2(RenderBuffer.velocity.x, jumpForce);
}
}
private bool IsGrounded()
{
return Physics2D.BoxCast(coll.bounds.center, coll.bounds.size, 0f, Vector2.down, .1f, jumpableGround);
}
}
I have tried changing a few things, but it doesn't seem to solve my problem.
On your jump logic you wrote RenderBuffer.velocity.x, seems out of place, try rb.velocity.x if you want to to access the current rigidbody velocity.
It looks like the problem is with the code RenderBuffer.velocity which is trying to access the velocity property of RenderBuffer, but velocity isn't a property of RenderBuffer. I notice you also have code like rb.veclocity where rb refers to a RigidBody (which does have a velocity property). Could you have a typo here? Perhaps you just need to replace RigidBody.velocity with rb.velocity in your code.
So as previously said I need the bullet to follow the enemy. I'm making a 2d Game and just can't figure out how. I'll appreciate it if someone could help me. Here are my scripts.
Bullet Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerFire : MonoBehaviour
{
float moveSpeed = 25f;
Rigidbody2D rb;
Monster target;
Vector2 moveDirection;
public float firespeed;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
transform.Translate(Vector2.right * firespeed * Time.deltaTime, Space.Self);
}
private void OnCollisionEnter2D(Collision2D collision)
{
Debug.Log(collision.transform.name);
Destroy(gameObject);
}
}
Enemy script
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Monster : MonoBehaviour {
[SerializeField]
GameObject bullet;
float fireRate;
float nextFire;
public Image BosshealthBar;
public float BosshealthAmount = 100;
public GameObject effect;
// Use this for initialization
void Start () {
fireRate = 1f;
nextFire = Time.time;
}
// Update is called once per frame
void Update () {
CheckIfTimeToFire ();
if (BosshealthAmount <= 0)
{
Destroy(gameObject);
}
}
void CheckIfTimeToFire()
{
if (Time.time > nextFire) {
Instantiate (bullet, transform.position, Quaternion.identity);
nextFire = Time.time + fireRate;
}
}
public void TakeDamage(float Damage)
{
BosshealthAmount -= Damage;
BosshealthBar.fillAmount = BosshealthAmount / 100;
}
public void Healing(float healPoints)
{
BosshealthAmount += healPoints;
BosshealthAmount = Mathf.Clamp(BosshealthAmount, 0, 100);
BosshealthBar.fillAmount = BosshealthAmount / 100;
}
void OnCollisionEnter2D(Collision2D col)
{
if (col.gameObject.tag.Equals("BulletPlayer"))
{
TakeDamage(10);
Instantiate(effect, transform.position, Quaternion.identity);
}
}
}
Something like this may help u
Vector3 direction = Target.position - Bullet.position;
float distance = direction.magnitude;
float TargetSpeed = 5
Vector3 Force = direction.normalized * ForceMagnitude;
Bullet.AddForce(Force);
But you will need to add it in yourself since i can't tell how your code speceficly works
https://drive.google.com/file/d/1Uc2sys7ZFd686UkQlYAMEDZekFEHZcAr/view?usp=sharing
as u see in the video when I try to jump from the mountain the player just goes through the terrain as if it's not there but I can still move normal and I noticed that whenever I move fast while being off the ground the problem appears like when I first started this level I was jumping and returning to the ground very slowly and whenever I jump and move fast towards a hell the problem occurs so I increased the gravity so that I can't move while jumping but still if I move fast while in the air the player won't collide with the terrain but when I move normally or fast on the ground non of that happen's can anyone help me?
I searched on the internet and I found nothing related to this.
terrain 1 properties:
https://drive.google.com/file/d/1VwelecESLjfD7UseP6lmQ0Kkg-3pwg67/view?usp=sharing
terrain 2 properties:
https://drive.google.com/file/d/1ZH-cIBXlhBMPbmkgZXkQmjt-QVpe6MLu/view?usp=sharing
player properties:
https://drive.google.com/file/d/1vTp3TSuIDcYm3orWhy5X3kw6wwBmwNcE/view?usp=sharing
here are the codes used for this:
mouse lock:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class mouselock : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
Cursor.lockState = CursorLockMode.None;
Cursor.visible = true;
}
}
player movement:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class playermove2 : MonoBehaviour
{
[Header("movement")]
private float movespeed = 10f;
[SerializeField] float airmultiplier = 0.4f;
[SerializeField] Transform oriantation;
private float horizontalmovement;
private float verticalmovement;
private float rbdrag = 6f;
private Vector3 movedirection;
public float movementmultiplier = 10f;
public bool isgrounded;
private float playerhight = 1.8f;
public float jumpforce = 8;
public Rigidbody rb;
public float grounddrag = 6f;
public float aridrag = 2f;
public int number;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
if (number == 11)
{
SceneManager.LoadScene("finish scene2");
}
if (Input.GetKeyDown(KeyCode.LeftShift))
{
movespeed = 25;
}
if (Input.GetKeyUp(KeyCode.LeftShift))
{
movespeed = 20;
}
if (Input.GetKeyDown(KeyCode.Space) && isgrounded)
{
jump();
}
print(isgrounded);
myinput();
controledrag();
}
private void myinput()
{
horizontalmovement = Input.GetAxisRaw("Horizontal");
verticalmovement = Input.GetAxisRaw("Vertical");
movedirection = oriantation.forward * verticalmovement + oriantation.right * horizontalmovement;
}
private void FixedUpdate()
{
moveplayer();
}
void moveplayer()
{
if (isgrounded)
{
rb.AddForce(movedirection.normalized * movespeed * movementmultiplier, ForceMode.Acceleration);
}
else
{
rb.AddForce(movedirection.normalized * movespeed * movementmultiplier * airmultiplier, ForceMode.Acceleration);
}
}
void controledrag()
{
if (isgrounded)
{
rb.drag = grounddrag;
}
else
{
rb.drag = aridrag;
}
}
void jump()
{
rb.AddForce(transform.up * jumpforce, ForceMode.Impulse);
}
}
collisions:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class collisions2 : MonoBehaviour
{
public playermove2 player;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
private void OnCollisionEnter(Collision other)
{
if (other.gameObject.layer == 6)
{
player.isgrounded = true;
}
}
private void OnCollisionExit(Collision other)
{
if (other.gameObject.layer == 6)
{
player.isgrounded = false;
}
}
}
camera movement:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoveCamera : MonoBehaviour
{
[SerializeField] Transform cameraposition;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
transform.position = cameraposition.position;
}
}
player look:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class playerlook : MonoBehaviour
{
[SerializeField] private float sensX;
[SerializeField] private float sensY;
[SerializeField] Transform cam;
[SerializeField] Transform oriantation;
private float mouseX;
private float mouseY;
private float multiplier = 0.01f;
private float xRotation;
private float yRotation;
private void Start()
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
private void Update()
{
mouseX = Input.GetAxisRaw("Mouse X");
mouseY = Input.GetAxisRaw("Mouse Y");
yRotation += mouseX * sensX * multiplier;
xRotation -= mouseY * sensY * multiplier;
xRotation = Mathf.Clamp(xRotation, -90f, 90f);
cam.transform.rotation = Quaternion.Euler(xRotation, yRotation, 0);
oriantation.transform.rotation = Quaternion.Euler( 0, yRotation, 0);
}
}
triggers:
using UnityEngine;
using UnityEngine.SceneManagement;
public class triggers2 : MonoBehaviour
{
public playermove2 playerMove;
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.layer == 8)
{
Destroy(other.gameObject);
playerMove.number++;
}
}
}
score and high score:
using UnityEngine;
using UnityEngine.UI;
public class scoreandhighscore2 : MonoBehaviour
{
public Text highscoretext;
public Text scoretext;
public playermove2 playermove;
// Start is called before the first frame update
void Start()
{
highscoretext.text = "high score: " + PlayerPrefs.GetInt("highscore");
}
// Update is called once per frame
void Update()
{
scoretext.text = "score: " + playermove.number.ToString();
}
private void FixedUpdate()
{
if ( playermove.number > PlayerPrefs.GetInt("highscore"))
{
PlayerPrefs.SetInt("highscore", playermove.number);
}
}
}
sooooo I solved the problem the solution is just converting the collision detection to continuous and just like that the player won't break through the terrain I hope this is helpful for other people
here's a pic of what I did:
https://drive.google.com/file/d/1ati8xQFQxwqAg8eJnmZzrs8D05HGuJLs/view?usp=sharing
In my Unity my Player gets everytime stuck in the collider when he has a big force (If he falls from a high place, The Gravity Scale or his jumppower is high). In the picture you can see Colliders and Renderer.
Only one Component is not on in screen at the platformcomponents, Tilemap.
Here is my code for Playermovement:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
[SerializeField] private LayerMask platformsLayerMask;
private Rigidbody2D rigidbody2d;
private BoxCollider2D boxCollider2d;
private void Awake()
{
rigidbody2d = transform.GetComponent<Rigidbody2D>();
boxCollider2d = transform.GetComponent<BoxCollider2D>();
}
private void Update()
{
if (IsGrounded() && Input.GetKeyDown(KeyCode.Space))
{
float jump_power = 17.5f;
rigidbody2d.velocity = Vector2.up * jump_power;
}
HandleMovement();
}
private bool IsGrounded()
{
RaycastHit2D raycastHit2d = Physics2D.BoxCast(boxCollider2d.bounds.center, boxCollider2d.bounds.size, 0f, Vector2.down, .1f, platformsLayerMask);
return raycastHit2d.collider != null;
}
private void HandleMovement()
{
float moveSpeed = 10f;
if (Input.GetKey(KeyCode.A))
{
rigidbody2d.velocity = new Vector2(-moveSpeed, rigidbody2d.velocity.y);
}
else if (Input.GetKey(KeyCode.D))
{
rigidbody2d.velocity = new Vector2(+moveSpeed, rigidbody2d.velocity.y);
}
else
{
rigidbody2d.velocity = new Vector2(0, rigidbody2d.velocity.y);
}
}
}
Have you tried setting the player's rigidbody collision detection to continuous? If the collision detection is set to discrete, it's really easy to come accross this kind of problem when using the built in physics system
How would I be able to set the speed for going forward and back in the Void FixedUpdate? Or is there a better way for doing this? I need to use the character controller though.
using UnityEngine;
using System.Collections;
public class CharacterControllerz : MonoBehaviour {
public float speed;
private CharacterController playerController;
void Start()
{
playerController = GetComponent<CharacterController>();
}
void Update()
{
}
void FixedUpdate()
{
if (Input.GetKey("right"))
{
playerController.Move (Vector3.forward);
Debug.Log ("RIGHT");
}
if (Input.GetKey("left"))
{
playerController.Move (Vector3.back);
Debug.Log ("LEFT");
}
playerController.Move (Vector3.left);
}
}
public float speed = 5f;
public float jumpStrenght = 8f;
public float gravity = 20f;
private Vector3 moveDirections = new Vector3();
private Vector3 inputs = new Vector3();
void FixedUpdate()
{
CharacterController cc = GetComponent<CharacterController>();
if (cc.isGrounded)
{
if (Input.GetKey("right"))
inputs.z = 1;
if (Input.GetKey("left"))
inputs.z = -1;
if (Input.GetKey("up"))
inputs.y = jumpStrenght;
moveDirections = transform.TransformDirection(inputs.x, 0, inputs.z) * speed;
}
moveDirections.y = inputs.y - gravity;
cc.Move(moveDirections * Time.deltaTime);
}
I think that's what you want but you may have to switch the axis.
Edit: Why is TransformDirection used? Because we want to move the object in a direction which shouldn't be relativ to the object's rotation.