Question by
yogeshbhatt · Aug 26, 2019 at 12:10 PM ·
transformscript.blender
Player floats in air when i enter play mode
I don't think there is any problem in my code. For x reason whenever i enter play mode the player gets some position added to its y-axis and doesn't fall. Use gravity is on. Is kinematic is off. I cant find any problem with the asset in blender but this shouldn't be a concern because the player changes its position on its own.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RunScript : MonoBehaviour
{
public Rigidbody rb;
Animator animator;
public float moveSpeed = 7f;
bool canMove = false;
bool canJump = false;
//Initializes animator;
void Start()
{
animator = GetComponent<Animator>();
}
//Sets canJump to true/false
private void OnCollisionEnter()
{
canJump = true;
}
private void OnCollisionExit(){
canJump = false;
}
void FixedUpdate()
{
float input= Input.GetAxisRaw("Horizontal");
canMove= input!= 0 ? true: false;
//Enables running based on following condition
if (canMove && !Input.GetKey(KeyCode.UpArrow)) {
animator.SetBool("canRun", true);
transform.eulerAngles = input > 0 ? Vector3.up * 90 : -Vector3.up * 90;
transform.Translate((input > 0 ? -transform.right : transform.right) * moveSpeed * Time.deltaTime);
}
else
{
animator.SetBool("canRun", false);
}
//OnPositionJump
if (Input.GetKey(KeyCode.Space) && canJump)
{
animator.SetBool("canJump", true);
}
else
{
animator.SetBool("canJump", false);
}
}
}
Comment
Your answer