I'm looking for some help! I'm trying to add a dot product and a raycast to my lunar lander. The lander is supposed to detect the platform that I created and, if the lander doest land flat, I what to call and explosion animation, if = true to play a sound. I'm confused about how to do that. Here is my script. using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class LunarLanderControl : MonoBehaviour { public GameObject GameManagerGO; //Reference to our game manager public GameObject Explosion; //Explosion Animation public Text LivesUIText; //Reference to lives ui text const int MaxLives = 3; //reference to the player lives int lives; //current player lives public float maxSpeed = 2f; // max speed public float rotationSpeed = 180f; //rotation speed public void Init() { lives = MaxLives; // player lives LivesUIText.text = lives.ToString();//update the lives UI text gameObject.SetActive(true); // Set this player game object to active } // Start is called before the first frame update void Start() { } // Update is called once per frame void Update() { Quaternion rotation = transform.rotation; float z = rotation.eulerAngles.z; z += Input.GetAxis("Horizontal"); rotation = Quaternion.Euler(0, 0, z); transform.rotation = rotation; //Move the lander Vector3 pos = transform.position; Vector3 velocity = new Vector3(0, Input.GetAxis("Vertical") * maxSpeed * Time.deltaTime, 0); pos += rotation * velocity; transform.position = pos; } // Needs to land flat(relatively) and on flat surfaces bool CanLand2() { // Get the hit from a raycasts that starts at the lander and goes negative along the world y axis 100 units RaycastHit2D hit = Physics2D.Raycast(transform.position, -transform.up, 100.0f); // Draw line Debug.DrawLine(transform.position, transform.position + 100.0f * -transform.up, Color.green); // Draw normal line Debug.DrawLine(hit.point, hit.point + 50 * hit.normal, Color.red); // Log the dot product //Debug.Log(Vector2.Dot(hit.normal, -transform.up)); return Vector2.Dot(hit.normal, -transform.up) > -1 && Vector2.Dot(hit.normal, -transform.up) < -.999f ? true : false; // ternary operator } //Function returns true if landing on flat surface, false if not. void OnTriggerEnter2D(Collider2D collision)//collisions references { if (collision.tag == "MountainTag") PlayExplosion();//ExplosionAnimation lives--; //Subtract one life LivesUIText.text = lives.ToString(); // update lives UI text if (lives == 0) //Game Over { //Change the game manager to game over state GameManagerGO.GetComponent<GameManager>().SetGameManagerState(GameManager.GameManagerState.GameOver); gameObject.SetActive(false); //hide the moon lander } //Function to instantiate an explosion void PlayExplosion() { GameObject explosion = (GameObject)Instantiate(Explosion); //set the position of the explosion explosion.transform.position = transform.position; } } }
You should use code tags to format your code in your posts: https://forum.unity.com/threads/using-code-tags-properly.143875/ Dot product returns a number between -1 and 1. https://docs.unity3d.com/ScriptReference/Vector2.Dot.html Your ship needs to be upright to land. You're comparing the ground normal pointing up, with the ship's Y axis pointing down (your comment says world Y axis, but it's local to the ship's transform). In a perfect landing, they will be opposite and return a -1. So if you require your ship to be within 45 degrees of that, you can check if the dot product is less than -0.5f.