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 Jkight1212 · Oct 24, 2018 at 06:50 AM · movement scriptprojectile

How can I make a unit fire in one direction while still moving in another?

My code ALMOST works perfectly but for some reason when I give my unit the command to fire while it's already moving, the first shot will fly off in the direction It was moving. (the following shots work fine, even while still moving).

I'm new to unity so i'm confused because everything seems like it should work here.

Here's the code. Any help is appreciated!

 using UnityEngine;
 using UnityEngine.AI;
 using System;
 
 public class PlayerControls : MonoBehaviour {
 
     public float moveSpeed;      // How fast this unit moves per frame.
     public Transform firePoint;  // Where the bullet is fired from.
     public GameObject bullet;    // Reference to the pre-created object called bullet.
 
     NavMeshAgent thisUnit;       // This unit's NavMeshAgent
     Vector3 clickLocation;       // The last clicked location.
     Rigidbody rigidBody;         // Reference to the Rigidbody used by this unit.
     LayerMask floorMask;         // The location of the floor.
     float camRayLength;          // How far the camera will look when searching for the floor.
 
     // Initialization of variables upon creation of this script.
     void Awake()
     {
         floorMask = LayerMask.GetMask("Floor");
         rigidBody = GetComponent<Rigidbody>();
         thisUnit = GetComponent<NavMeshAgent>();
         camRayLength = 150f;
     }
 
     // Game mainloop. Contains all player Inputs.
     private void Update()
     {
         // When the player right clicks, moves the player.
         if (Input.GetKey("mouse 1"))
         {
             clickLocation = FindMouse();
             Turn(clickLocation);
             Move(clickLocation);
         }
 
         //Fires a bullet when you push down the Q button.
         if (Input.GetKeyDown(KeyCode.Q))
         {
             Turn(FindMouse());
             Shoot();
         }
     }
 
 
     // Rotates the player to a certain direction
     void Turn(Vector3 targetRotation)
     {
         targetRotation = targetRotation - transform.position;
         targetRotation.y = 0f;
 
         Quaternion newRotation = Quaternion.LookRotation(targetRotation);
         rigidBody.MoveRotation(newRotation);
     }
 
 
     // Finds the location of the mouse in relation to the ground.
     public Vector3 FindMouse()
     {
         Ray camRay = Camera.main.ScreenPointToRay(Input.mousePosition);
         RaycastHit floorHit;
  
         Physics.Raycast(camRay, out floorHit, camRayLength, floorMask);
         Vector3 mouseLocation = floorHit.point;
         mouseLocation.y = 0f;
         return mouseLocation;
     }
 
 
     // Moves the player forwards by one unit of move speed.
     void Move(Vector3 target)
     {
         thisUnit.SetDestination(target);
     }
 
 
     // Creates a bullet, sets it's start position and start rotation.
     void Shoot()
     {
         Instantiate(bullet, firePoint.position, firePoint.rotation);
     }
 }
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 tormentoarmagedoom · Oct 24, 2018 at 11:42 AM

Good day.

In Void turn(), are you sure this is correct?

  targetRotation = targetRotation - transform.position;

I supouse you wanted to do this

  targetRotation = targetPosition - transform.position;

Bye!!

Comment
Add comment · Show 1 · 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 Jkight1212 · Oct 25, 2018 at 03:23 AM 0
Share

I'm not sure what you mean by this. targetPosition is not a variable anywhere in my script. targetRotation is a Vector3 that represents the point where the unit is supposed to turn towards.

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

95 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

Related Questions

2D Top Down Movement 2 Answers

Temporary GameObject Movement 0 Answers

How to calculate xSpeed and ySpeed according to float value of rotation? 1 Answer

Boundaries for Camera Movement 1 Answer

sonic movement stick to the ground 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