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 OmniKeith · Apr 13, 2015 at 05:53 PM · raycastplayer movementmovetowardscoliderobject movement

raycast isn't working in my movetowards script.

the script i'm using to have an enemy see a player and move to the position that it saw him at isn't working 100%. It only sees in a straight line from the start point of the enemy. so if I round a corner the enemy moves to the edge of the corner and waits but doesn't cast another ray to see me standing right in front of it.

 using UnityEngine;
 using System.Collections;

 public class Spark : MonoBehaviour {

 public GameObject Player;

 private Vector3 PlayerPos;
 private bool PlayerSeen;
 public float Speed;
 private RaycastHit hit;

 void Start () {

     //Player = GameObject.Find("FPSController");


 }
 
 void Update () {
     transform.LookAt (Player.transform.position);
     Vector3 RayStart = transform.forward;
     Vector3 RayEnd = Player.transform.position;

     if (Physics.Raycast (RayStart, RayEnd, out hit, 20)) {
         if (hit.collider.gameObject.tag == "Player") {

             PlayerPos = Player.transform.position;
             //Debug.Log(PlayerPos);
         }
     }

     float step = Speed * Time.deltaTime;
     transform.position = Vector3.MoveTowards(transform.position, new Vector3(PlayerPos.x, 1, PlayerPos.z), step);

 }

}

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 OmniKeith · Apr 13, 2015 at 09:32 PM

Again, I ended up fixing this one myself with a quick work around. It's still not what I want it to be but I know why it isn't working. The RayCast goes from point a to point b and then out to the ray... so since the enemy object is always facing the player i just created a new vector3 with the forward added to the enemies coordinates and draw a ray from point a to the new vector and out 20f. I would like to fix the problem of having to always have my enemy rotate to the players direction because I may want to use a better more featured model and I want the player to be able to see its back.

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

2 People are following this question.

avatar image avatar image

Related Questions

Move ball to touch input but freeze y axis 0 Answers

How can I split up player control logic? 1 Answer

Put an offset distance to gameobject that follows player 2 Answers

Change Colider to Raycast instead? 2 Answers

Shotting Or thrwoing Object wtih Vector3.MoveTowards 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