Im kinda new to this whole thing with scripting and I’m still trying to get my head around it.
I currently got 2 scripts that are attached to a GameObject that also has a Rigidbody attached to it.
I’ve almost got the result that i I am looking for but there seem to be a problem.
You se, Im working on a top down shooter. A basic x,z 2.5D game. Im using a rigidbody as my “player” and i wanna make this GameObject rotate along the y axis and face the mouse cursor at all times. and this is where it gets tricky.
It seems that the rigidbody is still getting affected by other colliders in the scen. An example is where if you would “grind” against a wall the player starts to rotate. Afterwards the z axis of the player (forward) is not pointing towards the mouse cursor.
I dont know what im doing wrong. also since im new to unityscript ive been tweeking around with others scripts.
this script is attached to the Player and if there is a better way to write this please give me an example.
#pragma strict
var moveSpeed: float = 5.0;
var moveSnapp: float = 50.0;
function FixedUpdate ()
{
//Axis
var moveForward: float = Input.GetAxis("Horizontal");
var moveStraif: float = Input.GetAxis("Vertical");
var moveAxis: Vector3 = Vector3(moveForward, 0, moveStraif);
var targetVelo : Vector3 = moveAxis * moveSpeed;
var deltaVelo : Vector3 = targetVelo - rigidbody.velocity;
if(rigidbody.useGravity)
rigidbody.AddForce(deltaVelo * moveSnapp , ForceMode.Acceleration);
}
the second script is also attached to the player game object. This is originaly not my script.
#pragma strict
// LookAtMouse will cause an object to rotate toward the cursor, along the y axis.
// To use, drop on an object that should always look toward the mouse cursor.
// Change the speed value to alter how quickly the object rotates toward the mouse.
// speed is the rate at which the object will rotate
var speed = 25.0;
function FixedUpdate () {
// Generate a plane that intersects the transform's position with an upwards normal.
var playerPlane = new Plane(Vector3.up, rigidbody.position);
// Generate a ray from the cursor position
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
/* Determine the point where the cursor ray intersects the plane.
This will be the point that the object must look towards to be looking at the mouse.
Raycasting to a Plane object only gives us a distance, so we'll have to take the distance,
then find the point along that ray that meets that distance. This will be the point
to look at. */
var hitdist = 0.0;
// If the ray is parallel to the plane, Raycast will return false.
if (playerPlane.Raycast (ray, hitdist))
{
// Get the point along the ray that hits the calculated distance.
var targetPoint = ray.GetPoint(hitdist);
// Determine the target rotation. This is the rotation if the transform looks at the target point.
var targetRotation = Quaternion.LookRotation(targetPoint - rigidbody.position);
// Smoothly rotate towards the target point.
rigidbody.rotation = Quaternion.Slerp(rigidbody.rotation, targetRotation, speed * Time.deltaTime);
// Move the object forward.
rigidbody.position * speed * Time.deltaTime;
}
}
If I understand what you are trying to do (I admit to looking at your code, I have to run)…
Set your rigidbody to isKinematic and then manipulate it using its transform. This will make it affect other rigidBodys but it won’t be affected itself.
For the look at. You can grab our (currently) free package “UnityConstraints” from http://constraints.path-o-logical.com. There is a SmoothLookAt constraint you can drop on the object you want to turn and then just give it the other object as a target and it will just work. We have forums as well if you need help with setup. We just released this package but we are using it extensively in our TD game to make towers face targets. We haven’t announced the release yet because I still want to make videos in the next couple of days, but you can download it from the site there in the upper right.
EDIT: You can make the object face the position where the mouse ray hits the game, and set the SmoothLookAt Constraint to only output to the character’s WorldY (or LocalY if that looks funny for some reason), however you will need a target in the game to look at. There is a sample script I am adding that shows how to do this but I have to test it tonight to make sure it works. Email me at support@path-o-logical.com if you choose to use this tool and I will notify you as soon as the example script is tested (unless you want to test it yourself of course, the code is posted on the docs page under Examples and Ideas)
So i tried your tip. Make the rigidbody kinematic, it sounds as if that would be the solution but i could not make it work. The problem was that my player kind of sunk thru the ground. to 0 @ Y. and also when i started to move to rigidbody it snapped to the value of 1 and the -1 on the local transform.
maybe you have an example script on how i would achive what im looking for?
Can you explain a little more about what is happening? What is the -1?
I can’t scroll your posted code from my iPad so I’ll come back soon when I am on a regular computer.
Btw, I spoke too soon about UnityConstraints. I’m working on a big update to allow it all to work without a physical target so I removed the example I mentioned. I hope to have it out tonight.
Alright. The rigidbody is located at 0,0,0 in world space. When i try to move it with my w,a,s,d, the rigidbody snaps to an integer in the x and z axis. I think that the reason for this is that Im using the Input.GetAxis horizontal and vertical attached inside the rigidbody.transform.position vector. Cuz if I’m right, the GetAxis function only returns a value between 1 and -1. and thats whats moving my rigidbody.
I dont know if that helped you understand or made you even more confused, but unfortunetly, ive erased the actual script, so i cant paste it here… (stupid…)
Ah, right. Well if it is -1, 0, 1 (not pressure sensitive), then you next need to decide if you want to move at a constant rate or accelerate.
Just to keep the language clear, the “rigidbody” is not anywhere. it is on your GameObject. The GameObject has two components which we are talking about. A RigidBody and a Transform. Both of these have the ability to change the position and orientation of the GameObject. You will only use one or the other though. You use a RigidBody to move if you are using physics and isKinematic is OFF. If isKinematic is ON, then you should use the Transform to move the GameObject.
So you need to make two decisions, which should help guide your research
Physics based movement or directly set movement
Constant speed or acceleration (and to decelerate or not is another decision)