Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 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.

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

Player not facing the mouse correctly 1 Answer

Using xbox controller with top down shooter 0 Answers

Place object on radius pointing to a other object 1 Answer

Unity Rotate Raycast on Quaternion 1 Answer

How can I turn my cube object to the direction it is traveling? 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