Hey i am having a trouble with my character.
Every time it touches another object with a collider on it it starts vibrating like crazy and sometimes even fall on his back.
How do i fix this ?
Here’s the code:
using UnityEngine;
using System.Collections;
public class PlayerControl : MonoBehaviour
{
bool grounded, interact; //bool for checking if player is grounded so they can jump, and bool for interact, so that player can only interact when in range of thing to interact with
public Transform JumpCheck, GroundCheck; //transform variable for the end points of the linecasts
RaycastHit2D interacted; //a variable type that stores a collider that was hit during linecast
public float speed = 6.0f;
float jumpTime, jumpDelay = .3f;
bool jumped;
Animator anim;
void Start()
{
anim = GetComponent<Animator>();
}
void Update()
{
Movement(); //call the function every frame
RaycastStuff(); //call the function every frame
}
void RaycastStuff()
{
//Just a debug visual representation of the Linecast, can only see this in scene view! Doesn't actually do anything!
Debug.DrawLine(transform.position, JumpCheck.position, Color.magenta);
Debug.DrawLine(transform.position, GroundCheck.position, Color.magenta);
//we assign the bool 'ground' with a linecast, that returns true or false when the end of line 'jumpCheck' touches the ground
grounded = Physics2D.Linecast (transform.position, GroundCheck.position, 1 << LayerMask.NameToLayer ("Ground"));
}
void Movement() //function that stores all the movement
{
if (Input.GetKey("left ctrl")) {
anim.SetBool("IsAttacking",true);
}
else
{
anim.SetBool("IsAttacking",false);
}
if (Input.GetKey ("right") || Input.GetKey ("left")) {
anim.SetBool ("IsRunning", true);
} else {
anim.SetBool ("IsRunning", false);
}
if(Input.GetAxisRaw("Horizontal") > 0)
{
transform.Translate(Vector3.right * speed * Time.deltaTime);
transform.eulerAngles = new Vector2(0, 0); //this sets the rotation of the gameobject
}
if(Input.GetAxisRaw("Horizontal") < 0)
{
transform.Translate(Vector3.right * speed * Time.deltaTime);
transform.eulerAngles = new Vector2(0, 180); //this sets the rotation of the gameobject
}
if(Input.GetKeyDown (KeyCode.Space) && grounded) // If the jump button is pressed and the player is grounded then the player jumps
{
GetComponent<Rigidbody2D>().AddForce(transform.up * 300f);
jumpTime = jumpDelay;
anim.SetTrigger("Jump");
jumped = true;
}
jumpTime -= Time.deltaTime;
if(jumpTime <= 0 && grounded && jumped)
{
anim.SetTrigger("Land");
jumped = false;
}
}
}