I want to create a 2d game but when I want the player to avoid being able to jump infinitely in the air by recovering his position I get an error.
My error :
Transform' does not contain a definition for 'postion' and no accessible extension method 'postion' accepting a first argument of type 'Transform' could be found
My code :
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float moveSpeed;
public float jumpForce;
public Rigidbody2D rb;
private Vector3 velocity = Vector3.zero;
public bool isJumping;
public bool isGrounded;
public Transform groundCheckLeft;
public Transform groundCheckRight;
// Update is called once per frame
void FixedUpdate()
{
isGrounded = Physics2D.OverlapArea(groundCheckLeft.Position, groundCheckRight.Position);
float horizontalMovement = Input.GetAxis("Horizontal") * moveSpeed * Time.deltaTime;
if (Input.GetButtonDown("Jump") && isGrounded)
{
isJumping = true;
}
MovePlayer(horizontalMovement);
}
void MovePlayer(float _horizontalMovement)
{
Vector3 targetVelocity = new Vector2(_horizontalMovement, rb.velocity.y);
rb.velocity = Vector3.SmoothDamp(rb.velocity, targetVelocity, ref velocity, .05F);
if(isJumping == true)
{
rb.AddForce(new Vector2(0f, jumpForce));
isJumping = false;
}
}
}
CodePudding user response:
It's transform.position not postion
Edit: Yep, just like Snir Ego said.
CodePudding user response:
the solution should be simple, you wrote groundCheckLeft.Postion
instead of groundCheckLeft.position