using System.Collections;
using UnityEngine;
public class ShieldController : MonoBehaviour
{
public Transform ShieldTform;
public Transform PlayerTform;
public Rigidbody2D rb2D;
public float JumpSpeed;
public float fallMultiplier;
public float lowJumpMultiplier;
public bool canJump;
public bool isJump;
void Update() {
//Jump button check
if (Input.GetKeyDown("space")) {
isJump = true;
}
if (Input.GetKeyUp("space")) {
isJump = false;
}
//Rotate to point at cursor
Vector3 mousePosition = Input.mousePosition;
mousePosition = Camera.main.ScreenToWorldPoint(mousePosition);
Vector2 direction = new Vector2(
mousePosition.x - transform.position.x,
mousePosition.y - transform.position.y
);
transform.up = direction;
//Better fall
if (rb2D.velocity.y < 0) {
rb2D.velocity += Vector2.up * Physics2D.gravity.y * (fallMultiplier - 1) * Time.deltaTime;
} else if (rb2D.velocity.y > 0 && !Input.GetButton("Jump")) {
rb2D.velocity += Vector2.up * Physics2D.gravity.y * (lowJumpMultiplier - 1) * Time.deltaTime;
}
}
void FixedUpdate() {
//Jump
if (isJump == true) {
if (canJump == true) {
Debug.Log("Jump");
rb2D.velocity += (new Vector2(ShieldTform.position.x, ShieldTform.position.y) - new Vector2(PlayerTform.position.x, PlayerTform.position.y)).normalized * JumpSpeed;
}
}
}
//Jump collision check
void OnTriggerEnter2D(Collider2D other) {
if (other.CompareTag("Level")) {
canJump = true;
}
}
void OnTriggerExit2D(Collider2D other) {
if (other.CompareTag("Level")) {
canJump = false;
}
}
}
#Casting broke my jumping
1 messages · Page 1 of 1 (latest)
okay, Ive got this player and a sheild that rotates around it, and when I jump I want to add a force in the opposite direction of the shield relative to the player.
had some problems getting casting to work (new to this) no errors anymore, but when I press jump it wont add any force. what am I doing wrong?
and more importantly, how can I fix it?