Answers for "how to add movement in unity 3d"

C#
2

how to make 3d player movement in unity

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

//Apply this script to what you want to move

public int MovementSpeed;

void Update(){


if(Input.GetKey("w"))
{
	transform.position += Vector3.forward * MovementSpeed * Time.deltaTime;
}
  
if(Input.GetKey("d"))
{
	transform.position += Vector3.right * MovementSpeed * Time.deltaTime;
}
  
if(Input.GetKey("a"))
{
	transform.position += Vector3.left * MovementSpeed * Time.deltaTime;
}
  
if(Input.GetKey("s"))
{
	transform.position += Vector3.back * MovementSpeed * Time.deltaTime;
}
Posted by: Guest on August-25-2021
3

how to add movement in unity 3d

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    //this is for first person movement
    public CharacterController controller;
    public float speed = 12f;
    Vector3 velocity;
    public float gravity = -9.81f;
    public float groundDistance = 0.4f;
    public LayerMask groundMask;
    public Transform groundCheck;
    bool isGrounded;
    float jumpheight = 5f;
 
    // Update is called once per frame
    void Update()
    {
        //movement
        float x = Input.GetAxis("Horizontal");
        float z = Input.GetAxis("Vertical");

        Vector3 move = transform.right * x + transform.forward * z;
        controller.Move(move * speed * Time.deltaTime);

        //gravity
        velocity.y += gravity * Time.deltaTime;

        isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
        controller.Move(velocity * Time.deltaTime);

        if(isGrounded && velocity.y >= 0)
        {
            velocity.y = -2f;
        }

        //jumping
        if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
        {
            velocity.y = Mathf.Sqrt(jumpheight * -2f * gravity);
        }

        //sprinting
        if(Input.GetKeyDown(KeyCode.LeftShift))
        {
            speed = 20f;
        }
        else
        {
            speed = 12f;
        }
    }
}
Posted by: Guest on August-04-2021
6

how to add movement in unity

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    private string moveInputAxis = "Vertical";
    

    public float moveSpeed = 0.1f;
    public Rigidbody rb;
    public bool cubeIsOnTheGround = true;


    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody>();
    }
    
    // Update is called once per frame
    void Update()
    {
       float moveAxis = Input.GetAxis(moveInputAxis); 

       ApplyInput(moveAxis);

       if(Input.GetButtonDown("Jump") && cubeIsOnTheGround == true)
       {
           rb.AddForce(new Vector3(0, 7, 0), ForceMode.Impulse);
           cubeIsOnTheGround = false;
       } 

    private void ApplyInput(float moveInput)
    {
        Move(moveInput);
    }

    private void Move(float input)
    {
        transform.Translate(Vector3.forward * input * moveSpeed);
    }    

    private void OnCollisionEnter(Collision collision) {
        if(collision.gameObject.tag == "Ground") {
            cubeIsOnTheGround = true;
        }
    }
}
Posted by: Guest on April-10-2020
1

how to add movement in unity 3d

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
	public Transform cam;
	public CharacterController controller;
	public float speed = 6f;
	public float turnSmoothTime = 0.4f;
	public float jumpForce = 10f;
	float turnSmoothVelocity;
	public LayerMask groundMask;
	public float groundDistance = 0.4f;
	public Transform groundCheck;
	bool isGrounded = true;
	Vector3 velocity;
	public float gravityForce = -19.62f;
	float _slopeAngle;

	//Update is called once per frame
	void Update()
	{
		float horizontal = Input.GetAxisRaw("Horizontal");
		float vertical = Input.GetAxisRaw("Vertical");
		Vector3 direction = new Vector3(horizontal, 0f, vertical);

		if (direction.magnitude >= 0.1f)
		{
			float targetAngle = Mathf.Atan2(direction.x, direction.y) * Mathf.Rad2Deg + cam.eulerAngles.y;
			float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
			transform.rotation = Quaternion.Euler(0f, angle, 0f);

			Vector3 moveDir = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
			controller.Move(moveDir.normalized * speed * Time.deltaTime);
		}

        //jumping
        if (isGrounded)
        {
			if (Input.GetKeyDown(KeyCode.Space))
			{
				velocity.y = Mathf.Sqrt(jumpForce * -2f * gravityForce);
			}
		}
	}

	void FixedUpdate()
	{
		if (isGrounded)
		{
			if (Input.GetKeyDown(KeyCode.LeftShift))
			{
				speed = 20f;
			}
			else if (Input.GetKeyUp(KeyCode.LeftShift))
			{
				speed = 6f;
			}
		}

		//Ground check
		isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);

		//gravity
		velocity.y += gravityForce * Time.deltaTime;

		isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
		controller.Move(velocity * Time.deltaTime);

		if (isGrounded && velocity.y < 0)
		{
			velocity.y = -2f;
		}
	}
}
Posted by: Guest on August-07-2021

C# Answers by Framework

Browse Popular Code Answers by Language