Hello everyone. I only recently started out with Unity and already I feel bad to have to pool your guys valuable time for my (presumably) minor problem. Thanks in advance for helping me out with this. I’m still trying to get the grips with the scripting and in order to do that I try to use the publicly available resources and get a understanding of how things work. However, I already ran into a problem now at a very basic thing: The movement of my character.
What I want is that the character rotates to where my mouse is pointed at to imitate a “Alien Swarm” kind of control.
My game is 3D but from a elevated angle and I used “Evac City” Scripting Resources from the PDF to achieve the basic control.
The issue is this: The script works great and objects rotate visibly properly to where my mouse is at, BUT the control is all wrong. For example when my mouse is on the bottom, my character looks at the bottom too (as it should) but when I press forward to walk where he looks at, he walks to the right instead. Somehow the forward point is all different.
Since I realize it’s hard to understand in pure text here is a screenshot showing the problem:
Contrary to “Evac City” I am using the Character Controller instead of pushing a Rigidbody.
Here is the Code of the Script:
//game objects (variables which point to game objects)
private var objPlayer : GameObject;
//input variables (variables used to process and handle input)
private var inputRotation : Vector3;
private var inputMovement : Vector3;
// identity variables (variables specific to the game object)
var moveSpeed : float = 100f;
private var thisIsPlayer;
// calculation variables (variables used for calculation)
private var tempVector : Vector3;
private var tempVector2 : Vector3;
var speed : float = 6.0;
var jumpSpeed : float = 8.0;
var gravity : float = 20.0;
private var moveDirection : Vector3 = Vector3.zero;
function Update() {
FindInput();
ProcessMovement();
var controller : CharacterController = GetComponent(CharacterController);
if (controller.isGrounded) {
// We are grounded, so recalculate
// move direction directly from axes
moveDirection = Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Input.GetButton ("Jump")) {
moveDirection.y = jumpSpeed;
}
}
// Apply gravity
moveDirection.y -= gravity * Time.deltaTime;
// Move the controller
controller.Move(moveDirection * Time.deltaTime);
// Debug Raycast
var hit : RaycastHit = RaycastHit();
Debug.DrawRay(transform.localPosition, transform.forward * 10);
if (Physics.Raycast(transform.position, transform.forward, hit, 10.0f))
{
Debug.Log(hit.transform.name);
}
}
function FindInput ()
{
// find vector to the mouse (unmodified from EvacCity)
tempVector2 = Vector3(Screen.width * 0.5f, 0, Screen.height * 0.5f); // the position of the middle of the screen
tempVector = Input.mousePosition; // find the position of the mouse on screen
tempVector.z = tempVector.y; // input mouse position gives us 2D coordinates, I am moving the Y coordinate to the Z coorindate in temp Vector and setting the Y coordinate to 0, so that the Vector will read the input along the X (left and right of screen) and Z (up and down screen) axis, and not the X and Y (in and out of screen) axis
tempVector.y = 0;
Debug.Log(tempVector);
inputRotation = tempVector - tempVector2; // the direction we want face/aim/shoot is from the middle of the screen to where the mouse is pointing
}
function ProcessMovement()
{
transform.rotation = Quaternion.LookRotation(inputRotation);
transform.eulerAngles = Vector3(0, transform.eulerAngles.y + 180, 0);
}
The first thing I’d do is double-check and make sure the axis assignments for the move direction in local space match the object orientation. The way you’re currently building the ‘move direction’ vector implies that +z is forward and x is to the side, so double-check and make sure that’s how your object is oriented.
Thank you Jesse for your reply. You can ignore the line in question, it was a obsolete leftover from “Evac City” code.
Unfortunately I am not sure I understand what you suggested or how to double-check it. However, the Axis Code in the FindInput() function is untouched from Evac City. Is the issue with it there? Or is it a issue that begins in the 3D App?
I’m so clueless :shock: The only thing I can say for sure is that the controls work fine it’s just that “forward” is in the wrong place.
the mouse is on the bottom of the screenshot, in the middle.
The monitor-like object is the (untextured) in-game character I will want to control. The cylinder was my first test object using the same component attached. That’s also why they both cast the ray to see where their forward movement point is.
Sorry, by ‘monitor-like object’, I actually meant the object floating in the air on the right (which looks kind of like a monitor or video screen to me).
I might just be missing the obvious, but it’s not immediately clear to me what’s causing the problem. One thing you might try is to observe the (selected) player object in the scene view while in play mode with the coordinate system gizmo in ‘local’ mode (I think there’s such a mode), and see if the ‘z’ (blue) axis is pointing in the expected direction.
Also, since this isn’t strictly top-down like the Evac City demo, what I’d probably do is cast a ray through the mouse position, intersect the ray with the ground plane, and then orient the player object towards the intersection point. That may be unrelated to the problem you’re seeing, but it seems like it’d be a more sensible approach given the context.
Oh, you ment that. That actually was a Placeholder Car I toyed around with how it reacts to explosions and it ended up sideways, giving you that monitor impression
Anyway, I tried to follow what you said. Result:
The character looks to where the X axis is, the control itself instead tries to go forward on Z axis. I’m guessing I need to change the characters axis from X to Z? How do I accomplish that?
I tried to capture the whole thing in movement too, see here: MP4-Video of the Problem
I’m pressing the key to walk forward there while rotating the mouse.
Thanks for the scripting suggestion. I keep that in mind and look more into it, for now I wouldn’t know where to begin to “intersect” a ray at all. I wanted to learn about handling Rays after I got my basic character movement, I just didn’t expect to need it before that and that getting the movement I want (check out the game “Alien Swarm”) would be so much trouble :shock:
Oh, weird - at that angle, it totally looked like a video monitor…
Anyway, it looks like the problem might be that your camera is positioned and oriented such that z is left to right and x is up and down in world space. The raycast-based solution I suggested would fix the problem, but if you want to stick with your current code for now, you should be able to simply swap the x and z elements of the ‘inputRotation’ vector (note that you might have to negate one or both elements as well).
Hi Jesse. We found the problem. There was a hiccup with the axis, apparently in 3dsmax the axis are all different (for example Z axis is up and down instead of Y axis) so the problem already began with the 3D models I received and not noticing the different axis system. I have sorted it out now. Still, I will try to use your other suggestion too in hopes to improve the code