- Home /
This question was
closed Aug 20, 2017 at 08:57 AM by
hexagonius for the following reason:
Unity answers is not about getting scripts fixed, it's about Unity
physicWalk.cs
hi can you fix my physicWalk script,when i click play button i just fall down in floor like broken leg and camera keep on floor
using UnityEngine; using System.Collections;
public class physicWalk : MonoBehaviour {
public static physicWalk instance;
//
public float speed = 7f;
public float maxSprintSpeed = 7f;
public float maxWalkSpeed = 4f;
public float force = 8f;
public float jumpSpeed = 5f;
public float footsFrequency = 0.7f;
//
public bool grounded = false;
private float fallingForce = 0f;
private CapsuleCollider collider;
private bool canJump = true;
private float canJumpCounter = 0f;
public AudioClip footstepSound;
public AudioClip fallSound;
void Start()
{
instance = this;
collider = gameObject.GetComponent< CapsuleCollider >();
}
// Don't let the Physics Engine rotate this physics object so it doesn't fall over when running
void Awake ()
{
speed = maxWalkSpeed;
}
public virtual float jump
{
get
{
if( Input.GetButton( "Jump" ) ) return 1f;
else return 0f;
}
}
public virtual float horizontal
{
get
{
float v = Input.GetAxis( "Horizontal" );
return v * force;
}
}
public virtual float vertical
{
get
{
float v = Input.GetAxis( "Vertical" );
return v * force;
}
}
float fr = 0f;
void Update()
{
{
fr += Time.deltaTime;
if( Input.GetButton( "Sprint" ) )
{
fr += Time.deltaTime*0.5f;
}
while( fr >= footsFrequency )
{
fr = 0f;
playFootstepSound();
}
}
if( Input.GetButton( "Sprint" ) )
{
speed = maxSprintSpeed;
}
else speed = maxWalkSpeed;
}
public void playFootstepSound()
{
}
void FixedUpdate ()
{
///Jump iteration
if( !canJump )
{
canJumpCounter += Time.fixedDeltaTime;
if( canJumpCounter >= 1f )
{
canJump = true;
canJumpCounter = 0f;
}
}
////Ground test
RaycastHit hit;
Vector3 tmpV = transform.position;
tmpV.y += 0.1f;
if (Physics.Raycast( tmpV, -Vector3.up, out hit, 0.3f))
{
if( hit.collider.tag == "GROUND" )
{
grounded = true;
}
else
{
grounded = false;
}
}
else
{
grounded = false;
}
////
{
}
{
Vector3 forceV = Vector3.Cross( hit.normal, Vector3.Cross( transform.forward, hit.normal ) );
forceV = forceV.normalized;
}
if( jump != 0f && grounded && canJump )
{
canJump = false;
Vector3 tmp = Vector3.up * jumpSpeed + ( transform.forward * vertical * 0.1f );
}
if( !grounded )
{
fallingForce = fallingForce + Time.fixedDeltaTime * 5f;
}
else
{
fallingForce -= (Time.fixedDeltaTime * 10f) + (fallingForce * 0.3f);
if( fallingForce < 0f ) fallingForce = 0f;
}
}
void OnCollisionEnter ( Collision other )
{
if( other.collider.tag == "GROUND" )
{
if( other.relativeVelocity.y >= 2f )
{
physicWalk_MouseLook.instance.wobble( 0f, other.relativeVelocity.y * 2f, 0f, other.relativeVelocity.y * 2f );
Vector3 tmpPosMod = Camera.main.transform.position;
tmpPosMod.y -= other.relativeVelocity.y * 0.15f;
physicWalk_MouseLook.instance._camPos.position = tmpPosMod;
}
}
}
}
Comment
Follow this Question
Related Questions
What is the error? 2 Answers
What does the "compute mesh inertia tensor failed" error mean? 6 Answers
Unity Script causing crash 1 Answer
physics rolling error( included sample and simple demo) 0 Answers
Lynda Tutroial projectile error 0 Answers