This script is attached to my player that have a rigidbody component. The rigidbody use gravity and is kinematic are set to ture.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RigidbodyPlayercontroller : MonoBehaviour
{
Rigidbody rb;
public float speed;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
float mH = Input.GetAxis("Horizontal");
float mV = Input.GetAxis("Vertical");
rb.velocity = new Vector3(mH * speed, rb.velocity.y, mV * speed);
}
}
But the player is not moving it does nothing. I tried speed value 1 and also 100.
Setting the velocity
of a kinematic Rigidbody doesn't cause any effect. Like the documentation says, use MovePosition to change the position of a kinematic Rigidbody .
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController: MonoBehaviour
{
public float speed;
private float vertical;
private float horizontal;
private Rigidbody rb;
void Start()
{
rb = GetComponent < Rigidbody > ();
}
void Update()
{
vertical = Input.GetAxis("Vertical") * speed;
horizontal = Input.GetAxis("Horizontal") * speed;
}
void FixedUpdate()
{
rb.MovePosition(
transform.position +
transform.right * horizontal * Time.fixedDeltaTime +
transform.forward * vertical * Time.fixedDeltaTime
);
}
}
it's a 2d or 3d game? i don't know much about 3d games, but that code seems to be like a 2d game controller, tell me if i'm wrong and i may help you
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.