- Home /
Question by
Aslan_Sumankalpa · Feb 11, 2021 at 05:08 PM ·
rigidbodytransformproblem during runtimemovements
How do I fix my FPS movement Script?
So, I followed a tutorial for the movement and the looking, then I applied sliding, with this code:
//Sliding if(Input.GetKey(KeyCode.LeftControl)) { transform.localScale = new Vector3 (1, slideHeight, 1); }else { transform.localScale = new Vector3 (1, normalHeight, 1); }
But then, I can't jump, and the layer mask function (for finding out if there is ground beneath the player) doesn't work. I tried deleting the sliding code, but it still didn't work.
Full code:
private Rigidbody rb;
public LayerMask layerMask;
public bool grounded = true;
public bool isJumpBoost = false;
public float moveSpeed = 6;
public float slideHeight = 0.5f;
public float normalHeight = 1.5f;
public float normalJump = 10f;
public float jumpForce = 10;
public float boostJump = 30f;
public float maxFall = -55;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
float x = Input.GetAxisRaw("Horizontal") * moveSpeed;
float y = Input.GetAxisRaw("Vertical") * moveSpeed;
grounded = Physics.CheckSphere(new Vector3(transform.position.x, transform.position.y - 1, transform.position.z), 0.4f, layerMask);
if(Input.GetKeyDown(KeyCode.Space) && grounded)
rb.velocity = new Vector3(rb.velocity.x, jumpForce, rb.velocity.z);
Vector3 movePos = transform.right * x + transform.forward * y;
Vector3 newMovePos = new Vector3(movePos.x, rb.velocity.y, movePos.z);
rb.velocity = newMovePos;
if(gameObject.transform.position.y <= maxFall)
{
Debug.Log("Player is falling");
Time.timeScale = 0f;
//Lose();
}
//Sliding
if(Input.GetKey(KeyCode.LeftControl))
{
transform.localScale = new Vector3 (1, slideHeight, 1);
}else
{
transform.localScale = new Vector3 (1, normalHeight, 1);
}
}
//Collision Detection
void OnCollisionEnter(Collision col)
{
if(col.collider.name == "jumpBoost")
{
jumpForce = boostJump;
Debug.Log("Player gets a jump boost");
isJumpBoost = true;
}else
{
jumpForce = normalJump;
Debug.Log("No jump boost now");
isJumpBoost = false;
}
}
//void Lose() this is a function
//{
//}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613024957im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Intercepting Alghorithm not wok properly 1 Answer
Why is the if condition returning true ? 0 Answers
Flinging Wall Death 1 Answer
Anti-Gravity vehicle bounce-back help. 1 Answer
My character moves under the terrain at strange Y position 1 Answer