• 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 n-carlson1 · Aug 08, 2016 at 04:48 PM · rotationtransformvector3transform.positionmovetowards

How to get a proper ramming effect?

So I want the enemy to look at the player and then after a second charge towards the player, but in a straight line. If the player moves, I want the enemy to run right past him into the wall. Here's what I have so far, but it's pretty wrong for many reasons. Just looking for guidance on how to at least get this part done. Currently he doesn't rotate right obviously because it's only being called for 1 frame or so and then movetowards is in update so its just a constant follow.

 using UnityEngine;
 using System.Collections;

 public class BossRam : MonoBehaviour {
  GameObject player; //for finding the player

 public Transform target;

 Transform myTransform;
 public float speed = 12f;
 bool ramming = false; //decide whether or not he's looking and about to ram
 bool charge = false;

 void Awake()
 {
     myTransform = transform;
 }
 void Start()
 {
     player = GameObject.Find("Player");
     target = player.transform;  
 }

 void Update()
 {
     if (ramming == false)
     {
         Ram();
     }
     if (charge == true)  //this code will always find him though. may need to do a vector3.position or something and then have him go past it?
     {
         float step = speed * Time.deltaTime;
         transform.position = Vector3.MoveTowards(transform.position, target.position, step);
     }
 }

 void Ram()
 {
     ramming = true;
     Vector3 vectorToTarget = target.position - transform.position;
     float angle = Mathf.Atan2(vectorToTarget.y, vectorToTarget.x) * Mathf.Rad2Deg;
     Quaternion q = Quaternion.AngleAxis(angle, Vector3.forward);
     transform.rotation = Quaternion.Slerp(transform.rotation, q, Time.deltaTime * speed);
     StartCoroutine(Ramming());
 }

 void OnCollisionEnter(Collision other)
 {
     if(other.gameObject.CompareTag("Wall"))
     {
         speed = 0;
         StartCoroutine(RamCooldown());
     }
 }
 IEnumerator Ramming()
 {
     yield return new WaitForSeconds(1);
     //charge
     charge = true;
 }

 IEnumerator RamCooldown()
  {
      charge = false;
      yield return new WaitForSeconds(1);
      //recover
      ramming = false;
  }
 }
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 Riiich · Jan 25 at 11:23 PM

Add a temporary position that the boss charges towards instead of getting the player position while charging.

 Vector3 chargeHere;
 
 IEnumerator Ramming()
 {
     yield return new WaitForSeconds(1);
     chargeHere = target.position;
     //charge
     charge = true;
 }
 
 // Change Line 33:
 transform.position = Vector3.MoveTowards(transform.position, chargeHere, step);
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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

69 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

Related Questions

Connecting transform with Vector3 4 Answers

Bullet Not Firing in Correct Direction 0 Answers

Move Transform to Target in X seconds 3 Answers

c# modify only one axis of a quaternion 2 Answers

How to make Y-Axis face away from a position? 2 Answers


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