- Home /
The question is answered, right answer was accepted
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;
}
}
}
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.
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.
Could you send me your project? I'll have a look for you.
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
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);
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.
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)
Hmmm, but if I do transform.position it won't take colliders into effect properly. I will very well consider this. Thanks.
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.