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 /
  • Help Room /
avatar image
0
Question by whamrick3 · Nov 27, 2016 at 02:59 PM · unity 5rotationrotation axisartificial intelligencerotatetowards

have enemy try to get in front of player and push them back help

Hi, I have an enemy that is supposed to constantly try to get in front of the player and push them backwards... right now I have this as the code -

 public class PeasantController : MonoBehaviour {
 
     Transform player;
     public float moveSpeed;
     public float rotationSpeed;
     public float minDist;
     public float force;
     Vector3 dir;
     Vector3 oldPos;
     Vector3 oldRot;
     public bool playerCollide;
 
     void Start () {
         player = GameObject.FindWithTag ("Player").transform;
         oldPos = transform.position;
         oldRot = transform.rotation.eulerAngles;
         playerCollide = false;
 
     }
 
     void Update () {
         bool held = Input.GetKey(KeyCode.Space);
 
 
         if (!playerCollide && !held) {
             transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation 
                 (player.position - transform.position), 
                 rotationSpeed * Time.deltaTime);
 
             transform.LookAt (player.transform);
 
             if (Vector3.Distance (transform.position, player.transform.position) <= minDist) {
                 transform.position += transform.forward * moveSpeed * Time.deltaTime;
             }
         }
 
         if (playerCollide) {
             if (held) {
                 GetComponent<Rigidbody>().AddForce(dir*force);
             }
         }
 
         playerCollide = false;
 
     }
 
     void OnCollisionEnter(Collision col) {
         if (col.collider.tag == "Player") {
             dir = col.contacts [0].point - transform.position;
             dir = -dir.normalized;
             playerCollide = true;
         }
     }
 }


and it works at first because the enemies are placed in front of the player and if you do nothing they constantly push him back right and sometimes you can knock them flying way back by pressing space bar with alot of force added...

however if you get past them they just constantly come from behind you and push you forward and the space button doesnt work to push them anymore. basically I want to check if the enemy is in front of the player, and if so do what the code up there does correctly constantly push them back, but if it gets to the side of the player or behind I want to increase the movement speed and have them speed up and chase the player and try to get in front of them again to push them back from the front again....

I tried using this code to check if the enemy was in front of the player however when I use this code my game just freezes up when I try to test it and doesnt work anymore...

 using UnityEngine;
 using System.Collections;
 
 public class PeasantController : MonoBehaviour
 {
 
     Transform player;
     public float moveSpeed;
     public float rotationSpeed;
     public float minDist;
     public float force;
     Vector3 dir;
     Vector3 oldPos;
     Vector3 oldRot;
     public bool playerCollide;
     bool held;
     bool inFront;
 
     void Start()
     {
         player = GameObject.FindWithTag("Player").transform;
         oldPos = transform.position;
         oldRot = transform.rotation.eulerAngles;
         playerCollide = false;
         held = false;
         inFront = false;
 
     }
 
     void Update()
     {
         held = Input.GetKey(KeyCode.Space);
         Vector3 heading = transform.position - player.position;
         float dot = Vector3.Dot(heading, player.transform.forward);
         if (dot != 1)
         {
             inFront = false;
         }
         if (dot == 1)
         {
             inFront = true;
         }
 
 
         if (inFront && !held)
         {
             moveSpeed = 10.0F;
             transform.rotation = Quaternion.Slerp(transform.rotation,
                 Quaternion.LookRotation(player.position - transform.position),
                 rotationSpeed * Time.deltaTime);
 
             transform.LookAt(player.transform);
 
             if (Vector3.Distance(transform.position, player.transform.position) <= minDist)
             {
                 transform.position += transform.forward * moveSpeed * Time.deltaTime;
             }
         }
         else if (!inFront && !held)
         {
             while (!inFront)
             {
                 moveSpeed = 25.0F;
                 transform.rotation = Quaternion.Slerp(transform.rotation,
                     Quaternion.Inverse(player.transform.rotation),
                     rotationSpeed * Time.deltaTime);
                 transform.LookAt(player.transform);
                 transform.position += transform.forward * moveSpeed * Time.deltaTime;
 
             }
         }
         else
         {
             Debug.Log("target should be knocked away");
         }
 
     }
 
     void OnCollisionEnter(Collision col)
     {
         if (col.collider.tag == "Player")
         {
             dir = col.contacts[0].point - transform.position;
             dir = -dir.normalized;
             if (held)
             {
                 GetComponent<Rigidbody>().AddForce(dir * force);
             }
             held = false;
         }
     }
 }
 


i also tried using code somehwat like this instead of the dot product but that made my game freeze up when pressing play as well..

 Vector3 directionToTarget = transform.position - player.position;
 float angle = Vector3.Angle(transform.forward, directionToTarget);
 float distance = directionToTarget.magnitude;
  
 if (Mathf.Abs(angle) != 90 && distance < 10) {
     inFront = false;
 }
 
 if (Mathf.Abs(angle) == 90)
 {
     inFront = true;
 }



any help is greatly appreciated

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

0 Replies

· Add your reply
  • Sort: 

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

127 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 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

have enemy try to get in front of player and push them back help 0 Answers

Unity 2D rotation not smooth? 0 Answers

LookAt only on local Z Axis 0 Answers

how to reset back the rotation 0 Answers

Why won't my model rotate up on X axis? 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