Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

My hitbox doesn't work

Discussion in 'Scripting' started by nguillermin01, Feb 6, 2020.

  1. nguillermin01

    nguillermin01

    Joined:
    Dec 30, 2019
    Posts:
    16
    Hi! I have a very big problem. My hitbox doesn't work. I created an floating island with blender.
    And I created a player (capsule). My player has a capsule collider, a Character controller and two script who I made and my floating island have a mesh collider but the player fall like a ghost.

    "Mouse Look"
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class MouseLook : MonoBehaviour
    6. {
    7.     // Public variables
    8.     public float MouseSensivity;
    9.     public bool RotateX;
    10.     public bool RotateY;
    11.     // Private variables
    12.     private Transform PlayerBody;
    13.     private float MouseX;
    14.     private float MouseY;
    15.  
    16.     // Start is called before the first frame update
    17.     void Start()
    18.     {
    19.         PlayerBody = this.transform;
    20.     }
    21.  
    22.     // Update is called once per frame
    23.     void Update()
    24.     {
    25.         if (RotateX == true)
    26.         {
    27.             MouseX = Input.GetAxis("Mouse X") * Time.deltaTime;
    28.             PlayerBody.Rotate(Vector3.up * MouseSensivity * MouseX);
    29.         }
    30.  
    31.         if (RotateY == true)
    32.         {
    33.             MouseY = Input.GetAxis("Mouse Y") * Time.deltaTime;
    34.             PlayerBody.Rotate(Vector3.left * MouseSensivity * MouseY);
    35.         }
    36.  
    37.         // transform.eulerAngles.x = Mathf.Clamp(transform.eulerAngles.x, -90f, 90f)
    38.     }
    39. }
    40.  
    And "Player Movements"
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerMovements : MonoBehaviour
    6. {
    7.     // Public variables
    8.     public float Speed;
    9.     public float HeighJump;
    10.     // Private variables
    11.     private Transform PlayerBody;
    12.     private Vector3 Movements;
    13.     private bool CanJump;
    14.  
    15.     // Start is called before the first frame update
    16.     void Start()
    17.     {
    18.         PlayerBody = this.transform;
    19.     }
    20.  
    21.     // Update is called once per frame
    22.     void Update()
    23.     {
    24.         Movements = new Vector3(Input.GetAxis("Horizontal"), Movements.y, Input.GetAxis("Vertical"));
    25.  
    26.         PlayerBody.Translate(0, -0.1f, 0);
    27.  
    28.         Movements.y += Physics.gravity.y * Time.deltaTime;
    29.         PlayerBody.Translate(Movements * Speed * Time.deltaTime);
    30.     }
    31.  
    32.     void OnCollisionEnter(Collision Col)
    33.     {
    34.         if (Col.gameObject.tag == "Platforms")
    35.         {
    36.             if (Input.GetButtonDown("Jump"))
    37.             {
    38.                 Movements.y = HeighJump;
    39.             }
    40.         }
    41.     }
    42. }
    43.  
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,748
    Does the floating island have a Rigidbody (on the same object as the collider)?
     
  3. nguillermin01

    nguillermin01

    Joined:
    Dec 30, 2019
    Posts:
    16
    No it isn't
     
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,748
    It needs to. That's your problem.
     
  5. nguillermin01

    nguillermin01

    Joined:
    Dec 30, 2019
    Posts:
    16
    I added a rigidbody on my island but it's work still not.
     
  6. PaulR

    PaulR

    Joined:
    Nov 14, 2012
    Posts:
    43
    Usually caused by one of:
    1- you have one or more colliders set as trigger
    2- you have one or more objects using non-uniform scaling
    3- you have one or more rigidbodies set as kinematic
    4- you’re modifying the transform directly instead of the RigidBody.transform

    From the code you posted it looks like 4
     
  7. nguillermin01

    nguillermin01

    Joined:
    Dec 30, 2019
    Posts:
    16
    I didn't understand the mistake n ° 4. What does the code look like? I need example
     
  8. nguillermin01

    nguillermin01

    Joined:
    Dec 30, 2019
    Posts:
    16
    I have to re-up my topic because my problem is an emergency.
    I can't do anything until it has been fixed.
     
  9. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,748
    On line 26 & 29, you're setting the .position of the Transform (PlayerBody). The Transform has no conception of the physics system, and thus it has no way of knowing there's a collider in the way.

    If you make PlayerBody type Rigidbody (on line 18 you'll have to change that to .GetComponent<Rigidbody>() ), then you can use MovePosition instead Translate. Because Rigidbody does know about physics, it will move towards the given position, but won't move through colliders. Note that .MovePosition takes a position to move towards, so you'll have to do something like:
    Code (csharp):
    1. //instead of:
    2. PlayerBody.Translate(0f, -0.1f, 0f);
    3. //use:
    4. PlayerBody.MovePosition(PlayerBody.position + new Vector3(0f, -0.1f, 0f) );
    For your second one: .Translate takes into account the transform's rotation, so you'll probably have to do that for your movement:
    Code (csharp):
    1.         PlayerBody.MovePosition(PlayerBody.position + PlayerBody.transform.rotation * Movements * Speed * Time.deltaTime);
     
  10. nguillermin01

    nguillermin01

    Joined:
    Dec 30, 2019
    Posts:
    16
    I made this but it's work still not. They are compiler error.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerMovements : MonoBehaviour
    6. {
    7.     // Public variables
    8.     public float Speed;
    9.     public float HeighJump;
    10.     // Private variables
    11.     private Rigidbody PlayerBody;
    12.     private Vector3 Movements;
    13.  
    14.     // Start is called before the first frame update
    15.     void Start()
    16.     {
    17.         PlayerBody.GetComponent<Rigidbody>();
    18.     }
    19.  
    20.     // Update is called once per frame
    21.     void Update()
    22.     {
    23.         Movements = new Vector3(Input.GetAxis("Horizontal"), Movements.y, Input.GetAxis("Vertical"));
    24.  
    25.         Movements.y += Physics.gravity.y * Time.deltaTime;
    26.         PlayerBody.MovePosition(PlayerBody.position + new Vector3(Movements * Speed * Time.deltaTime));
    27.     }
    28.  
    29.     // test if
    30.     void OnCollisionEnter(Collision Col)
    31.     {
    32.         if(Col.gameObject.tag == "Platforms")
    33.         {
    34.             if(Input.GetButtonDown("Jump"))
    35.             {
    36.                 Movements.y = HeighJump;
    37.             }
    38.         }
    39.     }
    40. }
    41.  
    Can you help me please?
     
  11. nguillermin01

    nguillermin01

    Joined:
    Dec 30, 2019
    Posts:
    16
    Sorry but I still have to re-up my topic because my problem is an emergency.
    I can't do anything until it has been fixed.

    If you want help me, I advise you to show the script full. Else, I will not know.
     
  12. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,748
    What is the compiler error?
     
  13. nguillermin01

    nguillermin01

    Joined:
    Dec 30, 2019
    Posts:
    16
    I don't know...
     
  14. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,386
    If you have compiler errors then there will be messages in the console.