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

I Don't Know How To Use Raycast?

Discussion in 'Scripting' started by False_Baconator, Nov 25, 2019.

  1. False_Baconator

    False_Baconator

    Joined:
    Oct 2, 2018
    Posts:
    35
    Hi, I'm trying to make a 2D game in which the player jumps from falling platform to falling platform. in order to make the jump, i'm trying to have it so that when you click on the screen, it will use the players position and the mouses position in order to create a ray, and then move the player(with the Ignore Raycast tag) to whatever collider collides with the ray.

    When testing i found that the ray refuses to take the player to a negative coordinate, and even if i try to take it into the positive coordinates, the coordinates will be slightly higher than they should.

    here's my code

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Player_Move : MonoBehaviour {
    6.  
    7.  
    8.     private Collider2D Move_Target;
    9.  
    10.  
    11.     private string Platform_Tag;
    12.  
    13.     private Rigidbody2D rb;
    14.  
    15.     private Vector2 Mouse_Pos;
    16.  
    17.     private RaycastHit2D Move_Ray;
    18.  
    19.     void Move()
    20.     {
    21.         Mouse_Pos = Input.mousePosition;
    22.  
    23.         Debug.Log(Mouse_Pos);
    24.  
    25.         Move_Ray = Physics2D.Raycast(transform.position, Mouse_Pos);
    26.  
    27.  
    28.  
    29.         Move_Target = Move_Ray.collider;
    30.  
    31.         Debug.Log(Move_Target);
    32.  
    33.         transform.position = Move_Ray.point;
    34.  
    35.  
    36.  
    37.     }
    38.    
    39.  
    40.     // Use this for initialization
    41.     void Start () {
    42.         rb = GetComponent<Rigidbody2D>();
    43.     }
    44.    
    45.     // Update is called once per frame
    46.     void Update () {
    47.  
    48.  
    49.         if (Input.GetButtonDown("Jump"))
    50.         {
    51.             Move();
    52.         }
    53.     }
    54. }
    55.  
     
  2. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    1,315
    Input.mousePosition is in screen space. Objects in the world are in world space. You need to convert screen space to world space in order to use it with Raycast.

    You can do this using methods on the camera, such as the ScreenToWorldPoint method.
     
  3. False_Baconator

    False_Baconator

    Joined:
    Oct 2, 2018
    Posts:
    35
    Thanks you so much

    now I just have to find out how to change the object it landed on's tag to Ignore Raycast as well.


    transform.parent = Move_Target.transform;

    transform.parent.tag = "Ignore Raycast"; // <- "tag.Ignore Raycast is not defined." it should be defined by default




    Edit: Nvm It's supposed to be a layer, i'm retarded.
     
    Last edited: Nov 26, 2019