Question by
Kaze077 · Oct 14, 2021 at 01:10 PM ·
c#scripting problem
how do i make the player walk the direction the camera looks at
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour
{
public Transform groundCheckTransform;
private bool jumpKeyWasPressed;
private float horizontalInput;
private float verticalInput;
private Rigidbody rigitbodyComponent;
public LayerMask playerMask;
// Start is called before the first frame update
void Start()
{
rigitbodyComponent = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
jumpKeyWasPressed = true;
}
horizontalInput = Input.GetAxis("Horizontal");
verticalInput = Input.GetAxis("Vertical");
}
private void FixedUpdate()
{
rigitbodyComponent.velocity = new Vector3(horizontalInput * 10, rigitbodyComponent.velocity.y, verticalInput * 10);
if (Physics.OverlapSphere(groundCheckTransform.position, 0.1f, playerMask).Length == 0)
{
return;
}
if (jumpKeyWasPressed)
{
rigitbodyComponent.AddForce(Vector3.up * 8, ForceMode.VelocityChange);
jumpKeyWasPressed = false;
}
float moveHorizontal = Input.GetAxisRaw("Horizontal");
float moveVertical = Input.GetAxisRaw("Vertical");
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
transform.rotation = Quaternion.LookRotation(movement);
transform.Translate(movement * 5 * Time.deltaTime, Space.World);
}
}
internal class SerializedFieldAttribute : Attribute
{
}
so this is my first code how do i make him walk the direction the camera looks at when i press "W" he just walks to the north no matter where i look can anyone help. thanks.
Comment
Answer by EvilBob99 · Oct 14, 2021 at 03:30 PM
I'm here to help I'm having problems doing this with an active rag doll but that's a different problem, always I think you need to rotate the local transform point and set rotation to global now this may not work but I wish to help :)
and I said the wrong thing you need to set rotation point to local I'm an idiot sry :0
Your answer
Follow this Question
Related Questions
No overload for method. 1 Answer
How do I limit an object to rotate back and forth between 2 angles? 1 Answer
Big numbers 2 Answers
Scripting Errors 1 Answer