Search Unity

axes throwing game: can stick to the wall, but can not make not stick if it dose not hit the head

Discussion in 'Scripting' started by Jaylovegame, Jun 21, 2018.

  1. Jaylovegame

    Jaylovegame

    Joined:
    Jun 21, 2018
    Posts:
    2
    Hello

    I super new at C# scripting. So iam making a throw the axe game. I found a script that can get the axe to go into the wall if it hit the wall hard. ill put it bellow:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class WallStick : MonoBehaviour
    6. {
    7.     public Collider sharpEnd;
    8.  
    9.     private Rigidbody rigidbody;
    10.  
    11.     private void Start()
    12.     {
    13.         rigidbody = GetComponent<Rigidbody>();
    14.     }
    15.  
    16.     private void OnCollisionEnter(Collision collision)
    17.     {
    18.         foreach (ContactPoint contactPoint in collision.contacts)
    19.         {
    20.             // are we hitting with the pointy bit?
    21.             if(contactPoint.thisCollider == sharpEnd)
    22.             {
    23.                 // is it going fast enough?
    24.                 if (rigidbody.velocity.magnitude >= 20)
    25.                 {
    26.                     // embed it
    27.                     transform.SetParent(contactPoint.otherCollider.transform.root, true);
    28.  
    29.                 }
    30.             }
    31.         }
    32.     }
    33. }
    and i have a way to throw the axes. (it just to throw the axe at the wall) it set to throw where a camera pointing. also below:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class AxeThrow : MonoBehaviour {
    6.  
    7.     public GameObject ThrowablePrefab;
    8.     public GameObject AxeCamera;
    9.  
    10.     //how far the axe is throw
    11.     public float axeDistance = 2f;
    12.     // how hard it throw
    13.     public float axeThrowingForce = 5f;
    14.  
    15.  
    16.     //if axe is in players hand
    17.     private bool HoldingAxe = true;
    18.     // Use this for initialization
    19.     void Start () {
    20.         ThrowablePrefab.GetComponent<Rigidbody> ().useGravity = false;
    21.     }
    22.    
    23.     // Update is called once per frame
    24.     void Update () {
    25.         if (HoldingAxe) {
    26.             ThrowablePrefab.transform.position = AxeCamera.transform.position + AxeCamera.transform.forward * 2;
    27.         }
    28.         if (Input.GetMouseButtonDown(0)) {
    29.             Debug.Log ("throwing");
    30.             HoldingAxe = false;
    31.             ThrowablePrefab.GetComponent<Rigidbody> ().useGravity = true;
    32.             ThrowablePrefab.GetComponent<Rigidbody> ().AddForce( AxeCamera.transform.forward * axeThrowingForce);
    33.             ThrowablePrefab.GetComponent<Rigidbody> ().AddTorque(transform.right * 10000);
    34.     }
    35. }
    36. }
    37.  
    The problem is the script I made allow everything to stick into the wall. I want to make it so that only when the blade hit the wall it, the axe stick to wall. If the handle or the back of the axe's head hit the wall, i want it to bonce off. I tried to make the axe head and the handle two different object, but the head would fly off during flight. ( I try playing with locking the X,Y,Z and it would keep the axe's head upright and not moving with the axe.) I also try to make it a prefab and the axe's head still did not want to follow the handle.

    So is there a way to keep the axe a single gameobject and ether script or put something in invisible on the axe's blade; that will only let the axe stick the wall if the blade hit the wall? (sorry for the crap English XP)
     

    Attached Files:

    • ath.jpg
      ath.jpg
      File size:
      111.6 KB
      Views:
      763
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,531
    Put a collider on the head of the axe only in the region where the blade is.

    If that hits, then stick.
     
  3. Jaylovegame

    Jaylovegame

    Joined:
    Jun 21, 2018
    Posts:
    2
    Thank for bring that idea up, but iam not sure how can add a script to collider?