Menú

Mostrar Mensajes

Esta sección te permite ver todos los mensajes escritos por este usuario. Ten en cuenta que sólo puedes ver los mensajes escritos en zonas a las que tienes acceso en este momento.

Mostrar Mensajes Menú

Temas - Evilhack

#1
He hecho un script de el comportamiento del jugador y me esta dando muchos problemas.El jugador se pega a las paredes, da doble salto a veces porque si, y en ciertas zonas no se puede ni mover ni saltar podrían arreglarlo?
 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerBehaviour : MonoBehaviour {
 
private Rigidbody rigidbody;
public Vector2 jumpForce = new Vector2 (0, 400);
public float maxspeed = 3.0f;
public float speed = 50.0f;
private float xMove;
private bool canJump;
private bool onTheground;
private float yPrevious;
private bool collidingWall;
public const int maxHealth = 100;
public int currentHealth = maxHealth;
 

void Start () {
   rigidbody = GetComponent<Rigidbody> ();   
   canJump = false;
   xMove = 0.0f;
   onTheground = false;
   yPrevious = Mathf.Floor (transform.position.y);
   collidingWall = false;   
}


void Update () {
   CheckOnTheGround ();
   Jumping ();
}
 
void FixedUpdate(){
   
   Movement ();
   
   Camera.main.transform.position = new Vector3(
      this.transform.position.x,
      this.transform.position.y,
      Camera.main.transform.position.z
   );
}
 
void Movement(){
   xMove = Input.GetAxis ("Horizontal");
   if (collidingWall && !onTheground) {
      xMove = 0;
   }
 

   if (xMove != 0) {
      float xSpeed = Mathf.Abs (xMove * rigidbody.velocity.x);
      
      if (xSpeed < maxspeed) {
         Vector3 movementForce = new Vector3 (1, 0, 0);
         movementForce *= xMove * speed;
            
         RaycastHit hit;
         if (!rigidbody.SweepTest (movementForce, out hit, 0.05f)) {
            rigidbody.AddForce (movementForce);
         }
      }
      
      if (Mathf.Abs (rigidbody.velocity.x) > maxspeed) {
         Vector3 newVelocity;
         newVelocity.x = Mathf.Sign (rigidbody.velocity.x) * maxspeed;
         newVelocity.y = rigidbody.velocity.y;
         newVelocity.z = 0;
         rigidbody.velocity = newVelocity;
      }
   } else {
         Vector3 newVelocity = rigidbody.velocity;
         newVelocity.x *= 0.9f;
         rigidbody.velocity = newVelocity;
      }  
   
   }
 
void Jumping(){
   if (Input.GetButtonDown ("Jump")) {
      canJump = true;
   }
   if (canJump && onTheground) {
      rigidbody.AddForce (jumpForce);
      canJump = false;
   
   }
}
void CheckOnTheGround(){
   
   float distance = (GetComponent<CapsuleCollider> ()).height / 2 * this.transform.localScale.y + 1.5f;
   
   Vector3 floorDirection = transform.TransformDirection (-Vector3.up);
   
   Vector3 origin = transform.position;
   
   if (!onTheground) {
      
      if (Physics.Raycast (origin, floorDirection, distance)) {
         onTheground = true;
      }
   } else if (Mathf.Floor (transform.position.y) != yPrevious) {
      onTheground = false;
   
   }
   
   yPrevious = Mathf.Floor (this.transform.position.y);   
}
 
void OnDrawGizmos(){
   Debug.DrawLine (transform.position, transform.position + rigidbody.velocity, Color.red);
}
void OnCollisionStay(Collision collision){
   if (!onTheground) {
      collidingWall = true;
   }
}
void OnCollisionExit(Collision collision){
   collidingWall = false;
}
   
   
public void TakeDamage(int amount){
   currentHealth -= amount;
   if (currentHealth <= 0){
   currentHealth = 0;

   Debug.Log("Dead!");
   }
}
}

 
Gracias por leer