- Home /
This question was
closed Feb 27 at 02:04 PM by
meesterturner for the following reason:
The question is answered, right answer was accepted
Question by
meesterturner · Feb 27 at 01:51 PM ·
c#character movement
Why wont the GameObject move?
The object has a rigidbody attached, but I haven't referenced it here as I am not using it, and the object is supposed to move when I press H, but it only does the Debug.Log command. How do I fix this, thanks. (There is also no character controller on it as it is not needed).
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BoatPlayerController : MonoBehaviour
{
public float moveSpeed = 10.0f;
public Vector3 newPosition;
public GameObject mainCamera;
public GameObject boatCamera;
public GameObject boat;
// Start is called before the first frame update
void Start()
{
newPosition = boat.transform.position;
}
// Update is called once per frame
void Update()
{
if(Input.GetKeyDown(KeyCode.H))
{
Debug.Log("Move Boat UP");
newPosition = transform.forward * moveSpeed;
}
}
}
Comment
Answer by JdeHaan · Feb 27 at 01:59 PM
You are not actually updating the transform of the player. You need to add transform.position += newPosition * Time.deltaTime;
after newPosition = transform.forward * moveSpeed;