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 JMC17 · Oct 28, 2013 at 02:35 PM · c#positioncubecorner

C# sticking to Cube corners position (raycasting AI script)

I'm trying to use raycasting to make an AI script but I have to cast rays from the forward face. Right now I'm stuck as you can see below, I created colored sphere to see the actual positions i'm gathering. (Raycast origin)

So I tried to get the four corners of the forward face but as you can see, only half of it is actually working properly.

alt text

Here's my script: (I might have declared a few things i'm not currently using, sorry for the mess)

 using UnityEngine;
 using System.Collections;
 
 public class AIscript4 : MonoBehaviour {
 
      Vector3 DTarget;
      Vector3 DSource;
     float Swidth;
         MeshFilter mshFilt;
     Mesh gridMesh;
     GameObject[] goa=new GameObject[4];
     // Use this for initialization
     ArrayList Waypoints =new ArrayList();
     ArrayList goA=new ArrayList();
     void Start () {
         Transform nz=GameObject.Find ("TargetCube").GetComponent<Transform>();
     Vector3 SC=new Vector3(0.3f,0.3f,0.3f);
         
         
      mshFilt = (MeshFilter)GetComponent<MeshFilter>();
          gridMesh = mshFilt.mesh;
         DSource=transform.position;
         DTarget=nz.position;
         Vector3 fixdirection=DTarget-DSource;
         Debug.DrawRay(DSource,fixdirection,Color.red,555f);
     
 
         CreateDebugSphere(Vector3.one,Color.red,SC);
         CreateDebugSphere(Vector3.one,Color.yellow,SC);
         CreateDebugSphere(Vector3.one,Color.green,SC);
         CreateDebugSphere(Vector3.one,Color.magenta,SC);
     }
     int inc;
     
         void CreateDebugSphere(Vector3 pos,Color col,Vector3 scale){
     
         GameObject go = Instantiate(Resources.Load("DebugSphere")) as GameObject; 
         go.renderer.material.color=col;
         go.transform.position=pos;
         go.transform.localScale=scale;
         Debug.Log (inc);
         goa[inc]=go;
             inc+=1;
     }
     
     // Update is called once per frame
     void Update () {
         Vector3 br1=transform.TransformPoint(gridMesh.bounds.min);
         Vector3 br2=transform.TransformPoint(gridMesh.bounds.max);
     // Red
                 Vector3 DebugS1=new Vector3(
             br2.x,
             br1.y,
             br2.z
             );
    // Yellow
                 Vector3 DebugS2=new Vector3(
             br2.x,
             br2.y,
             br2.z
             );
    // Green
                 Vector3 DebugS3=new Vector3(
             br1.x,
             br2.y,
             br2.z
             );
    // Magenta
                 Vector3 DebugS4=new Vector3(
             br1.x,
             br1.y,
             br2.z
             );
 
         goa[0].transform.position=DebugS1;
         goa[1].transform.position=DebugS2;
         goa[2].transform.position=DebugS3;
         goa[3].transform.position=DebugS4;
     }
 }


I spent some time searching around for an answer but can't find anything anymore to help myself =\\

I would love some explanation why its acting like that!

To recapitulate, all spheres should stick to the sky blue face corners, in any rotation.

Eventually, I want to cast rays from the bottom of the blue face, two on the corners and one in the middle, towards a target.

Also, if you have better solution or better ways of doing AIs capable of moving and dodging static and/or non-static objects I'd love to know, although ultimately it will be for iPhones, so can't be too much CPU intensive.

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 robertbu · Oct 28, 2013 at 04:10 PM

I haven't got my brain around exactly why it is happening. It has to do with how the rotation is applied in the TransformPoint(). But you can fix the problem doing the TranslatePoint() after you've defined the points:

     // Update is called once per frame
     void Update () {
        Vector3 br1=gridMesh.bounds.min;
        Vector3 br2=gridMesh.bounds.max;
         
     // Red
                 Vector3 DebugS1=new Vector3(
          br2.x,
          br1.y,
          br2.z
          );
    // Yellow
           Vector3 DebugS2=new Vector3(
          br2.x,
          br2.y,
          br2.z
          );
    // Green
           Vector3 DebugS3=new Vector3(
          br1.x,
          br2.y,
          br2.z
          );
    // Magenta
           Vector3 DebugS4=new Vector3(
          br1.x,
          br1.y,
          br2.z
          );
          
        goa[0].transform.position=transform.TransformPoint(DebugS1);
        goa[1].transform.position=transform.TransformPoint(DebugS2);
        goa[2].transform.position=transform.TransformPoint(DebugS3);
        goa[3].transform.position=transform.TransformPoint(DebugS4);
     }

Note that you use GameObject.Find() to find 'TargetCube', but you don't save 'nz' and use 'transform' to reference other items. As it is now, this script must be attached to the cube to work.

BTW: Well asked question. Without the animated GIF I would not have understood.

Comment
Add comment · Show 1 · 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 JMC17 · Oct 28, 2013 at 05:12 PM 0
Share

That is insanely great of you, thank you so much! It's working perfectly now, I'll remember to use TransformPoint only after defining the positions. And yes, I will fix those issues right away, it won't be attached to the cube.

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

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Smooth change of camera position goes wrong 1 Answer

Cube rotation and force 2 Answers

Multiple Cars not working 1 Answer

Checki f Player is within area? C# 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