Question by
Gohst002 · Jun 03, 2017 at 11:53 AM ·
gravityfps controllerfpstutorialclimb
character climb
I looked at a tutorial on how to make a basic FPS controller and coud not figure out how to make the character climb a wall when he jumps against it and it has the tag JumpBoost. using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerMovement : MonoBehaviour {
public float speed = 2f;
public float sens = 2f;
public float jumpForce = 4f ;
public float gravity;
public float climbForce;
public Rigidbody rB;
private Collider other;
CharacterController player;
public GameObject eyes;
float moveFB, moveLR, rotX, rotY, verticalVelocity;
private bool hasJumped, isCrouched;
// Use this for initialization
void Start () {
Cursor.visible = false;
Cursor.lockState = CursorLockMode.Locked;
player = GetComponent<CharacterController> ();
other = GetComponent<Collider> ();
}
// Update is called once per frame
void Update () {
Movement ();
if (Input.GetButtonDown ("Jump"))
{
hasJumped = true;
}
ApplyGravity ();
if (Input.GetButtonDown ("Crouch"))
{
if (isCrouched == false)
{
isCrouched = true;
player.height = player.height / 2;
}
}
if (Input.GetButtonUp ("Crouch"))
{
if (isCrouched == true)
{
isCrouched = false;
player.height = player.height * 2;
}
}
}
public void Movement()
{
moveFB = Input.GetAxis ("Vertical") * speed;
moveLR = Input.GetAxis("Horizontal") * speed;
rotX = Input.GetAxis ("Mouse X") * sens;
rotY = Input.GetAxis ("Mouse Y") * sens;
Vector3 movement = new Vector3 (moveLR,verticalVelocity,moveFB);
Vector3 climb = new Vector3 (0, climbForce, 0);
transform.Rotate (0,rotX,0);
movement = transform.rotation * movement;
climb = transform.position.y * climb;
player.Move (movement * Time.deltaTime);
player.Move (climb * Time.deltaTime);
eyes.transform.Rotate (-rotY, 0, 0);
if (Input.GetKeyDown (KeyCode.LeftShift))
{
speed = 7f;
}
if (Input.GetKeyUp (KeyCode.LeftShift))
{
speed = 4f;
}
}
private void ApplyGravity()
{
if (player.isGrounded == true)
{
if (hasJumped == false)
{
verticalVelocity = Physics.gravity.y * gravity;
}
else
{
verticalVelocity = jumpForce;
}
}
else
{
verticalVelocity += Physics.gravity.y * gravity * Time.deltaTime;
verticalVelocity = Mathf.Clamp (verticalVelocity, -50f, jumpForce);
hasJumped = false;
}
if (player.isGrounded == false)
{
if (other.GetComponent<Collider> ().tag == "JumpBoost" && hasJumped == true)
{
Vector3 climb = new Vector3 (0,climbForce,0);
}
}
}
}
I tried making a climb Vector3 but my player always bugs out and gets hit by gravity when he hits the object.
Comment
Your answer
Follow this Question
Related Questions
Changing gravity at the click of a button... 5 Answers
simple roll a ball game - ball slips threw ground 1 Answer
Spheres change form on collision 0 Answers
How to make GameObject fall with the same gravity while using AddForce() 1 Answer
[Kudan AR] (Markerless) Augmented Reality and Rigidbodies 0 Answers