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 PokeRwOw · Sep 29, 2015 at 03:36 AM · raycasttransformlayermask

Enemy gameObject didn't move

I'm actually working on a game project with Unity3D in C#. I already have a good part of my project, but i'm confronted at a problem with the implementation of enemies. Firstly I'm following this tutorial. So I have a gameobject for enemy with box collider and basically all I need. And there is my script :

 using UnityEngine;
 using System.Collections;
 
 public class Enemy : MonoBehaviour {
 
 [SerializeField]
 private float rotationSpeed = 180; // In degrees per second
 
 [SerializeField]
 private float movementSpeed = 1f; // In units per second
 
 [SerializeField]
 private float meshRadius = 1f; // In units
 
 private IEnumerator turnTowardsPlayerCoroutine;
 private IEnumerator moveTowardsPlayerCoroutine;
 
 void OnTriggerEnter(Collider collider)
 {
     if (collider.gameObject.tag == "Player")
     {
         float playerDistance = Vector3.Distance(collider.transform.position, transform.position);
 
         // Ignore trigger events from the inner colliders
         if (playerDistance >= 2f * meshRadius)
         {
             turnTowardsPlayerCoroutine = TurnTowardsPlayer(collider.transform);
             moveTowardsPlayerCoroutine = MoveTowardsPlayer(collider.transform);
             StartCoroutine(turnTowardsPlayerCoroutine);
             StartCoroutine(moveTowardsPlayerCoroutine);
         }
     }
 }
 
 void OnTriggerExit(Collider collider)
 {
     if (collider.tag == "Player")
     {
         float playerDistance = Vector3.Distance(collider.transform.position, transform.position);
 
         // Ignore trigger events from the inner colliders
         if (playerDistance >= 2f * meshRadius)
         {
             StopCoroutine(turnTowardsPlayerCoroutine);
             StopCoroutine(moveTowardsPlayerCoroutine);
         }
     }
 }
 
 private IEnumerator TurnTowardsPlayer(Transform player)
 {
     while (true)
     {
         Quaternion targetRotation = Quaternion.LookRotation(player.position - transform.position, Vector3.up);
         targetRotation.x = 0f;
         targetRotation.z = 0f;
 
         transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation, rotationSpeed * Time.deltaTime);
         yield return 0;
     }
 }
 
 private IEnumerator MoveTowardsPlayer(Transform player)
 {
     while (true)
     {
         Vector3 playerDirection = transform.position - player.position;
         playerDirection.y = 0;
         playerDirection = playerDirection.normalized;
 
         Vector3 deltaMovement = playerDirection * movementSpeed * Time.deltaTime;
 
         int layermask = LayerMask.GetMask("Environment");
         Vector3 movingTowards = transform.position - playerDirection*meshRadius + (new Vector3(0f, 0.1f, 0f));
         if (Physics.Raycast(movingTowards, Vector3.down, 0.25f, layermask))
         {
             transform.position -= deltaMovement;
         }
 
         yield return 0;
     }
 }
 }

The result is, when I enter in the zone where enemy must attacking me, the enemy rotated towards me but he didn't move. Did I forget something or did something wrong ?

Thanks per advance!

PokeRwOw

Comment
Add comment · Show 2
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 Kornikolia · Sep 29, 2015 at 07:23 AM 0
Share

Do you have Rigidbody Component on this gameObject?

avatar image PokeRwOw · Sep 29, 2015 at 01:07 PM 0
Share

Yeah you can see all propertys of the GameObject in this screen

alt text

ennemy.png (45.8 kB)

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by theLittleSettler · Sep 29, 2015 at 07:50 AM

Physics.Raycast is almost certainly false. I've had trouble using layermasks with raycasting before when I've assigned the layermask in code (no doubt due to my lack of understanding), though when I assign it though the inspector its fine.

So remove that if statement to check if that is the cause. If so, then remove the layermask from the raycast so its Physics.Raycast(movingTowards, Vector3.down, 0.25f)...to check if its due to the layer or something else. Continue that process until you solve it.

Comment
Add comment · Show 2 · 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 PokeRwOw · Sep 29, 2015 at 01:09 PM 0
Share

I deleted the whole condition with Physics.Raycast and the enemy still didn't move... I have really no idea where it may come.

avatar image theLittleSettler PokeRwOw · Oct 07, 2015 at 06:43 AM 0
Share

use Debug.Log() to see whether $$anonymous$$oveTowardsPlayer is executing at all...

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Raycasting on mask doesnt work 2 Answers

How does Raycast layer mask work? 1 Answer

JS Dynamic Height Camera vibrating =( 1 Answer

Push object in direction of camera 0 Answers

SphereCast doesn't work with a layerMask 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