Question by
Zandlie · Sep 01, 2015 at 09:35 AM ·
charactercharactercontrollercontrollercharacter controllerboat
BoatRiderController?
Hello im trying to set up a boat controller. basically when you get on the boat press E, controls go to the boat i was wondering if im doing something wrong.
im using a third person controller right now with a mouse controlled camera.
this is the code so far C#
using UnityEngine;
using System.Collections;
public class BoatRide : MonoBehaviour {
CharacterController cc;
CharacterMotor cm;
GameObject player;
Transform DefualtPlayerTransform;
bool isDriving=false;
// Use this for initialization
void Start () {
cc = GameObject.FindObjectOfType<CharacterController> ();
cm = GameObject.FindObjectOfType<CharacterMotor> ();
player = cm.GameObject;
DefualtPlayerTransform = player.transform.parent;
}
bool IsplayerCloseToBoat()
{
return Vector3.Distance(GameObject.transform.position,
player.transform.position)<1;
}
void SetDriving(bool isDriving)
{
this.isDriving = isDriving;
SetDriving (!isDriving);
cm.enabled = !isDriving;
cc.enabled = !isDriving;
if (isDriving)
player.transform.parent = gameObject.transform;
else
player.transform.parent = DefualtPlayerTransform;
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown (KeyCode.E) && IsPlayerCloseToBoat ())
{
}
}
}
Comment
Answer by Positive7 · Sep 01, 2015 at 10:01 AM
There was some misspelling GameObject to gameObject etc...
using UnityEngine;
using System.Collections;
using UnityStandardAssets.Characters;
public class BoatRide : MonoBehaviour {
CharacterController cc;
CharacterMotor cm;
GameObject player;
public bool isDriving=false;
// Use this for initialization
void Start () {
cc = GameObject.FindObjectOfType<CharacterController> ();
cm = GameObject.FindObjectOfType<CharacterMotor> ();
player = cm.gameObject;
}
bool IsplayerCloseToBoat()
{
return Vector3.Distance(transform.position,
player.transform.position)<1;
}
void SetDriving(bool isDriving)
{
this.isDriving = isDriving;
SetDriving (!isDriving);
//cm.enabled = !isDriving;
cc.enabled = !isDriving;
if (isDriving)
player.transform.parent = gameObject.transform;
else
player.transform.parent = null;
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown (KeyCode.E) && IsplayerCloseToBoat())
{
Debug.Log("Driving");
}
}
}
thank on that note! but im getting an error saying i dont have a character motor. im using unity 5 and i guess its an old code or something
Your answer
Follow this Question
Related Questions
Boat Controller Help 0 Answers
Error CS0103 0 Answers
Player pushes object but gets pushed into the Wall/Boundary 0 Answers
How to smooth the movement of the camera? 0 Answers
fps script not working 1 Answer