Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by zstanzbose · Jun 05, 2018 at 07:27 PM · movement2d gamephysics2dplayer movement

Need help with Raycast2D

Hi everyone, here's my setup. I have a 2D side-scrolling game where movement is determined by the position returned by a mouse click. I'm trying to use a Raycast 2D to determine if the tap is on a UI object or not. If the Raycast doesn't hit a UI object, then the click location is set as the target location and the player moves to that target. Right now, the character moves to the target when the layerMask is set to Everything, but no other setting. Also, I added a button and the player moves to the button location when it's clicked when it should be ignoring that click. If you could offer some advice on what the settings should be there, that would be wonderful. The player also doesn't stop when it's within range of the target. I messed with the distance tolerance and the player only stops if you click a target near it's current position while it's currently moving.

Here's my script: using System.Collections; using System.Collections.Generic; using UnityEngine;

public class YoloMates : MonoBehaviour {

 public Animator animator;
 public LayerMask layerMask;
 public Vector2 target = new Vector2();
 public Vector2 direction = new Vector2();
 public Vector2 position = new Vector2();
 public Vector2 movement = new Vector2();
 Rigidbody2D rb2D;
 public float speed = 2.0f;

 void Start()
 {
     animator = gameObject.GetComponent<Animator>();
     rb2D = GetComponent<Rigidbody2D>();
 }
 void FixedUpdate()
 {
     Vector3 m = Input.mousePosition;
     m = new Vector3(m.x, m.y, transform.position.z);
     Vector3 p = Camera.main.ScreenToWorldPoint(m);

     position = gameObject.transform.position;

     if (Input.GetMouseButtonDown(0))
     {
         Debug.Log("Click");
         RaycastHit2D hit = new RaycastHit2D();
         Ray2D ray2d = new Ray2D(new Vector2(p.x, p.y), Vector3.down);

         hit = Physics2D.Raycast(new Vector2(p.x, p.y), Vector3.down, Mathf.Infinity, layerMask);

         Debug.Log(hit.collider.gameObject.name);

         if (hit)
         {
             {
                 target = Camera.main.ScreenToWorldPoint(Input.mousePosition);
                 Debug.Log("Click");

                 if (target != Vector2.zero && (target - position).magnitude >= 1)
                 {
                     direction = (target - position).normalized;

                     movement.x = (direction.x * speed * Time.deltaTime);
                     movement.y = (direction.y * speed * Time.deltaTime);

                     rb2D.velocity = movement;

                     animator.SetBool("isMoving", true);
                     animator.SetFloat("moveX", direction.x);
                     animator.SetFloat("moveY", direction.y);
                 }
                 else
                 {
                     direction = Vector2.zero;
                     rb2D.velocity = Vector2.zero;
                     animator.SetBool("isMoving", false);
                 }
             }

         }
     }
 }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

139 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Need Help on Obj Movement via Mouse (particular physics rulles) 1 Answer

Is there a standardized way for creating a 2D ball physics movement method? 0 Answers

3D RPG - Player movement - He automatically finds the fastest way and goes around everything. How to limit his movement? 1 Answer

2D Character Jumps only 1 time 2 Answers

I am having problems with making my character face the movement direction after stopping. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges