- Home /
Preventing Rotated Jumping (C#)
I am currently working with 2D rigid bodies, and I seem to encounter the problem of having the ability to jump on walls as well as jumping while rotated. What would be the best method of preventing my player from doing this? Here is the script that I am currently using:
using UnityEngine;
using System.Collections;
public class CubeMovement : MonoBehaviour {
float maxSpeed = 4.0F;
bool isGrounded;
int moveSpeed = 15;
int jumpSpeed = 600;
void OnCollisionEnter2D (Collision2D col) {
if (col.gameObject.tag == "Ground") {
isGrounded = true;
}
}
void OnCollisionExit2D (Collision2D col) {
if (col.gameObject.tag == "Ground") {
isGrounded = false;
}
}
void OnTriggerEnter2D (Collider2D other) {
if(other.gameObject.tag == "Divider") {
Application.LoadLevel(Application.loadedLevel);
}
}
void FixedUpdate () {
if (Input.GetMouseButton(0)) {
if(rigidbody2D.velocity.x > -maxSpeed) {
rigidbody2D.AddForce(-Vector2.right * moveSpeed);
}
}
if (Input.GetMouseButton(1)) {
if(rigidbody2D.velocity.x < maxSpeed) {
rigidbody2D.AddForce(Vector2.right * moveSpeed);
}
}
if(Input.GetKeyDown(KeyCode.Space))
{
if (isGrounded) {
rigidbody2D.AddForce(Vector2.up * jumpSpeed);
}
}
}
}
Answer by StaNov · Sep 08, 2015 at 01:20 PM
You can tick "Freeze rotation" in settings of Rigid Body in Inspector. Then your object will not rotate around selected axis anymore.
Your answer
Follow this Question
Related Questions
Flip over an object (smooth transition) 3 Answers
i want to get my to cube to rotate 90 degrees on the x-axis as it jumps 0 Answers
Need Help Getting My Player To Jump. 1 Answer
Configurable Joints pass through colliders 2 Answers
Is it possible to swap my x Axis and y Axis on a rigidbody2D? 1 Answer