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 /
  • Help Room /
avatar image
0
Question by je66_b · Apr 05, 2017 at 05:03 PM · transform.positionraycasthit2d

trying to move child object on raycast check, object not moving

Hi, so i have a dash attack in my game and the dash distance is determined by a child game object X distance in front of the player object.. im trying to use a raycast hit to tell the dash object to move closer to the player object if it collides with a wall, i feel like im doing everything right but the result i want is not being achieved, the bool for collision with the wall is returning true and i have all my layers set the way theyre supposed to etc. the dash pos is just an empty game object.

if anyone has an alternative to solving this problem im all for it.. Thanks.

 private GameObject playerObject;
 private GameObject dashLocation;
 private Transform Player;
 
 
     //dashing
     public float dashSpeed;
     bool dashing;
 
 
     public bool wallhit;
 
 
 void Awake()
     {
         Player = this.GetComponent<Transform>();
         playerObject = GameObject.FindGameObjectWithTag("Player");
         dashPos = playerObject.transform.FindChild("dashPos");
         dashLocation = GameObject.FindGameObjectWithTag("dashPos");
     }
 
 void FixedUpdate()
     {
         Raycasting();
 
     }
 
 void Raycasting()
     {
 
         float wallDistance;
         float start = Player.transform.position.x;
         float end = dashPos.transform.position.x;
         Vector2 rayOrigin = new Vector2(Player.transform.position.x, Player.transform.position.y);
         Vector2 rayEnd = new Vector2(dashPos.transform.position.x, dashPos.transform.position.y);
         Vector2 rayDirection = new Vector2(Player.transform.localScale.x, 0);
 
         RaycastHit2D hit = Physics2D.Raycast(rayOrigin, rayDirection, 2.1f);
         //Debug.DrawLine(rayOrigin, rayEnd, Color.green);
 
         wallhit = Physics2D.Linecast(rayOrigin, rayEnd, 1 << LayerMask.NameToLayer("Walls"));
 
 
         //checks for walls, if wall is closer than dash position, move dash position closer to player
         if ((hit.collider.tag == "Walls"))
             {
                 wallDistance = hit.transform.position.x;
 
                 if (end >= wallDistance)
                 {
                     dashLocation.transform.position = new Vector2(wallDistance - 1f, Player.transform.position.y);
 
                 }
 
             }
         }
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by je66_b · Apr 05, 2017 at 06:40 PM

solved using an alternate method. removed child game object and just used distance.

 private void rayCasting()
     {
 
         rayOrigin = new Vector2(transform.position.x, transform.position.y);
         rayDirection = new Vector2(transform.localScale.x, 0);
 //the change i made
         rayCastDistance = 2.1f;
 //
         hit = Physics2D.Raycast(rayOrigin, rayDirection, rayCastDistance, 1 << LayerMask.NameToLayer("Walls"));
         if (hit)
         {
             maxDistance = hit.distance;
         }
         if (!hit)
         {
             maxDistance = rayCastDistance;
         }
 
     }    
Comment
Add comment · Share
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

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

99 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

Related Questions

Issue with character movement and collision using Physics2D.Raycast. 0 Answers

I can't get Raycasting in 2D to work, what am I missing? 1 Answer

Allow raycast to skip an object with rigidbody 0 Answers

Is there any way how to do raycasts like this? 0 Answers

two boxCasts casted from same point give different results 0 Answers


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