• 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
Question by Toni95 · Aug 20, 2015 at 12:16 PM · velocitymovement scriptrightshoots

Proper movement code for calculating leading shoots

Hello Unity community.

First of all, you will probably wonder what I exactly mean with that title. I wanted to make leading shoots to fire to mobile targets so that they will hit the target even though the target was in movement, so I found this page: http://wiki.unity3d.com/index.php/Calculating_Lead_For_Projectiles and copied these code, assuming it would work.

Then, when I tested my project, I noticed it wasn't working, the shoots where facing the target's current position. Later I noticed that Unity was assuming that the target velocity was 0, to be precise (0.0, 0.0, 0.0), the same as if the target was standing still.

At this point, I assumed that I had done my player movement wrong, so I need to know a movement code that Unity could recognise as a proper movement so that my leading shoots will work and the target.GetComponent().velocity would be greather than 0.

Here is my code:

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class PlayerController : MonoBehaviour {
     
     public float speed;
     public float rotationSpeed;    
 
 void FixedUpdate ()
     {       // Rotates right and left
         transform.Rotate(0, -Input.GetAxis("Horizontal")*rotationSpeed*Time.deltaTime, 0,Space.World);
             
         if (Input.GetKey("up")||Input.GetKey("w")){ // Goes forward
             transform.Translate(0,0,Time.deltaTime*speed,Space.Self);
         }
 
         if(Input.GetKey ("down")||Input.GetKey("s")){ // Goes backward
             transform.Translate(0,0,-Time.deltaTime*speed,Space.Self);
         }
 
         if (Input.GetKey ("e")){ // Goes right
             transform.Translate(Time.deltaTime*speed,0, 0, Space.Self); 
         }
         if (Input.GetKey ("q")){ // Goes left
             transform.Translate(-Time.deltaTime*speed,0, 0, Space.Self); 
         }
     }
 }

I need to know if this code is right or, instead, a proper code that Unity could recognise as a movement (I tested the speed by writing "print(target.GetComponent().velocity)". Could you tell me if this is right?

Comment

People who like this

0 Show 0
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

Answer by Youri1er · Aug 20, 2015 at 01:13 PM

Hi,

Your problem is simple to resolve. Don't use transform translate ! First put a rigdbody on your player. Second use GetComponent().velocity = new vector3(.......);

You don't need to use Time.deltatime because velocity is update by unity with a Time.deltatime.

Say me if you don't arrive, i'll give you some code. But it's better to arrive alone to progress.

Comment

People who like this

0 Show 10 · 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 Toni95 · Aug 21, 2015 at 12:34 PM 0
Share

Thank you for answering Youri1er.

I have tried GetComponent().velocity but Unity tells me there is a compiler error, I tried GetComponent().velocity and it worked.

The velocity and the shoots are working perfectly, but without translate I can't move away of the X and Z axis, so even if I rotate the player, when I go forward it faces the Z axis.

Here is my code:

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class PlayerController : MonoBehaviour {
     
     public float speed;
     public float rotationSpeed;
 
 void FixedUpdate ()
     {   
         // Rotates right and left
         transform.Rotate(0, -Input.GetAxis("Horizontal")*rotationSpeed*Time.deltaTime, 0,Space.World);
             
         // Forward and backward
         if (Input.GetKey ("w")){
             GetComponent<Rigidbody>().velocity = new Vector3 (0.0f, 0.0f, speed); 
         }
         if (Input.GetKey ("s")){
             GetComponent<Rigidbody>().velocity = new Vector3 (0.0f, 0.0f, -speed); 
         }
 
         // Goes right and left
         if (Input.GetKey ("e")){
             GetComponent<Rigidbody>().velocity = new Vector3 (speed, 0.0f, 0.0f); 
         }
         if (Input.GetKey ("q")){
             GetComponent<Rigidbody>().velocity = new Vector3 (-speed, 0.0f, 0.0f); 
         }
     }

How could I move my player properly?

avatar image Youri1er · Aug 21, 2015 at 01:07 PM 1
Share

You can add two key, first to go up and second to go down. In General, video games use your move with one touch to jump

if(Input.GetKey("Space")) GetComponent().velocity = new Vector3 (speed, speed,0.0f);

But you can improve your code because if you press more than one touch your character block !

So you must do this :

Vector3 temp_Move_Direction;

if (Input.GetKey ("w")) temp_Move_Direction.z = 1;

if (Input.GetKey ("s")) temp_Move_Direction.z = -1;

// Goes right and left if (Input.GetKey ("e")) temp_Move_Direction.x = 1;

if (Input.GetKey ("q")) temp_Move_Direction.x = -1;

if (Input.GetKey ("Space")) temp_Move_Direction.y = 1;

if (Input.GetKey ("Shift")) temp_Move_Direction.y = -1;

temp_Move_Direction.Normalize ();

GetComponent().velocity = temp_Move_Direction * speed;

avatar image Toni95 · Aug 22, 2015 at 01:21 PM 0
Share

Thank you again Youri1er.

Your idea is amazing, I have learnt a lot thanks to you in just two days. I don't want to disturb you again, but I have another question: When I go up, down, jump... The object keeps moving to that direction, so if I go forward, it keeps going forward, if I jump, it starts going up and don't stop. And I can't move away from the X and Z axis, just as before. Should I do something that, for instance, when 0.2 seconds go on make the player's velocity return to 0 or something like that?

avatar image Youri1er · Aug 22, 2015 at 01:52 PM 0
Share

Yes that's an idea but i prefer to verify if there is no key pressed so i put velocity at Vector3.zero.

avatar image Owen-Reynolds · Aug 22, 2015 at 05:14 PM 3
Share

How objects should move is a big design decision. You should NOT change from translate to rigidbodies just because it might make one little thing easier.

Speed of a translated object can be calculated based on the movement variables. In this case, it's speed times the direction for whatever key.

Now, if reading all the comments makes you realize that rigidbodies are better for your game, that's great. Or if you're learning Unity and just want to try some stuff (which I think is the case.) But for real, there's almost always a better way than a giant redo.

Show more comments

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How to Make Velocity.y based on AddForce 0 Answers

Player is not moving 0 Answers

(C#) Accelerating Player Based On Distance 2 Answers

How to move Character with AddForce 1 Answer

Object flickers back and forth if it reaches mouseposition. 0 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