- Home /
How to make character asset moveable?
My problem is simple. I downloaded and imported into my scene free 'Sci-Fi Soldier' asset from AssetStore and would like to move it with Arrows/WASD keys but I don't know how to do this. Should I add script to it or maybe there is another way to do this? Also I'm curious why when I add Rigidbody component to my soldier he pass through floors? Shouldn't he behave with physics forces?
Answer by sujitmarcus · Apr 11, 2020 at 02:26 PM
You should learn Basic of unity first. Good news for you is that Unity Is offering their premium tutorials for free link text
Find your tutorial here
Good luck
Answer by terovsky · Apr 11, 2020 at 03:39 PM
Hey! Thanks for the reply but I must say I hate these kinds of answers. It's too general. It's like I would ask: "Hey, Could someone tell me how to change Windows desktop background?" and you would reply: "Hey, just read a Windows manual. You should find your answer here!". If you know the answer go straight to the point. I have found some clues that it may have something to do with the Character Controller component but I don't know Am I correct or not.
Answer by Quickz · Apr 11, 2020 at 07:49 PM
He's falling down due to gravity and he passes through the floor because he doesn't have a collider. Make sure to add a collider to both the floor and the soldier."Also I'm curious why when I add Rigidbody component to my soldier he pass through floors? Shouldn't he behave with physics forces?"
Regarding the character movement, there are many ways to accomplish that. The one example I can give is changing the velocity of a RigidBody.
Here's a small example:
using UnityEngine; [RequireComponent(typeof(Rigidbody))] public class PlayerMover : MonoBehaviour { [SerializeField] private float speed = 150f; private new Rigidbody rigidbody; private void Awake() { rigidbody = GetComponent<Rigidbody>(); } private void FixedUpdate() { ProcessMovement(); } private void ProcessMovement() { float moveX = 0f; float moveY = 0f; if (Input.GetKey(KeyCode.A)) { moveX = -1f; } else if (Input.GetKey(KeyCode.D)) { moveX = 1f; } if (Input.GetKey(KeyCode.W)) { moveY = 1f; } else if (Input.GetKey(KeyCode.S)) { moveY = -1f; } Vector2 movement = new Vector2(moveX, moveY).normalized; rigidbody.velocity = new Vector3( movement.x * Time.fixedDeltaTime * speed, rigidbody.velocity.y, movement.y * Time.fixedDeltaTime * speed); } }
That' s how the inspector might end up looking: [1]: https://imgur.com/a/utQt1Vg