Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 AusieJamster · Apr 09, 2014 at 12:53 PM · raycastspriteraycasthit2d

RaycardHit2D not working on Sprites?

I've taken out some of the code so its easier to read but being as there is 2 tags one for unpassable terran and one for tall grass, ive put the tall grass as more of a 3d raycast so they dont interfere with eachother but the tall grass only checks once and stops :\

 using UnityEngine;
 using System.Collections;
 
 public class PlayerStats : MonoBehaviour {
 
     Vector2 startPoint;
     Vector2 endPoint;
     public float speed;
     private float increment = 0;
     bool isMoving;
 
     public static int walkCounter;
     int walkCounter2;
     bool isInCombat;
     bool ableMove;
     RaycastHit2D hit;
     
     Animator anim;
     
     public GameObject CameraMain;
     public GameObject CameraCombat;
 
     public GameObject[] teleportLoc;
     
     
     void Start() {
         startPoint = transform.position;
         endPoint = transform.position;
         
         anim = GetComponent<Animator>();
         
         walkCounter2 = Random.Range(5,15);
     }
 
     void Update () {
         Animations();
 
         if(increment <= 1 && isMoving){
             increment += speed/100;
         }
         else
             isMoving = false;
         
         if(isMoving)
             transform.position = Vector2.Lerp(startPoint, endPoint, increment);
 
         if (!isInCombat)
         {
             else if(Input.GetKey("a") && !isMoving)
             {
                 if (Physics2D.Raycast (transform.position, -Vector2.right, 1))
                 {
                     hit = Physics2D.Raycast (transform.position, -Vector2.right, 1);
                     if(hit.collider.tag == "Collision") {
                         ableMove = false;
                     }
                 }
 
                 if(ableMove)
                 {
                     calculateWalk();
                     increment = 0;
                     isMoving = true;
                     startPoint = transform.position;
                     endPoint = new Vector2(transform.position.x - 1, transform.position.y);
                 }
                 ableMove = true;
             }
         }
     }
     
     void calculateWalk () {
 
         if(Physics.Raycast(transform.position, Vector3.forward, 1))
         {
             walkCounter++;
             Debug.Log(walkCounter);
         }
 
         if(walkCounter >= walkCounter2) {
             walkCounter2 = Random.Range(5,15);
             walkCounter = 0;
             enterCombat();
         }
     }
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 jann1 · Apr 09, 2014 at 03:27 PM

Maybe your collider is set to trigger? Or the GameObject youre trying to hit isnt tagged correctly. Do you use a rigidbody2D instead of a rigidbody?

Comment
Add comment · Show 3 · 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
avatar image AusieJamster · Apr 09, 2014 at 05:39 PM 0
Share

I worked out it wasn't working probably because there was 2 different tagged sprites and they were interfering with each other. Do you know how i can fix that?

avatar image jann1 · Apr 10, 2014 at 02:57 PM 0
Share

Sorry I dont really understand what the problem is. $$anonymous$$aybe you can provide more context? Or you already have solved it yourself?

avatar image AusieJamster · Apr 11, 2014 at 04:30 PM 0
Share

2 objects with 2 different tags where both hitting the raycast when i wanted to pick which ones hit it. I just put them on layers and fixed it, thanks anyways.

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

22 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

Related Questions

Cutting a Sprite 0 Answers

Raycasting in 2D is not working 0 Answers

OnCollisionEnter2D is not working 0 Answers

0 Understanding of raycast2D commands 2 Answers

New UI ~ Check for mouse-hover 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