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 Tariq-mehmood · Oct 11, 2016 at 07:51 AM · raycastraycasthitartificial intelligencesensor

RayCast not working properly and ray is not hitting an object

now what i am try to do is to create AI for enemy car in my racing game for that i have to create a path and now i am trying to create sensors in front of my enemy car so that it can dough/avoid collision with those obstacle in front of car and for sensor i am using raycast function in c# but what is happening is my ray is not displaying properly or i can say its start position is ok but end position is not towards on obstacle and its towards terrain's start position you can also see in pictures below is my script

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;

  public class CarEngin : MonoBehaviour {

 public Transform path;
 public Transform[] pathTranform;
 private int currentNode = 0;
 private List<Transform> nodes;
 public float MaxSteerAngle = 45f;
 public WheelCollider wheelFL;
 public WheelCollider wheelFR;
 public WheelCollider wheelBL;
 public WheelCollider wheelBR;
 public float maxTorque = 50f;
 public float distfrompath = 20f;
 public int pathTranformLength;
 float topSpeed = 150f;
 float currentSpeed;
 float deaccelrationSpeed = 10;
 float frntSensorStartPoint = 5;
 float sensorLength = 5;
 float frontSensorDist = 5;
 
 




 // Use this for initialization
 void Start () {
    
    Transform[] pathTranform = path.GetComponentsInChildren<Transform>();
     nodes = new List<Transform>();
     pathTranformLength = pathTranform.Length;
     for (int i = 1; i < pathTranform.Length; i++)
     {
         if (pathTranform[i] != path.transform)
         {
             nodes.Add(pathTranform[i]);
             
         }
       print(pathTranform);

     }
    
 }
 
 // Update is called once per frame
 void FixedUpdate () {
    ApplySteer();
     Move();
     sensor();

 }

 void ApplySteer()
 {
     Vector3 relativeVector = transform.InverseTransformPoint(nodes[currentNode].position.x,transform.position.y, nodes[currentNode].position.z);
     //relativeVector = relativeVector / relativeVector.magnitude;
     float newSteer = (relativeVector.x / relativeVector.magnitude) * MaxSteerAngle;
     wheelFL.steerAngle = newSteer;
     wheelFR.steerAngle = newSteer;
             
     if(relativeVector.magnitude <= distfrompath)
     {
         currentNode++;
         if(relativeVector.magnitude >= pathTranformLength)
         {
             currentNode = 0;
         }
     }

 }

 void Move()
 {
     currentSpeed = 2 * (22 / 7) * wheelBL.radius * wheelBL.rpm * 60 / 1000;
     currentSpeed = Mathf.Round(currentSpeed);
     if (currentSpeed <= topSpeed)
     {
         wheelBL.motorTorque = maxTorque;
         wheelBR.motorTorque = maxTorque;
         wheelBL.brakeTorque = 0;
         wheelBR.brakeTorque = 0;
     }
     else
     {
         wheelBL.motorTorque = 0;
         wheelBR.motorTorque = 0;
         wheelBL.brakeTorque = deaccelrationSpeed;
         wheelBR.brakeTorque = deaccelrationSpeed;

     }
 }


 void sensor()
 {
     Vector3 pos;
     RaycastHit hit;
     pos = transform.position;
     pos += transform.forward * frntSensorStartPoint;
     if(Physics.Raycast(transform.forward,pos,out hit,sensorLength))
     {
         Debug.DrawLine(pos,hit.point, Color.red,1000);

     }



 }

 }
  

alt text

zddsfsdfsdfsdsdfsdfdsfdsf.png (340.9 kB)
zddsfsdfsdfsdsdfsdfdsfdsf.png (340.9 kB)
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 doublemax · Oct 11, 2016 at 10:56 AM

 if(Physics.Raycast(transform.forward,pos,out hit,sensorLength))

You swapped the first two parameters, position comes first, then direction vector.

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 Tariq-mehmood · Oct 11, 2016 at 07:47 PM 0
Share

Thanks @doublemax its done

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to place an object in the direction of a raycast 1 Answer

ray cast returning the wrong object, going through multiple objects 1 Answer

Click to move script help 1 Answer

Get TYPE script from raycast target 1 Answer

Raycasting and foreach loops 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