- Home /
Alternative to transform.Translate?
Now was trying to create a simple movement script problem is that im using transform.Translate.See Here:
var speed : int = 30;
function Update ()
{
var horizontal = 0;
if (Input.GetKey(KeyCode.A)) horizontal = -1;
if (Input.GetKey(KeyCode.D)) horizontal += 1;
transform.Translate (horizontal * speed * Time.deltaTime*3, 0, 0);
var vertical = 0;
if (Input.GetKey(KeyCode.S)) vertical = -1;
if (Input.GetKey(KeyCode.W)) vertical += 1;
transform.Translate (0, vertical * speed * Time.deltaTime*3, 0);
}
What I have different from most movement script I only want my character moving on the X and Y axes. (As it is always moving on the Z axis)
Now as you probably know transform.Translate does not have collision detection. I found that out the hard way. I was wonder if there was an alternative to transform.Translate that would do much the same thing (albeit with collision)
I was trying to get other things to work but thing like Input.GetAxis("Vertical")but it was not liking(not moving when attached to) the Y axis. I'm not sure if this was just incorrect syntax or not.
So to review , I'm am trying to get a simple movement script that has collision and moves on the X and Y axes.(Preferably no sliding)
And before you ask it has a Rigidbody and CharacterController attached to it.
rigidbody.AddRelativeForce(); moves it relative to the current velocity / rotation AddForce moves relative to the world "velocity" and rotation
//along the xy plane
or rigidbody.AddForce(Vector3.right speed Time.deltaTime Input.GetAxis("Horizontal")); rigidbody.AddForce(Vector3.up speed Time.deltaTime Input.GetAxis("Vertical"));
Why do use both Rigidbody and CharacterController, check this
http://docs.unity3d.com/Documentation/Components/class-CharacterController.html
CharacterController component, not gameObject, Sorry if you got confused. And Ok I checked it but, what am I looking for?
Answer by SGamerXxX · Aug 19, 2012 at 10:12 PM
Well i got it working (mostly) I switched out the character controller component with a box collider. That worked but it when flying off at weird rotations, that is until I locked the rotations by putting this
transform.eulerAngles.x=0;
transform.eulerAngles.y=0;
transform.eulerAngles.z=0;
code into the Update Function. It now collides. Now if only I could get it to stop sliding when I collide.
Anyway thanks for your help even if I didn't use it , It did give me ideas on how to modify my code to work.
Answer by Rati_Old · Aug 17, 2012 at 01:37 AM
perhaps you should get your CharacterController
CharacterController MyControl= GetComponent<CharacterController>();
MyControl.Move(horizontal * speed * Time.deltaTime*3, 0, 0);
Answer by Rati_Old · Aug 17, 2012 at 01:37 AM
If you have a CharacterController, you could use it to move. It have a collision detection
// declaration
CharacterController MyControl = GetComponent<CharacterController>();
// in your update
MyControl.Move(0, vertical * speed * Time.deltaTime*3, 0);
First off, Edit your answer ins$$anonymous$$d of creating it twice, and second that doesn't work.
Your answer
Follow this Question
Related Questions
Moving on the Y Axis (Javascript) 2 Answers
Lerp movement speed 1 Answer
How to stop transform.Translate instantly ? 1 Answer
Mose look no limits 1 Answer
move character at constant speed, and detect collisions. 1 Answer