- Home /
Question by
Quique_Maracas · Mar 22, 2021 at 11:25 AM ·
movement3djumpboardgame
Move while jump on board
Hello i am new in unity. And i need help with a game proyect. I have mi character on a board and i would like that my character move while jump on other square of board. I tried with this code. But i can´t get the results.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(Rigidbody))]
public class PlayerC : MonoBehaviour
{
public int speed = 6;
public float jumpForce = 1f;
public GameObject cameraView;
private Rigidbody rb;
private bool isGrounded;
// Start is called before the first frame update
void Start()
{
cameraView.transform.SetParent(transform);
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
if(Input.GetKey("up") && isGrounded){
PlayerRotacion(0);
rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
this.transform.Translate(Vector3.forward*this.speed*Time.deltaTime);
isGrounded = false;
}
if(Input.GetKey("right") && isGrounded){
PlayerRotacion(90);
rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
this.transform.Translate(Vector3.forward*this.speed*Time.deltaTime);
isGrounded = false;
}
if(Input.GetKey("left") && isGrounded){
PlayerRotacion(-90);
rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
this.transform.Translate(Vector3.forward*this.speed*Time.deltaTime);
isGrounded = false;
}
if(Input.GetKey("down") && isGrounded){
PlayerRotacion(180);
rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
this.transform.Translate(Vector3.forward*this.speed*Time.deltaTime);
isGrounded = false;
}
}
void OnCollisionStay(){
isGrounded = true;
}
void PlayerRotacion(int x){
cameraView.transform.SetParent(null);
this.transform.rotation = Quaternion.Euler(0,x,0);
cameraView.transform.SetParent(transform);
}
}
Thanks for your time and your help.
prototive.png
(37.0 kB)
Comment
Your answer
Follow this Question
Related Questions
In Air Movement 0 Answers
3D Object Jump Glitch 0 Answers
Moving player in direciton camera is facing 1 Answer
unity is not responding 1 Answer
why I don't have in the butter one click player movement ? 1 Answer