Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
This question was closed Feb 14, 2014 at 10:25 PM by Dothackking for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Dothackking · Feb 09, 2014 at 06:10 PM · c#2dmovementtranslate

Why is my character strafing around the mouse (2d)

The way I understand it, my character SHOULD be translating +X, -X, +Y, -Y, but instead it's translating "left around mouse cursor" etc. I have no idea what happened, the code is simple enough.

Any tips? ideas? (I know my bullet code is broken, I'm trying to fix that one before coming asking for help)

Thanks

 using UnityEngine;
 using System.Collections;
 
 public class Movement : MonoBehaviour {
     public int spd;
     public GameObject bullet;
     GameObject firedBullet;
     Vector3 mousePos;
     Vector3 worldPos;
     float angle;
 
     // Use this for initialization
     void Start () {
 
     
     }
     // Update is called once per frame
     void Update () {
 
 
 
 
         Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
         transform.rotation = Quaternion.LookRotation(Vector3.forward, mousePos - transform.position);
 
 
 
         if(Input.GetKey (KeyCode.W))
         {
 
             transform.Translate (0, (spd * Time.deltaTime), 0);
 
         }
         if(Input.GetKey (KeyCode.S))
         {
 
             transform.Translate (0, 0 - (spd * Time.deltaTime), 0);
         }
         if(Input.GetKey (KeyCode.A))
         {
 
             transform.Translate ( 0 - (spd * Time.deltaTime), 0, 0);
         }
         if(Input.GetKey (KeyCode.D))
         {
 
             transform.Translate ((spd * Time.deltaTime),0 , 0);
         }
         if(Input.GetButtonDown ("Fire1"))
         {
             firedBullet = Instantiate(bullet, transform.position, Quaternion.AngleAxis (90, Vector3.up) ) as GameObject;
         }
             
     }
 }
 
Comment
Add comment · Show 7
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 Invertex · Feb 10, 2014 at 04:53 AM 0
Share

Perhaps check to see if you have by accident applied an animation to the bullet, in your Animation tab? It could be automatically setting the rotation back to downward constantly.

avatar image Dothackking · Feb 10, 2014 at 07:43 AM 0
Share

Nothing. I'ts just a default sphere. I really don't understand the problems. Usually if it's a Unity error, restarting unity fixes it. Not this time.

avatar image Dothackking · Feb 10, 2014 at 10:18 PM 0
Share

I still haven't been able to solve this :(

avatar image MaGuSware™ · Feb 11, 2014 at 01:55 PM 1
Share

Could you send me your project? I'll have a look for you.

avatar image Dothackking · Feb 11, 2014 at 05:26 PM 0
Share

Sure, although it is just as I said, a cube with this script on it, which fires a sphere with a script to move forward on it. I must apologize for the size, I didnt know what I'd be needing so I imported anything I might need. I was just considering s$$anonymous$$ling my two scripts and re-doing the project, incase for some reason, something else was messing it up. Hopefully this is how you wanted it: http://www.mediafire.com/download/7ao5p7ecfgnv4nh/unityproject.rar

Show more comments

1 Reply

  • Sort: 
avatar image
1
Best Answer

Answer by MaGuSware™ · Feb 10, 2014 at 12:35 AM

--EDIT-- Removed previous answer and replaced with this, so for anybody reading... the comments might not make sense.

Ok, after looking at your project I see what you mean and I have found your problem.

The following line

 transform.rotation = Quaternion.LookRotation(Vector3.forward, mousePos - transform.position); 

will always make your cube look at the mouse position.

The transform.Translate() function is movement relative to it's current position and local orientation.

So this means every time you move left and right, and the next time it updates the cube will look back at the mouse giving it a new direction to translate with, causing it to orbit.

If you don't want it to face the mouse when strafing, add that line of code to the if statements on W and S instead of it being where it is now. Doing this will only make the cube look at the mouse when moving forwards and backwards (assuming this is what you want).

But if you want it to just move around without influence of the mouse position and still rotate, you need to use a different method of moving your cube. There are a few. You could set the position of the cube manually... like this:

 [old] transform.Translate (0, (spd * Time.deltaTime), 0);
 [new] transform.position += new Vector3(0, spd * Time.deltaTime, 0);
Comment
Add comment · Show 4 · 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 Dothackking · Feb 10, 2014 at 01:38 AM 0
Share

It works for you? I'm using a cube and when I move my mouse, the cube will go towards the mouse if I hold W, if I hold A it will circle-strafe the mouse. I only have two scripts, this one and one to propel bullets forward, a single line script.

avatar image Dothackking · Feb 10, 2014 at 01:46 AM 0
Share

I think my Unity may just be bugged out. It's bugged out before, but never to this extent. I set my bullet's transform.rotation to be the same as the player's, so it should fly towards the mouse, but ins$$anonymous$$d it goes downward along the Z. It does this for everything except making it a set angle every shot (90degree for example is toward right of screen)

avatar image Dothackking · Feb 13, 2014 at 06:12 PM 0
Share

Hmmm, but if I do transform.position it won't take colliders into effect properly. I will very well consider this. Thanks.

avatar image MaGuSware™ · Feb 13, 2014 at 06:37 PM 0
Share

you can still used translate, but you'll have to calculate the angles to move it at or alternatively, (what I would do), split up the movement and looking into 2 separate scripts on to 2 different objects. So you have the movement on ObjectA, and Ai$$anonymous$$g and Shooting on ObjectB... make ObjectB a child of ObjectA.

Follow this Question

Answers Answers and Comments

20 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Enemy not moving towards Player 0 Answers

Making a bubble level (not a game but work tool) 1 Answer

Unusual error regarding movement in a top down 2d sidescroller 1 Answer

No movement when animation plays (2d) 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