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 Selevan · Jun 22, 2013 at 01:50 PM · javascriptcollisionmovementraycast

Physics.Raycast.hit not working properly with moving chars...

So, i am making kind of a roguelike game with speed/turn based movement, and i use raycast.hit for every char to check if there is something at the position the specific char wants to move to. And if there is, then do not move there.

Gave a random movement for npcs, and if they bump into wall, other npc, everything works.

But sometimes when npc decides to move at the same spot i picked, he does it, and therfor we end at the same position. (Doesnt always happen. Sometimes he bumps into me, and sometimes not.)

It may be something with us both moving at the same moment, but it only happens with player char <-> npc char. Heres the player movement script, npc movement script, and the game manager script. I've only started scripting few days ago, so nevermind beginner mistakes. Any idea ?

 public var speed : int;
 function Update () 
 {
     if (Input.GetKeyDown (KeyCode.Keypad5))
           {
           Move(0,0);
           }
      if (Input.GetKeyDown (KeyCode.Keypad8))
           {
         var hit : RaycastHit;
         if (Physics.Raycast (transform.position, Vector3(0, 0, 1), hit, 1))
             {
             if (hit.collider.tag == "Wall")
                 {
                 Debug.Log("Walnelismy w sciane z góry");
                 }
             else if (hit.collider.tag == "Enemy")
                 {
                 Debug.Log("we bumped into some enemy.. would of attack him if it wasn't a prealpha version of the game!");
                 }
             }
             else
                 {
                 Move(0,1.4);
                 }
           }
       if (Input.GetKeyDown (KeyCode.Keypad6))
           {
         if (Physics.Raycast (transform.position, Vector3(1, 0, 0), hit, 1))
             {
             if (hit.collider.tag == "Wall")
                 {
                 Debug.Log("Walnelismy w sciane z prawej");
                 }
             else if (hit.collider.tag == "Enemy")
                 {
                 Debug.Log("we bumped into some enemy.. would of attack him if it wasn't a prealpha version of the game!");
                 }
             }
             else
                 {
                 Move(1,0);
                 }
           }
      if (Input.GetKeyDown (KeyCode.Keypad2))
           {
         if (Physics.Raycast (transform.position, Vector3(0, 0, -1), hit, 1))
             {
             if (hit.collider.tag == "Wall")
                 {
                 Debug.Log("Walnelismy w sciane z dołu");
                 }
             else if (hit.collider.tag == "Enemy")
                 {
                 Debug.Log("we bumped into some enemy.. would of attack him if it wasn't a prealpha version of the game!");
                 }
             }
             else
                 {
                 Move(0,-1.4);
                 }
           }
     if (Input.GetKeyDown (KeyCode.Keypad4))
           {
         if (Physics.Raycast (transform.position, Vector3(-1, 0, 0), hit, 1))
             {
             if (hit.collider.tag == "Wall")
                 {
                 Debug.Log("Walnelismy w sciane z lewej");
                 }
             else if (hit.collider.tag == "Enemy")
                 {
                 Debug.Log("we bumped into some enemy.. would of attack him if it wasn't a prealpha version of the game!");
                 }
             }
             else
                 {
                 Move(-1,0);
                 }
           }
     if (Input.GetKeyDown (KeyCode.Keypad9))
           {
         if (Physics.Raycast (transform.position, Vector3(1, 0, 1.4), hit, 1))
             {
             if (hit.collider.tag == "Wall")
                 {
                 Debug.Log("Walnelismy w sciane z góra-prawo");
                 }
             else if (hit.collider.tag == "Enemy")
                 {
                 Debug.Log("we bumped into some enemy.. would of attack him if it wasn't a prealpha version of the game!");
                 }
             }
             else
                 {
                 Move(1,1.4);
                 }
           }
     if (Input.GetKeyDown (KeyCode.Keypad3))
           {
         if (Physics.Raycast (transform.position, Vector3(1, 0, -1.4), hit, 1))
             {
             if (hit.collider.tag == "Wall")
                 {
                 Debug.Log("Walnelismy w sciane z dół-prawo");
                 }
             else if (hit.collider.tag == "Enemy")
                 {
                 Debug.Log("we bumped into some enemy.. would of attack him if it wasn't a prealpha version of the game!");
                 }
             }
             else
                 {
                 Move(1,-1.4);
                 }
           }
     if (Input.GetKeyDown (KeyCode.Keypad1))
           {
         if (Physics.Raycast (transform.position, Vector3(-1, 0, -1.4), hit, 1))
             {
             if (hit.collider.tag == "Wall")
                 {
                 Debug.Log("Walnelismy w sciane z dół-lewo");
                 }
             else if (hit.collider.tag == "Enemy")
                 {
                 Debug.Log("we bumped into some enemy.. would of attack him if it wasn't a prealpha version of the game!");
                 }
             }
             else
                 {
                 Move(-1,-1.4);
                 }
           }
     if (Input.GetKeyDown (KeyCode.Keypad7))
           {
         if (Physics.Raycast (transform.position, Vector3(-1, 0, 1.4), hit, 1))
             {
             if (hit.collider.tag == "Wall")
                 {
                 Debug.Log("Walnelismy w sciane z góra-lewo");
                 }
             else if (hit.collider.tag == "Enemy")
                 {
                 Debug.Log("we bumped into some enemy.. would of attack him if it wasn't a prealpha version of the game!");
                 }
             }
             else
                 {
                 Move(-1,1.4);
                 }
           }
           
 }
     
 function Move (X : float, Z : float)
 {
 rigidbody.position += Vector3(X,0,Z);
       transform.position = rigidbody.position;
         GameManager.instance.turn = GameManager.instance.turn + speed;
         GameManager.instance.speed = speed;
 }



 public var turnspassed : int;
 public var turnsrequired : int;
 
 function Update () {
 turnspassed += GameManager.instance.speed;
 if (turnspassed >= turnsrequired)
 {
         var tempselectedx = Random.Range(0, 3);
         var tempselectedz = Random.Range(0, 3);
         var selectedx : float = 0;
         var selectedz : float = 0;
             if (tempselectedx == 0)
                     {
                     selectedx = 0;
                     }
             else if (tempselectedx == 1)
                     {
                     selectedx = 1;
                     }
             else if (tempselectedx == 2)
                     {
                     selectedx = -1;
                     }
             if (tempselectedz == 0)
                     {
                     selectedz = 0;
                     }
             else if (tempselectedz == 1)
                     {
                     selectedz = 1.4;
                     }
             else if (tempselectedz == 2)
                     {
                     selectedz = -1.4;
                     }
                     
         turnspassed -= turnsrequired;
 Move(selectedx, selectedz);
 }
 }
 
 function Move (X : float, Z : float)
 {
 var hit : RaycastHit;
         if (Physics.Raycast (transform.position, Vector3(X, 0, Z), hit, 1))
             {
             if (hit.collider.tag == "Wall")
                 {
                 Debug.Log("MOB has crashed into the wall");
                 turnspassed = turnspassed + turnsrequired;
                 }
             else if (hit.collider.tag == "Playerchar")
                 {
                 Debug.Log("MOB has bumped into the player's char.. would of kick his ass, but wont yet.");
                 }
             else if (hit.collider.tag == "Enemy")
                 {
                 Debug.Log("BOING, two enemies hit each other..");
                 turnspassed = turnspassed + turnsrequired;
                 }
             }
         else
         {
         rigidbody.position += Vector3(X,0,Z);
         transform.position = rigidbody.position;
         }
 }


 public var turn : int;
 public var speed : int;
 static var instance : GameManager = null;
 
 function Awake()
 {
 if (!instance)
 {
  instance = this;
 }
 }
 
 function LateUpdate()
 {
 speed = 0;
 }
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

Answer by Selevan · Jun 22, 2013 at 04:12 PM

No chances ?

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

15 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

Related Questions

Custom Collision Detection 4 Answers

Can't detect collision. 1 Answer

detecting reycast collisions 1 Answer

Issues with Bullet Ricochet 1 Answer

Teleporting To Back Of Object? 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