• 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 soco2211 · Mar 22, 2018 at 01:56 PM · rigidbodyvelocitybeginnerprojectilemovetowards

MoveTowards not working the way I'd like

I know I've done this before, but for some reason I can't get it to work this time. Trying to shoot a projectile at an enemy. Easy peasy, right? Apparently not today. I tried using Vector3.forward but it always shot along the Z axis not relative to my player. Then I tried MoveTowards, but it would just appear and not move. I tried another way using Lerp and it just got super close to my target but wouldn't collide. Anyway, this is the code so far, I've been commenting stuff out because I've been trying so many different way to do this.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class HeroStateMachine : MonoBehaviour {
 
 //bsm.performList[0] is the player
 
 ...unrelated stuff above this
 
         } else if (bsm.performList[0].activeRange == "Ranged") {
 
             Vector3 enemyPos = new Vector3(enemyToAtk.transform.position.x + 1.5f, enemyToAtk.transform.position.y, enemyToAtk.transform.position.z);
 
             //animation
             anim.Play(bsm.performList[0].activeAtk.atkAnim);
             AnimatorClipInfo[] clipInfo = anim.GetCurrentAnimatorClipInfo(0);
 
             //wait a bit
             yield return new WaitForSeconds(clipInfo[0].clip.length * 0.75f);

             //Instatiate magic prefab
             GameObject magicAtk = Instantiate(bsm.performList[0].activeAtk.atkPrefab);
             //magicAtk.transform.LookAt(bsm.performList[0].attackerTarget.transform.position);
             magicAtk.transform.position = transform.position;
             //magicAtk.transform.rotation = transform.rotation;
 
             //do dmg
             DoDamage();
 
 ...unrelated stuff below this
 
 }
 

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class BulletController : MonoBehaviour {
 
     public float bulletSpeed = 5.0f;    //bullet speed
     private Rigidbody rb;
     private BattleStateMachine bsm;
     private GameObject origin;
     private GameObject target;
 
     void Awake() {
         bsm = GameObject.Find("BattleMgr").GetComponent<BattleStateMachine>();
         origin = bsm.performList[0].attackerGameObject;
         target = bsm.performList[0].attackerTarget;
     }
 
 
     void Start () {
         print(target);
         rb = GetComponent<Rigidbody>();
         Vector3 targetLookAt = new Vector3(target.transform.position.x, target.transform.position.y, target.transform.position.z);
         transform.LookAt(targetLookAt);
         //rb.velocity = Vector3.forward * bulletSpeed;
     }
 
     void Update() {
         //transform.position = Vector3.MoveTowards(origin.transform.position, target.transform.position, bulletSpeed * Time.deltaTime);
 
         rb.velocity = Vector3.forward * bulletSpeed;
     }


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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by hexagonius · Mar 22, 2018 at 02:15 PM

Vector3.forward is just a vector and without context it's the world forward. you're aiming for some transform.forward.

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

Answer by BigPha · Mar 22, 2018 at 05:06 PM

Vector3.forward will always be (0, 0, 1).

You can instead try to use the the tranform.forward of the launcher gameobject.

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

108 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

Related Questions

Controll 3D Rigidbody Projectile with Joystick 1 Answer

ball breaks through walls 1 Answer

Projectile launch insufficient velocity 2 Answers

Velocity powered rigidbody on a moving platform without parenting. 3 Answers

relativeVelocity how to? (noob question) 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